Question for Thomas Method for Numerical method.

6 Ansichten (letzte 30 Tage)
SEUNGMYEONG CHOO
SEUNGMYEONG CHOO am 4 Okt. 2015
Kommentiert: Walter Roberson am 5 Okt. 2015
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

Antworten (1)

Jan
Jan am 4 Okt. 2015
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 Kommentare
SEUNGMYEONG CHOO
SEUNGMYEONG CHOO am 5 Okt. 2015
Can you provide whole codes that you got? I have still error though..
Walter Roberson
Walter Roberson am 5 Okt. 2015

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);

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Mathematics finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by