Question for Thomas Method for Numerical method.

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

Réponses (1)

Jan
Jan le 4 Oct 2015

0 votes

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 commentaires

Can you provide whole codes that you got? I have still error though..

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

Connectez-vous pour commenter.

Catégories

En savoir plus sur Mathematics dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by