You are on page 1of 2

Thomas algorithm Programme:-

Q. Solve the following equation using Thomas algorithm:-


2.04a-b= 40.8
-a+2.04b= 0.8
-b+2.04c= 0.8
-c+2.04d=200.8

Code:-

function x= thomas(e,f,g,r)
%input
%e= subdiagonal vector
%f=diagonal vector
%g=super diagonal matrix
%r=right handside vector
%x= solution
n=length(f);
%forward elimination
for k=2:n
factor=e(k)/f(k-1);
f(k)=f(k)-factor*g(k-1);
r(k)=r(k)-factor*r(k-1);
end
%back subsitution
x(n)=r(n)/f(n);
for k=n-1:-1:1
x(k)=(r(k)-g(k)*x(k+1))/f(k);
end
for i=1:length(x)
fprintf('\nx%d=%f\n',i,x(i));
end

Output:-

You might also like