Numerical Analysis - Spring 2021 (Final) - 2
Numerical Analysis - Spring 2021 (Final) - 2
Important
• الـ PDFده مجهود طالب مش دكتور ،وارد يكون فيه أخطاء .فـ ياريت محدش يعتمد اي حاجة هنا كـ
model answer
• السؤال اللي قدامه "☹" معناه إني معرفتش أحله أو إني مش واثق من إلمعلومة اللي عندي خالص
• أي تعديل في الـ PDFهينزل على الدرايف [Click Here]
Numerical Analysis – Spring 2021 Final Exam Page |2
𝟐𝒙𝟏 + 𝒙𝟐 + 𝒙𝟑 = 𝟓
𝟒𝒙𝟏 − 𝟔𝒙𝟐 = −𝟐
−𝟐𝒙𝟏 + 𝟕𝒙𝟐 + 𝟐𝒙𝟑 = 𝟗
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 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 |
- Ahmed Huzain
Numerical Analysis – Spring 2021 Final Exam Page |4
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]
𝑑𝑓 𝑑𝑓 −4
Step 2: Calculate gradiant F = 𝑑𝑥 𝑖 + 𝑑𝑦 𝑗 = 𝑡𝑎𝑛−1 (−2) = 63.435
Step 3: H matrix:
𝑑2𝑓 𝑑2𝑓
| |
𝑑𝑥 2 𝑑𝑥𝑑𝑦
𝑑2𝑓 𝑑2𝑓
| |
𝑑𝑦𝑑𝑥 𝑑𝑦 2
- Ahmed Huzain
Numerical Analysis – Spring 2021 Final Exam Page |6
Root = 3.344
b) Write pseudocode for calculating roots of the previous function in (3a). [3 marks]
- 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 |
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);
- Ahmed Huzain
Numerical Analysis – Spring 2021 Final Exam Page |9
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]
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;
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