Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Contents

Function ThomasAlg
Inputs:
Outputs:
VarArgIn Logic
Forward Elimination
Back Substitution

function x1 = ThomasAlg(AA,BB,CC,DD,varargin)

Function ThomasAlg

This function solves CFD problems using the Thomas Algorithm. The function solves
tridiagonal equations in the form of:
A(count)*x(count-1)+B(count)*x(count)+C(count)*x(count+1)=D(count)

Inputs:

A = Coefficient matrix for the first term B = Coefficient matrix of the second term C =
Coefficient matrix of the third term D = RHS matrix NN = If given, vector length
Outputs:

x = Solution matrix
VarArgIn Logic

If only 4 inputs are given, calculate NN

if nargin == 4
NN = length(BB);
% If 5 are given, assign the variable to NN
elseif nargin == 5
NN = varargin{1};
% If neither is true, terminate the function
else
fprintf( '\n')
fprintf( '%s\n','Sorry, you provided an incorrect number of inputs.'
fprintf( '%s\n','This function takes 4 or 5 inputs: matricies A, B, C, and D'
fprintf( '%s\n','and an optional variable NN.' )
fprintf( '%s\n','Please try again.' )
fprintf( '\n')
error( 'Program terminated' )
end

Sorry, you provided an incorrect number of inputs.


This function takes 4 or 5 inputs: matricies A, B, C, and D
and an optional variable NN.
Please try again.

Error using ==> ThomasAlg at 33


Program terminated

Forward Elimination

1) Create the output matrix to speed the processing up

x1 = zeros(NN,1);

% 2) Start forward elimination loop


for inda1 = 2:NN
coeff = BB(inda1-1)/AA(inda1-1);
AA(inda1) = AA(inda1)-coeff*CC(inda1-1);
DD(inda1) = DD(inda1)-coeff*DD(inda1-1);
end

Back Substitution

1) Solve the last entry in the vector

x1(NN) = DD(NN)/AA(NN);

% 2) Start back substitution loop


for indb1 = 1:(NN-1)
count1 = NN-indb1;
x1(count1)=(DD(count1)-CC(count1)*x1(count1+1))/AA(count1);
end

Published with MATLAB® 7.10

You might also like