Question for Thomas Method for Numerical method.
조회 수: 7 (최근 30일)
이전 댓글 표시
I got the code for Thomas Method, but after run the program, the output is like this:
>> d = [2 4 3 5];
a = [2 4 3 0];
b = [0 2 1 2];
r = [4 6 7 10];
x = Thomas(a, d, b, r);
r =
2 6 7 10
**Can anybody help with me?
Here is the Thomas Method code what i got.**
function x = Thomas(a, d, b, r)
%solve A x = b, where A is a tridiagonal matrix
% a upper diagonal of matrix A, a(n) = 0
% d diagonal of matrix A
% b lower diagonal of matrix A, b(1) = 0
% r right-hand side of equation
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
댓글 수: 0
답변 (1개)
Jan
2015년 10월 4일
What is your problem? All I'd do is to append a semicolon after "r(1) = r(1)/d(1)", such that the value of r is not written to the command line anymore. Afterwards the result is stored in the variable x.
댓글 수: 2
Walter Roberson
2015년 10월 5일
Change the line
a(1) = a(1)/d(1); r(1) = r(1)/d(1)
to
a(1) = a(1)/d(1); r(1) = r(1)/d(1);
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!