In: Computer Science
l = [0 1 -2 1 1];
d = [2 -2 4 2 2];
r = [-1 1 -1 -2 0];
b = [2 0 -6 1 4];
n = length(d);
x= zeros(n,1);
for i = 2:n
factor = l(i)/d(i-1);
d(i) = d(i) - factor*r(i-1);
b(i) = b(i) - factor*b(i-1);
end
x(n) = b(n)/d(n);
for i = n-1:-1:1
x(i) = b(i)-r(i)*x(i+1) / d(i);
end
x1 = x(1);
x2 = x(2);
x3 = x(3);
x4 = x(4);
fprintf('\nx1 = %f\nx2 = %f\nx3 = %f\nx4 = %f\n',x1,x2,x3,x4);
this is matlab code!
It's a question of getting answers using the Thomas method.
The answer should be x1=1, x2=0, x3=-1, x4=2, x5=1.
Please fix what's wrong.
CODE:-
l = [0 1 -2 1 1];
d = [2 -2 4 2 2];
r = [-1 1 -1 -2 0];
b = [2 0 -6 1 4];
n = length(d);
x= zeros(n,1);
for i = 2:n
factor = l(i)/d(i-1);
d(i) = d(i) - factor*r(i-1);
b(i) = b(i) - factor*b(i-1);
end
x(n) = b(n)/d(n);
for i = n-1:-1:1
x(i) = (b(i)-r(i)*x(i+1)) / d(i);
end
x1 = x(1);
x2 = x(2);
x3 = x(3);
x4 = x(4);
x5 = x(5);
fprintf('\nx1 = %d\nx2 = %d\nx3 = %d\nx4 = %d\nx5 =
%d\n',x1,x2,x3,x4,x5)
screenshots of code and result:-
please like if you find it needfull.....