Implement the Gauss-Thomas Method using matlab? a,d,b,r are provided below and t
ID: 3028552 • Letter: I
Question
Implement the Gauss-Thomas Method using matlab? a,d,b,r are provided below and the gauss thomas method is provided
the upper diagonal a which is [1 1 3 -2 -2 -2 0],
diagonal d which is [-1 4 1 -1 -2 -2 4 2],
lower diagonal b which is [-1 4 0 -2 -4 2 0]
and r which is [7 13 -3 -2 -4 -28 26 10]
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
end
Can someone help me with a workable code for matlab, I am just not getting this question. The output should be x=[-4.0000 3.0000 -3.0000 -4.0000 3.0000 3.0000 5.0000 5.0000] I believe.
Explanation / Answer
a=[0 -1 4 0 -2 -4 2 0 ];
b=[-1 4 1 -1 -2 -2 4 2];
c=[1 1 3 -2 -2 -2 0 0];
f=[7 13 -3 -2 -4 -28 26 10];
for i =2:n
end
n=length(f);
b1(1)=b(1);
f1(1)=f(1);
for i=1:n
c1(i)=c(i);
end
for i=2:n
b1(i)=b(i)-(c1(i-1)*(a(i)/b1(i-1)));
end
for i=2:n
f1(i)=f(i)-(f1(i-1)*(a(i)/b1(i-1)));
end
u(n)=f1(n)/b1(n);
for i=n-1:-1:1
u(i)=(f1(i)-c1(i)*u(i+1))/b1(i);
end
u
Answer
u =
Columns 1 through 5
-4.0000 3.0000 -3.0000 -4.0000 3.0000
Columns 6 through 8
3.0000 5.0000 5.0000
Note
here a is lower , b= main diagonal , c= upper diagonal,, f=r, i taken a(1)=0, c(8)=0. then it will work my code