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
Adam
2017년 8월 18일
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.
says
2017년 8월 18일
Torsten
2017년 8월 18일
b must be an array of length m, but it is a single number in your case.
Best wishes
Torsten.
says
2017년 8월 18일
Torsten
2017년 8월 18일
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
2017년 8월 18일
says
2017년 8월 18일
Image Analyst
2017년 8월 18일
Do it all from a script file instead of the command window.
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.
답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!