Can someone with experience with writing matlab code help me
Solution
Matlab code:
% we need to create a matrix with the help of upperdiagonal, diagonal, lower diagonals.
a=[1 1 3 -2 -2 -2 0 0]; % is a upper diagonal vector with a(n)=0.
b=[0 -1 4 0 -2 -4 2 0]; % is a upper diagonal vector with b(1)=0.
d=[-1 4 1 -1 -2 -2 4 2]; % is a diagonal vector.
r=[7 13 -3 -2 -4 -28 26 10]; % is a right hand side vector.
Now we have to call a function thomas.m which gives a direct solution vector x
function x=thomas(a,b,d,r)
n=length(d);
a(1)=a(1)/d(1);
r(1)=r(1)/d(1);
for i=2:n-1
denom=d(i)-b(i)*a(i-1);
if (denom==0),error(\'zero in denominator\'),end
a(i)=a(i)/denom;
r(i)=(r(i)-b(i)*r(i-1))/denom;
end
r(n)=(r(n)-b(n)*r(n-1))/(d(n)-b(n)*a(n-1));
x(n)=r(n);
for i=n-1:-1:1
x(i)=r(i)-a(i)*x(i+1);
end
x
end
Output :
x=[-4.0000 3.0000 -3.0000 -4.0000 3.0000 3.0000 5.0000 5.0000]
