Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
34 views3 pages

Linear Equation Solvers: Gauss, Seidel, Thomas

Uploaded by

Jatin Kolambe
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)
34 views3 pages

Linear Equation Solvers: Gauss, Seidel, Thomas

Uploaded by

Jatin Kolambe
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/ 3

Gauss Elimination Method:

Program-
clc;
clear all;
a=input('Enter the A matrix= ')
b=input('Enter the B matrix= ')
n=length(b)
Aug=[a b]
x=zeros(n,1)
for j=1:n-1
for i=j+1:n
m=Aug(i,j)/Aug(j,j)
Aug(i,:)=Aug(i,:)-m*Aug(j,:)
end
end
Aug
x(n)=Aug(n,n+1)/Aug(n,n)
for k=n-1:-1:1
x(k)=(Aug(k,n+1)-Aug(k,k+1:n)*x(k+1:n))/Aug(k,k)
fprintf('\n x(%d)=%f',k,x(k))
end

Output
Enter the A matrix= [3 6 1;1 3 2;2 4 3];
Enter the B matrix= [16;9;13];
x =1
2
1
Solver
A=[3 6 1;1 3 2;2 4 3];
B=[16;9;13];
X=A\B
X=
1
2
1
Gauss Seidel Method:
Program-
clc;
clear all;
fstr1=input('\n Enter the function x=','s');
fx=inline(fstr1);
fstr2=input('\n Enter the function y=','s');
fy=inline(fstr2);
fstr3=input('\n Enter the function z=','s');
fz=inline(fstr3);
x=0;
y=0;
z=0;
n=input('Number of iterations n=');
for i=1:n
x=fx(y,z);
y=fy(x,z);
z=fz(x,y);
fprintf('\n%f \t%f \t%f',x,y,z)
end

Output

Enter the function x=(12+y-2*z)/3


Enter the function y=(11-x-3*z)/2
Enter the function z=2*x-2*y-2
Number of iterations n=10
4.000000 3.500000 -1.000000
5.833333 4.083333 1.500000
4.361111 1.069444 4.583333
1.300926 -2.025463 4.652778
5.502118 2.894099 3.216039
2.820674 -0.734395 5.110138
0.348443 -2.339428 3.375743
Thomas Algorithm for Tridiagonal Method:
Program-
clc;
clear all;
a=input('Enter the matrix a=');
b=input('Enter the matrix b=');
n=length(b);
x=zeros(n,1);
for i=2:n
m=a(i,i-1)/a(i-1,i-1);
a(i,i)=a(i,i)-m*a(i-1,i);
b(i)=b(i)-m*b(i-1);
end
for i=n:-1:1
x(i)=(b(i)-a(i,i+1:n)*x(i+1:n))/a(i,i)
fprintf('\n x(%d)=%f',i,x(i))
end

Output
Enter the matrix a=[10 2 0 0;2 9 3 0;0 1 10 4;0 0 3 11]
Enter the matrix b=[12;14;15;14]
x =1.0000
1.0000
1.0000
1.0000
Solver- A=[10 2 0 0;2 9 3 0;0 1 10 4;0 0 3 11];
B=[12;14;15;14];
X=A\B
X =1.0000
1.0000
1.0000
1.0000

You might also like