Solving
the system of linear equations using Gauss-Seidel method
clc;
close;
clear;
a
= input("Enter the co-efficient matrix:");
b
= input("Enter the constant matrix:");
n
= size(a);
oldx
= input("Initial guesses:");
x
= oldx;
for k
= 1:20
fprintf("\n
Iteration %d:\t ", k)
for i =
1:n
sum = 0;
for j
= 1:n
if i~=j
sum = sum+a(i,j) * x(j);
end
end
x(i) = (b(i)-sum)/a(i,i);
fprintf("x(%d)=%0.4f\t",i,x(i))
end
if abs(max(x)-max(oldx))<=0.0001
break;
else
oldx
= x;
end
end
Note: Enter the coefficient matrix after making matrix diagonally dominant
Example
Use Gauss Siedel method to solve: $$5x+2y+z=1; x+4y+2z=0; x+2y+5z=3$$ considering the initial guess (1,0,3)
Output
Enter the co-efficient matrix:[5 2 1;1 4 2;1 2 5]
Enter the constant matrix:[1;0;3]
Initial guesses:[1;0;3]
Iteration 1: x(1)=-0.4000 x(2)=-1.4000 x(3)=1.2400
Iteration 2: x(1)=0.5120 x(2)=-0.7480 x(3)=0.7968
Iteration 3: x(1)=0.3398 x(2)=-0.4834 x(3)=0.7254
Iteration 4: x(1)=0.2483 x(2)=-0.4248 x(3)=0.7202
Iteration 5: x(1)=0.2259 x(2)=-0.4166 x(3)=0.7215
Iteration 6: x(1)=0.2223 x(2)=-0.4163 x(3)=0.7221
Iteration 7: x(1)=0.2221 x(2)=-0.4166 x(3)=0.7222
Iteration 8: x(1)=0.2222 x(2)=-0.4166 x(3)=0.7222
Rayleigh power method
Program to find the
numerically largest eigenvalue and eigenvector using Rayleigh power method
clc;
clear
n
= input("Enter the number of varibles:");
a
= input("Enter the matrix:");
x0
= input("Initial guesses:");
for k
= 1:5
C=a*x0;
m=max(abs(C));
x=(C./m);
if abs((x)-(x0))<=0.0001
break;
else
x0
= x;
end
fprintf("---------------------------------------------\nIteration
%d :\n",k);
fprintf("The Eigen vector:\n");
disp(x);
fprintf("The largest Eigen value: %f\n",m);
end
Example
Find the numerically largest eigenvalue and eigenvector of
by taking [1 0 0]' as the initial approximation using Rayleigh power method
Output
Enter the number of
varibles:3
Enter the matrix:[2 0 1;0 2 0; 1 0 2]
Initial guesses:[1; 0 ;0]
---------------------------------------------
Iteration 1 :
The Eigen vector:
1.0000
0
0.5000
The largest Eigen value:
2.000000
---------------------------------------------
Iteration 2 :
The Eigen vector:
1.0000
0
0.8000
The largest Eigen value:
2.500000
---------------------------------------------
Iteration 3 :
The Eigen vector:
1.0000
0
0.9286
The largest Eigen value:
2.800000
---------------------------------------------
Iteration 4 :
The Eigen vector:
1.0000
0
0.9756
The largest Eigen value:
2.928571
---------------------------------------------
Iteration 5 :
The Eigen vector:
1.0000
0
0.9918
The largest Eigen value:
2.975610
No comments:
Post a Comment