How can I execute my Thomas Algorithm function?

I have created a function to execute the thomas algorithm. I'm trying to use my function to solve a system with the following arrays:
b = -4ε + 2αh^2
a = 2ε - h(1+α(n+1)h)
c = 2ε + h(1+αnh)
g = 4kπh^2sin(kπnh)
where α=1.2, k=2, ε=0.02, R=4
I've inserted my function (below), but I'm not completely sure how to enter in these parameters in the command window as I'm pretty new to Matlab. Any help would be much appreciated.
function y = ThomasAlgorithm(a,b,c,f)
% obtain values
m = length(f);
f(1) = f(1)/b(1);
% Forward Substitution
for j = 1:m-1
c(j) = c(j)/b(j);
b(j+1) = b(j+1) - a(j)*c(j);
f(j+1) = (f(j+1) - a(j)*f(j))/b(j+1);
end;
% Backwards Substitution
for k = m-1:-1:1
f(k) = f(k) - c(k)*f(k+1);
end;
% Output
y = f;
end
I tried to put this into the command window but didn't have any luck. I'm not really sure where I'm going wrong at the moment.
>> m=10;
x0=0, xm=1;
y0=R, ym=0;
alpha=1.2;
k=2;
eps=0.02;
R=4;
h=xm-x0/m;
a=[2*eps-h*(1+alpha*((1:m-1)+1)*h)];
b=[-4*eps+2*alpha*h*h];
c=[2*eps+h*(1+(alpha*(1:m-1)*h))];
f=[4*k*pi*h*h*sin(k*pi*(1:m-1)*h)];
x=ThomasAlgorithm(a,b,c,f);
for ic=1:n
disp(x);
end

10 comentarios

What does 'didn't have any luck' mean? Unexpected output? Error?
for ic=1:n
disp(x);
end
is just going to do the same thing n times as nothing changes at each iteration of the loop.
I've been getting a few errors, but at the moment I'm getting:
Index exceeds matrix dimensions.
Error in ThomasAlgorithm (line 11)
b(j+1) = b(j+1) - a(j)*c(j);
Torsten
Torsten el 18 de Ag. de 2017
b must be an array of length m, but it is a single number in your case.
Best wishes
Torsten.
says
says el 18 de Ag. de 2017
I'm not really sure if you're referring to b=[-4*eps+2*alpha*h*h] or line 11: b(j+1) = b(j+1) - a(j)*c(j) or how I would change it from a single number to an array of length m.
Torsten
Torsten el 18 de Ag. de 2017
I refer to both. In "b=[-4*eps+2*alpha*h*h]" you define b to be a scalar while in "b(j+1) = b(j+1) - a(j)*c(j)" you use it as an array. This is impossible.
Best wishes
Torsten.
says
says el 18 de Ag. de 2017
I'm not sure how to make b=[-4*eps+2*alpha*h*h] into an array. I'm not sure why I'm getting the error for b but not for a
Torsten
Torsten el 18 de Ag. de 2017
Editada: Torsten el 18 de Ag. de 2017
You don't get the error for a and c because they are arrays of length (m-1).
You will also get an error for the array f since it is of length (m-1), but you try to access f(m) in the "Backwards Substitution" part.
Best wishes
Torsten.
I tested out my function and got the correct result. I don't really understand the error I'm supposed to get for the f array...
>> m=6;
a=[4 3 1 2 3];
b=[7 6 9 5 4 7];
c=[3 1 5 2 1];
f=[13 19 68 46 37 61];
x=ThomasAlgorithm(a,b,c,f);
for ic=1:n
disp(x);
end
1.0000 2.0000 3.0000 7.0000 4.0000 7.0000
Image Analyst
Image Analyst el 18 de Ag. de 2017
Do it all from a script file instead of the command window.
Torsten
Torsten el 18 de Ag. de 2017
Editada: Torsten el 18 de Ag. de 2017
If you prescribe m=6 elements for f as you do above, everything is fine.
In your original code, you only defined f to be a vector of (m-1) (thus 5) elements, but in the "Backwards Substitution" loop, you tried to access f(m) (thus f(6)). This would have lead to an error.
Best wishes
Torsten.

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 18 de Ag. de 2017

Editada:

el 18 de Ag. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by