Numerical Methods - L33
Numerical Methods - L33
1
NUMERICAL METHODS WITH MATLAB L-3
EXAMPLE2:
2
NUMERICAL METHODS WITH MATLAB L-3
To compute Ea:
3
NUMERICAL METHODS WITH MATLAB L-3
Ea1=abs(3-0)/3*100=100%, Ea1=abs(2.875-3)/2.875*100=4%,
Ea2=abs(3.0026-2.875)/3.0026*100=4%
clc
clear
A=input('Enter the matrix=');
tol=input('Enter the value of tolerance=');
Ea=100;
x1new=0;x2new=0;x3new=0;
while abs(Ea)>tol;
x1old=x1new;
x2old=x2new;
x3old=x3new;
x1new=(A(1,4)-A(1,2)*x2old-A(1,3)*x3old)/A(1,1);
x2new=(A(2,4)-A(2,1)*x1new-A(2,3)*x3old)/A(2,2);
x3new=(A(3,4)-A(3,1)*x1new-A(3,2)*x2new)/A(3,3);
Ea=abs(x1new-x1old)/x1new*100;
end
fprintf('The value of x1new=%f\n',x1new);
fprintf('The value of x2new=%f\n',x2new);
fprintf('The value of x3new=%f\n',x3new);
4
NUMERICAL METHODS WITH MATLAB L-3
2- Jacobi Method
Let: given the equation :
x1n+1 = (d1-b1x2-c1x3)/a1
x2n+1 = (d2-a2x1-c2x3)/b2
x3n+1 = (d3-a31x1-b3x2)/c3
Example1:
Use the Jacobi iteration method to obtain the solution of the following equations:
6x1 - 2 x2 + x3 = 11
x1 + 2x2 - 5x3 = -1
-2x1 +7x2 +2x3 = 5
The solution:
Re-write the equations such that each equation has the unknown with largest coefficient
on the left hand side:
5
NUMERICAL METHODS WITH MATLAB L-3
First Iteration:
6
NUMERICAL METHODS WITH MATLAB L-3
7
NUMERICAL METHODS WITH MATLAB L-3
8
NUMERICAL METHODS WITH MATLAB L-3
Example:
9
NUMERICAL METHODS WITH MATLAB L-3
Example2:
10
NUMERICAL METHODS WITH MATLAB L-3
11
NUMERICAL METHODS WITH MATLAB L-3
B=
3 1 -2 9
-1 4 -3 -8
1 -1 4 1
B=
B=
B=
0 0 3.5385 -3.5385
x=3.000000
x=-2.000000
x=-1.000000
12
NUMERICAL METHODS WITH MATLAB L-3
To clarify:
B=
3 1 -2 9
-1 4 -3 -8
1 -1 4 1
B=
B=
Step3; B(3,2)=(1.3333/4.3333)*4.3333-1.3333=0;
B(3,3)=(1.3333/4.3333)*(-3.6667)+4.6667=3.5385,
B(3,4)=(1.3333/4.3333)*(-5)-2=-3.5385
B=
0 0 3.5385 -3.5385
13
NUMERICAL METHODS WITH MATLAB L-3
0*x1+0*x2+3.5385*x3=-3.5385--------x3=-1 .
0*x1+4.3333*x2-3.6667*(-1)=-5 ------x2=-2
3*x1+1*(-2)-2*(-1)=9,-------------------x1=3
14