0% found this document useful (0 votes)
43 views

Tridiagonal Solver

This function solves a tridiagonal system of equations by inverting the lower, main, and upper diagonals provided in vectors a, b, and c. It takes the right-hand side vector r, size of the system N, and uses forward and back substitution to solve for and return the result vector u. It first checks for a zero on the main diagonal and performs decomposition and forward substitution, then back substitution to solve the system of equations.

Uploaded by

Oluwaseun Godson
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

Tridiagonal Solver

This function solves a tridiagonal system of equations by inverting the lower, main, and upper diagonals provided in vectors a, b, and c. It takes the right-hand side vector r, size of the system N, and uses forward and back substitution to solve for and return the result vector u. It first checks for a zero on the main diagonal and performs decomposition and forward substitution, then back substitution to solve the system of equations.

Uploaded by

Oluwaseun Godson
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

TridiagonalsolverroutineforMATLAB

functionu=tridiag(a,b,c,r,N);
%Functiontridiag:
%Invertsatridiagonalsystemwhoselower,mainandupperdiagonals
%arerespectivelygivenbythevectorsa,bandc.ristherighthand
%side,andNisthesizeofthesystem.Theresultisplacedinu.
%(AdaptedfromNumericalRecipes,Pressetal.1992)
if(b(1)==0)
fprintf(1,'Reordertheequationsforthetridiagonalsolver...')
pause
end
beta=b(1);
u(1)=r(1)/beta;
%Startthedecompositionandforwardsubstitution
forj=2:N
gamma(j)=c(j1)/beta;
beta=b(j)a(j)*gamma(j);
if(beta==0)
fprintf(1,'Thetridiagonalsolverfailed...')
pause
end
u(j)=(r(j)a(j)*u(j1))/beta;
end
%Performthebacksubstitution
forj=1:(N1)
k=Nj;
u(k)=u(k)gamma(k+1)*u(k+1);
end

You might also like