0% found this document useful (0 votes)
5 views

Numerical Analysis - Spring 2021 (Final) - 2

Numerical questions

Uploaded by

medohegazy509
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Numerical Analysis - Spring 2021 (Final) - 2

Numerical questions

Uploaded by

medohegazy509
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

‫‪Numerical Analysis – Spring 2021 Final Exam‬‬

‫‪Important‬‬
‫• الـ ‪ PDF‬ده مجهود طالب مش دكتور‪ ،‬وارد يكون فيه أخطاء‪ .‬فـ ياريت محدش يعتمد اي حاجة هنا كـ‬
‫‪model answer‬‬
‫• السؤال اللي قدامه "☹" معناه إني معرفتش أحله أو إني مش واثق من إلمعلومة اللي عندي خالص‬
‫• أي تعديل في الـ ‪ PDF‬هينزل على الدرايف ‪[Click Here] ‬‬
Numerical Analysis – Spring 2021 Final Exam Page |2

Question 1) [10 Marks]


a) Solve the following system of equation using LU Decomposition with partial pivoting: [5 marks]

𝟐𝒙𝟏 + 𝒙𝟐 + 𝒙𝟑 = 𝟓
𝟒𝒙𝟏 − 𝟔𝒙𝟐 = −𝟐
−𝟐𝒙𝟏 + 𝟕𝒙𝟐 + 𝟐𝒙𝟑 = 𝟗
Step 1: Augmented Matrix A & B Step 2: Swap R1 <-> R2 (because 4 > 2)
A: B: P21A:
|2 1 1 | |5| |4 -6 0 |
|4 -6 0 | |-2| |2 1 1 | m21 = 2/4
| -2 7 2 | |9| | -2 7 2 | m31 = -2/4

Step 3: Apply R2 = R2 - R1(m21) Step 4: Apply R3 = R3 - R1(m31)


P21A: P21A:
|4 -6 0 | |4 -6 0 |
|0 4 1 | |0 4 1 |
| -2 7 2 | |0 4 2 | m32 = 4/4

Step 5: Apply R3 = R3 - R2(m32) Step 6: P21B → B


P21A: |5| |-2|
|4 -6 0 | |-2| |5|
|0 4 1 | |9| |9|
|0 0 1 |

Step 7: Get L Matrix Step 8: Apply LC = B:


L: |1 0 0 | | C1 | | -2| c1 = -2
|1 0 0 | | 0.5 1 0 | | C2 | = | 5 | c2 = 6
| 0.5 1 0 | | -0.5 1 1 | | C3 | | 9 | c3 = 2
| -0.5 1 1 |

Step 9: Apply UX = C:
|4 -6 0 | | X1 | | -2| c1 = 1
|0 4 1 | | X2 | = | 6 | c2 = 1
|0 0 1 | | X3 | | 2 | c3 = 2

- Ahmed Huzain
Numerical Analysis – Spring 2021 Final Exam Page |3

b) Use Gauss Jordan with partial pivoting to solve the system of equations given in (a) [5 marks]

𝟐𝒙𝟏 + 𝒙𝟐 + 𝒙𝟑 = 𝟓
𝟒𝒙𝟏 − 𝟔𝒙𝟐 = −𝟐
−𝟐𝒙𝟏 + 𝟕𝒙𝟐 + 𝟐𝒙𝟑 = 𝟗
Step 1: Augmented Matrix Step 2: Swap R1 <-> R2 (because 4 > 2)
A: A:
|2 1 1 5 | |4 -6 0 -2 |
|4 -6 0 -2 | |2 1 1 5 |
| -2 7 2 9 | | -2 7 2 9 |

Step 3: Divide R1 by 4 Step 4: R2 = R2 + -2 * R1


|1 -1.5 0 -0.5 | |1 -1.5 0 -0.5 |
|2 1 1 5 | |0 4 1 6 |
| -2 7 2 9 | | -2 7 2 9 |

Step 5: R3 = R3 + 2 * R1 Step 6: Divide R2 by 4


|1 -1.5 0 -0.5 | |1 -1.5 0 -0.5 |
|0 4 1 6 | |0 1 0.3 1.5 |
|0 4 2 8 | |0 4 2 8 |

Step 7: R1 = R1 + 1.5 * R2 Step 8: R3 = R3 + -4 * R2


|1 0 0.4 1.8 | |1 0 0.4 1.8 |
|0 1 0.3 1.5 | |0 1 0.3 1.5 |
|0 4 2 8 | |0 0 1 2 |

Step 9: R1 = R1 + -0.3 * R3 Step 10: R2 = R2 + -0.3 * R3


|1 0 0 1 | |1 0 0 1 | x1 = 1
|0 1 0.3 1.5 | |0 1 0 1 | x2 = 1
|0 0 1 2 | |0 0 1 2 | x3 = 2

- Ahmed Huzain
Numerical Analysis – Spring 2021 Final Exam Page |4

Question 2) [12 Marks]


a) Use the bisection method (halving) to find the smallest positive root of the following equation
f(x) = −𝟐𝒙𝟔 − 𝟏. 𝟔𝒙𝟒 + 𝟏𝟐𝒙 − 𝟏 until the estimated error Ea= 5%. Employ the initial guess of x1 = 0 and xu=l
[ 5 marks ]

i Xl F(xl) xu F(xu) xr F(xr) ea%


1 0 -1 1 7.4 0.5 4.869 ----
2 0 -1 0.5 4.869 0.25 1.993 100%
3 0 -1 0.25 1.993 0.125 0.5 100%
4 0 -1 0.125 0.5 0.063 -0.244 98.413%
5 0.063 -0.244 0.125 0.5 0.094 0.128 32.979%
6 0.063 -0.244 0.094 0.128 0.079 -0.052 18.987%
7 0.079 -0.052 0.094 0.128 0.087 0.044 9.195%
8 0.079 -0.052 0.087 0.044 0.083 -0.004 4.819%

Root = 0.083

b) Write C++ program for getting the root for problem 2a using recursive function

#include <iostream>
#include <cmath>
using namespace std;
const double eps = 5.0;
int iter = 0;
double xr = 0, xrOld = 0, error = 0;
double f(double x){
return -2 * pow(x, 6) - 1.6 * pow(x, 4) + 12 * x - 1;
}
double bisect(double xl, double xu, double error, int iter, double xr){
xrOld = xr;
xr = (xl + xu) / 2;
error = abs((xr - xrOld) / xr) * 100;
if (f(xl) * f(xr) > 0) xl = xr;
else if (f(xl) * f(xr) < 0) xu = xr;
else return xr;
iter++;
if(error > eps) bisect(xl, xu, error, iter, xr);
else return xr;
}
int main(){
float xl = 0, xu = 1;
cout << "Root =" << bisect(xl, xu, error, iter, xr) << endl;
return 0;
}

- Ahmed Huzain
Numerical Analysis – Spring 2021 Final Exam Page |5

c) Find the minimum value of 𝒇(𝒙, 𝒚) = (𝒙 − 𝟐)𝟐 + (𝒚 − 𝟑)𝟐 starting at x = 1 and y = 1, using the conjugate
gradiant method [5 marks]

Step 1: Get df/dx & df/dy


df/dx: 2 * (x - 2) = -2
df/dy: 2 * (y - 3) = -4

𝑑𝑓 𝑑𝑓 −4
Step 2: Calculate gradiant F = 𝑑𝑥 𝑖 + 𝑑𝑦 𝑗 = 𝑡𝑎𝑛−1 (−2) = 63.435

Step 3: H matrix:
𝑑2𝑓 𝑑2𝑓
| |
𝑑𝑥 2 𝑑𝑥𝑑𝑦
𝑑2𝑓 𝑑2𝑓
| |
𝑑𝑦𝑑𝑥 𝑑𝑦 2

Step 4:Substitute in H matrix:


| 2 0 |
| 0 2 |

Step 5: Calculate Det(H)


| H | = 2 (2) - 0 (0) = 4

Step 6: Final result:


Det|H| > 0 AND d2f/dx^2 > 0 → local min

- Ahmed Huzain
Numerical Analysis – Spring 2021 Final Exam Page |6

Question 3) [13 Marks]


a) Determine the real root of f(x) = −𝒙𝟑 + 𝟕. 𝟖𝟗𝒙 + 𝟏𝟏. Using the Secant Method Use an initial guess of xi-1
= 3 and x0 = 5 and iterate until ea% < 0.1. [5 marks]

i Xi-1 F(xi-1) xi F(xi) ea%


1 3 7.67 5 -74.55 ----
2 5 -74.55 3.187 3.775 56.8%
3 3.187 3.775 3.274 1.738 2.65%
4 3.274 1.738 3.348 -0.112 2.21%
5 3.348 -0.112 3.344 -0.01 0.14%
6 3.344 -0.01 3.344 -0.01 0.02%

Root = 3.344

b) Write pseudocode for calculating roots of the previous function in (3a). [3 marks]

FUNCTION Secant(a, b, imax, es)


iter = 0
DO
x = a – (f(a)*(b-a)/f(b)-f(a))
iter = iter + 1
ea = abs((xr - xrold)/xr)*100
b = a
a = x
IF ea < es OR iter ≥ imax EXIT
END DO
Secant = x
END Secant

- Ahmed Huzain
Numerical Analysis – Spring 2021 Final Exam Page |7

c) Write the algorithm for Gauss elimination using pivoting to solve system of equation and then write C++
code for it. [5 marks]

Algorithm

1- Forward elimination
| 𝑎11 𝑎12 𝑎13 𝑏1 | If 𝑎11 < 𝑎21 𝑂𝑅 𝑎11 < 𝑎31 𝑇𝐻𝐸𝑁 𝑠𝑤𝑎𝑝
| 𝑎21 𝑎22 𝑎23 𝑏2 |
| 𝑎31 𝑎32 𝑎33 𝑏3 |

| 𝑎11 𝑎12 𝑎13 𝑏1 |


| 0 𝑎22 𝑎23 𝑏2 | If 𝑎22 < 𝑎32 𝑇𝐻𝐸𝑁 𝑠𝑤𝑎𝑝
| 0 𝑎32 𝑎33 𝑏3 |

| 𝑎11 𝑎12 𝑎13 𝑏1 |


| 0 𝑎22 𝑎23 𝑏2 |
| 0 0 𝑎33 𝑏3 |

2- Backward substitution
𝑏3
𝑥3 =
𝑎33
𝑏3 − 𝑎23 ∗ 𝑥3
𝑥2 =
𝑎22
𝑏1 − 𝑎12 ∗ 𝑥2 − 𝑎13 ∗ 𝑥3
𝑥1 =
𝑎11

Code:
main():
int main(){
float _a[3][4];
float _m21 = 0, _m31 = 0, _m32 = 0;
cout << "Enter matrix" << endl;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
{
cout << "row" << i << j << ": ";
cin >> _a[i][j];
}
}
GJE(_a, _m21, _m31, _m32);
}

- Ahmed Huzain
Numerical Analysis – Spring 2021 Final Exam Page |8

Swap():
void swap(float _a[][4], int r1, int r2){
cout << "Swap R" << r1 << " <-> R" << r2 << endl;
for(int i = 0; i < 4; i++){
float temp = _a[r1][i];
_a[r1][i] = _a[r2][i];
_a[r2][i] = temp;
}
}

GJE():
void GJE(float _a[][4], float &m21, float &m31, float &m32){
// Check if pivot is larger than the elements below it
if(abs(_a[0][0]) < abs(_a[1][0]) || abs(_a[0][0]) < abs(_a[2][0]))
if(abs(_a[1][0]) > abs(_a[2][0]))
swap(_a, 0, 1);
else swap(_a, 0, 2);

m21 = _a[1][0]/_a[0][0]; // Calculate m21


m31 = _a[2][0]/_a[0][0]; // Calculate m31

for(int j = 0; j < 4; j++){


_a[1][j] -= _a[0][j] * m21; // R2 = R2 - R1(m21)
_a[2][j] -= _a[0][j] * m31; // R3 = R3 - R1(m31)
}

// Check if m22 is larger than the element below it


if(abs(_a[1][1]) < abs(_a[2][1]))
swap(_a, 1, 2);

m32 = _a[2][1]/_a[1][1]; // Calculate m32

for(int j = 0; j < 4; j++)


_a[2][j] -= _a[1][j] * m32; // R3 = R3 - R2(m32)

float x3 = _a[2][3] / _a[2][2]; // Get x3


float x2 = (_a[1][3] - (_a[1][2] * x3)) / _a[1][1]; // Get x2
float x1 = (_a[0][3] - ((_a[0][1] * x2) + (_a[0][2] * x3))) / _a[0][0]; // Get x1
cout << "Gauss Jordan Result" << endl << "X1 = " << x1 << endl << "X2 = "
<< x2 << endl << "X3 = " << x3 << endl;
}

- Ahmed Huzain
Numerical Analysis – Spring 2021 Final Exam Page |9

Question 4) [15 Marks]


a) Use the golden-section search to find the maximum of
f (x) = −𝟏. 𝟓𝒙𝟔 − 𝟐𝒙𝟒 + 𝟏𝟐𝒙 Within the interval x1 = 0 and Xu= 2. [5 marks]

5 ‫ انا افترضت‬,iterations ‫السؤال هنا مش محدد عدد‬

i Xl F(xl) x2 F(x2) x1 F(x1) xu F(xu) d


1 0 0 0.764 8.188 1.236 4.816 2 -104 1.236
2 0 0 0.472 5.548 0.764 8.188 1.236 4.816 0.764
3 0.472 5.548 0.764 8.188 0.944 8.678 1.236 4.816 0.472
4 0.764 8.188 0.944 8.678 1.056 8.105 1.236 4.816 0.292
5 0.764 8.188 0.876 8.656 0.944 8.678 1.056 8.105 0.18

b) Use Newton method to find the real root to four decimals of the following polynomials
f(x) = 𝒙𝟒 + 𝒙𝟑 − 𝟒𝒙𝟐 − 𝟑𝒙 + 𝟑. Use an initial guess of x0 = 5 and iterate until ea < 10%. [4 marks]

i Xi F(xi) F`(xi) Ea%


1 5 638 532 ----
2 3.801 197.455 229.596 31.544%
3 2.941 59.831 101.173 29.242%
4 2.35 17.336 46.679 25.149%
5 1.979 4.486 23.92 18.747%
6 1.791 0.83 15.275 10.497%
7 1.737 0.064 13.119 3.109%

Root = 1.933

- Ahmed Huzain
Numerical Analysis – Spring 2021 Final Exam P a g e | 10

c) Write a C++ program for getting the root using Simple fixed point for a function f(x) = -x3 + 7.89x + 11. Use
initial guess ofx0 = 1 and iterate until ea < 2 % [3 marks]

#include <iostream>
#include <cmath>
using namespace std;
double eps = 2;
int iter = 0;
double xiPlus1 = 0, xi = 0, error = 0;

double f(double x){


return cbrt(7.89*x+11);
}

double fixedpoint(double x){


xi = x;
xiPlus1 = f(xi);
cout << "i=" << iter << " | Xi= " << x << " | Xi+1= " << xiPlus1 << " | E= %" <<
error << endl;
error = abs((xiPlus1 - xi) / xiPlus1) * 100;
iter++;
if (error > eps) fixedpoint(xiPlus1);
return xi;
}

int main(){
double xo = 1;
double root;
root = fixedpoint(xo);
cout << "root= " << root << endl;
return 0;
}

𝒂 𝟏 𝟏 𝒂
d) If [ ]=4,[ ] = 12. Use Cramer’s Rule to find values of x1, x2 [3 marks]
𝒃 𝟕 𝟑 𝒃
𝒙𝟏 + 𝒙𝟐 = 𝒂
𝟑𝒙𝟏 + 𝟕𝒙𝟐 = 𝒃

1 1
[ ]
3 7
D=1*7–1*3=4

D1 = 4, D2 = 12
𝐷1 4
X1 = =4 =1
𝐷
𝐷2 12
X2 = = =3
𝐷 4

- Ahmed Huzain

You might also like