Question for Thomas Method for Numerical method.

조회 수: 7 (최근 30일)
SEUNGMYEONG CHOO
SEUNGMYEONG CHOO 2015년 10월 4일
댓글: Walter Roberson 2015년 10월 5일
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

답변 (1개)

Jan
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
SEUNGMYEONG CHOO
SEUNGMYEONG CHOO 2015년 10월 5일
Can you provide whole codes that you got? I have still error though..
Walter Roberson
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);

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Mathematics에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by