0% found this document useful (0 votes)
41 views14 pages

Numerical Methods - L33

The document discusses three iterative methods for solving systems of linear equations: Gauss-Seidel, Jacobi, and Gauss elimination. For each method, it provides an example problem, shows the step-by-step working, and provides the MATLAB code to implement the method. It demonstrates applying each method to solve a 3x3 system of equations and displays the final solutions.

Uploaded by

eng.chem.2022.10
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)
41 views14 pages

Numerical Methods - L33

The document discusses three iterative methods for solving systems of linear equations: Gauss-Seidel, Jacobi, and Gauss elimination. For each method, it provides an example problem, shows the step-by-step working, and provides the MATLAB code to implement the method. It demonstrates applying each method to solve a 3x3 system of equations and displays the final solutions.

Uploaded by

eng.chem.2022.10
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/ 14

NUMERICAL METHODS WITH MATLAB L-3

Iterative Methods for Solving System


of Linear Equation
1- Gauss – Seidel.

1
NUMERICAL METHODS WITH MATLAB L-3

EXAMPLE2:

2
NUMERICAL METHODS WITH MATLAB L-3

3rd Iteration: x1= 3.0026, x2=-1.9554; x3=-0.9895

4th Iteration: x1=2.9921, x2= -1.9941, x3=-0.9966

5th Iteration: x1= 3.0003, x2= -1.9973, x3=-0.9994

6th Iteration x1= 2.9995, x2= -1.9997, x3=-0.9998

7th Iteration x1=3.0000 , x2= -1.9998 , x3= -1.0000

8th Iteration x1=3.0000, x2= -2.0000, x3= -1.0000

The final converged solution is x1=3, x2=-2, x3=-1

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%

Gauss – Seidel Method Code in MATLAB

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);

After Running the code will display in command window:

Enter the matrix=[3 1 -2 9;-1 4 -3 -8;1 -1 4 1]

Enter the value of tolerance=0.000001

The value of x1new=3.000000

The value of x2new=-2.000000

The value of x3new=-1.000000

4
NUMERICAL METHODS WITH MATLAB L-3

2- Jacobi Method
Let: given the equation :

a1x1 + b1x2 + c1x3 = d1

a2x1 + b2x2 + c2x3 = d2

a3x1 + b3x2 + c3x3 = d3

If given system of equation is diagonally dominant :

x1n+1 = (d1-b1x2-c1x3)/a1

x2n+1 = (d2-a2x1-c2x3)/b2

x3n+1 = (d3-a31x1-b3x2)/c3

Special condition for Jacobi method:

1- |a1| greater than or equal |b1|+|c1|


2- |b2| greater than or equal |a2|+|c2|
3- |c3| greater than or equal |a3|+|b3|

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:

6x1 = 11+ 2x2 - x3


7x2 = 5 + 2x1 - 2x3
5x3 = 1 + x1 + 2x2

5
NUMERICAL METHODS WITH MATLAB L-3

Let: x1=0, x2=0, x3=0

First Iteration:

Second Iteration: x1=1.833, x2=0.714, x3=0.2

Third Iteration: x1=2.038, x2=1.181, x3=0.852

Final Iteration: x1=2, x2=1, x3=1

6
NUMERICAL METHODS WITH MATLAB L-3

Jacobi Method Code in MATLAB


clc
clear
A=input('Enter the matrix=');
tol=input('Enter the value of tolerance=');
Ea=100;
x1old=0;x2old=0;x3old=0;
while abs(Ea)>tol;
x1new=(A(1,4)-A(1,2)*x2old-A(1,3)*x3old)/A(1,1);
x2new=(A(2,4)-A(2,1)*x1old-A(2,3)*x3old)/A(2,2);
x3new=(A(3,4)-A(3,1)*x1old-A(3,2)*x2old)/A(3,3);
Ea=abs(x1new-x1old)/x1new*100;
x1old=x1new;
x2old=x2new;
x3old=x3new;
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);

After Running the code will display in command window:

Enter the matrix=[6 -2 1 11;-2 7 2 5;1 2 -5 -1]

Enter the value of tolerance=0.000001

The value of x1new=2.000000

The value of x2new=1.000000

The value of x3new=1.000000

7
NUMERICAL METHODS WITH MATLAB L-3

3-Guss Elimination Method:

8
NUMERICAL METHODS WITH MATLAB L-3

Example:

9
NUMERICAL METHODS WITH MATLAB L-3

Example2:

10
NUMERICAL METHODS WITH MATLAB L-3

Guss Elimination Method Code in MATLAB


clc
clear
A=input('Enter the value of matrix=');
b=input('Enter the value of b=');
n=length(b);
B=[A b']
for k=1:n-1;
for i= k+1:n;
lambda = A(i,k)/A(k,k);
A(i,k:n)= A(i,k:n)-lambda*A(k,k:n);
b(i)= b(i) - lambda*b(k);
B=[A b']
end
end
Anew=[A b'];
x(n)=Anew(n,n+1)/Anew(n,n);
for i=n-1:-1:1;
sum=0;
for j=i+1:n+1-1
sum=sum+Anew(i,j)*x(j);
x(i)=(Anew(i,n+1)-sum)/Anew(i,i);
end
end
fprintf('x=%f\n',x);

After running the code will display in command window:

11
NUMERICAL METHODS WITH MATLAB L-3

Enter the value of matrix=[3 1 -2;-1 4 -3;1 -1 4];

Enter the value of b=[9 -8 1]

B=

3 1 -2 9

-1 4 -3 -8

1 -1 4 1

B=

3.0000 1.0000 -2.0000 9.0000

0 4.3333 -3.6667 -5.0000

1.0000 -1.0000 4.0000 1.0000

B=

3.0000 1.0000 -2.0000 9.0000

0 4.3333 -3.6667 -5.0000

0 -1.3333 4.6667 -2.0000

B=

3.0000 1.0000 -2.0000 9.0000

0 4.3333 -3.6667 -5.0000

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

Step1: B(2,1)=1/3*3-1=0, B(2,2)=1/3*1+4=4.3333, B(2,3)=1/3*(-2)-3=-3.6667,


B(2,4)=1/3*9-8=-5. Then:

B=

3.0000 1.0000 -2.0000 9.0000

0 4.3333 -3.6667 -5.0000

1.0000 -1.0000 4.0000 1.0000

Step2: B(3,1)=-1/3*3+1=0, B(3,2)=-1/3*1-1=-1.3333, B(3,3)=-1/3*(-2)+4=4.6667),


B(3,4)=-1/3*9-5=-8. Then:

B=

3.0000 1.0000 -2.0000 9.0000

0 4.3333 -3.6667 -5.0000

0 -1.3333 4.6667 -2.0000

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=

3.0000 1.0000 -2.0000 9.0000

0 4.3333 -3.6667 -5.0000

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

You might also like