How can I execute my Thomas Algorithm function using Matlab? -


i have created function execute thomas algorithm. i'm trying use function solve system 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 function (below), i'm not sure how enter in these parameters in command window i'm pretty new matlab. appreciated.

function y = thomasalgorithm(a,b,c,f)    % obtain values    m = length(f);      f(1) = f(1)/b(1);      % forward substitution      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      k = m-1:-1:1          f(k) = f(k) - c(k)*f(k+1);      end;      % output      y = f;   end 

i tried put command window (below) got error:

error in thomasalgorithm (line 11)        b(j+1) = b(j+1) - a(j)*c(j); 

i'm not sure i'm going wrong @ moment or how solve , i've kind of hit wall.

>> 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); ic=1:n disp(x); end 

put of stuff you've put in command window separate script (.m file) instead, run it. allows actual full error message, , keeps command window clutter free!

when running script code in, see following error:

undefined function or variable 'r'.
error in myscript (line 3)
y0=r, ym=0;

now (first) problem clear! set y0=r when r doesn't exist. it's practise @ times run clear before run script, workspace emptied , know you've not defined in script.

so add r = 1 or start, run again. now have indexing error!

index exceeds matrix dimensions.
error in thomasalgorithm (line 8)
b(j+1) = b(j+1) - a(j)*c(j);

this because defined b

b=[-4*eps+2*alpha*h*h]; % scalar not vector! 

then passed thomasalgorithm , expected able index it, when isn't vector.


hopefully points out immediate problem, , how better diagnose issues. when code in script can step through it debug things.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -