CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > General Forums > Main CFD Forum

TDMA code for MATLAB

Register Blogs Community New Posts Updated Threads Search

Like Tree2Likes
  • 2 Post By amin144

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   February 15, 2012, 14:34
Smile TDMA code for MATLAB
  #1
Member
 
Amin Shariat KHah
Join Date: Apr 2011
Location: Shiraz
Posts: 86
Rep Power: 15
amin144 is on a distinguished road
TDMA is a quick Aligorithm for solving AX=b when A is Tridiagonal matrix:

Code:
% Written by Amin ShariatKhah 2012 - Shahrood University Of technology
%Feel Free to use it
%This code solve the AX=b (When A is Tridiagonal )

function X=TDMAsolver(A,b)

m=length(b);                 % m is the number of rows
X=zeros(m,1);
A(1,2)= A(1,2)  ./ A(1,1);    % Division by zero risk.
b(1)=  b(1)    ./ A(1,1);    % Division by zero would imply a singular matrix

for i=2:m-1
    temp=  A(i,i) - A(i,i-1) .* A(i-1,i);
    A(i,i+1)=  A(i,i+1)  ./ temp;
    b(i)= ( b(i) - A(i,i-1) .* b(i-1) )  ./ temp;
end 

i=m;
X(m)=(b(i) - A(i,i-1) .* b(i-1))  ./ (A(i,i) - A(i,i-1) .* A(i-1,i));

for i=m-1:-1:1
X(i)=  -A(i,i+1) .* X(i+1) + b(i);
end
end

there is another version of this code in wikipedia:

Code:
function x = TDMAsolver(a,b,c,d)
%a, b, c are the column vectors for the compressed tridiagonal matrix, d is the right vector
n = length(b); % n is the number of rows
 
% Modify the first-row coefficients
c(1) = c(1) / b(1);    % Division by zero risk.
d(1) = d(1) / b(1);    % Division by zero would imply a singular matrix.
 
for i = 2:n-1
    temp = b(i) - a(i) * c(i-1);
    c(i) = c(i) / temp;
    d(i) = (d(i) - a(i) * d(i-1)) / temp;
end
 
d(n) = (d(n) - a(n) * d(n-1))/( b(n) - a(n) * c(n-1));
 
% Now back substitute.
x(n) = d(n);
for i = n-1:-1:1
    x(i) = d(i) - c(i) * x(i + 1);
end
end

you can find some explanation about TDMA and find this code in C and Fortran90 in :
HTML Code:
http://en.wikipedia.org/wiki/Tridiagonal_matrix_algorithm
hityangsir and CZdozen like this.
amin144 is offline   Reply With Quote

Old   March 27, 2012, 12:33
Talking thank you very much,that is what I need
  #2
New Member
 
hityangsir's Avatar
 
Y. Yang
Join Date: Mar 2010
Location: Miami, United States
Posts: 28
Rep Power: 16
hityangsir is on a distinguished road
thank you very much,that is what I need
hityangsir is offline   Reply With Quote

Old   April 6, 2012, 16:27
Default
  #3
Member
 
Amin Shariat KHah
Join Date: Apr 2011
Location: Shiraz
Posts: 86
Rep Power: 15
amin144 is on a distinguished road
You're welcome
amin144 is offline   Reply With Quote

Old   March 2, 2016, 09:04
Default 2D line by line iterative TDMA
  #4
New Member
 
farah souayfane
Join Date: Jan 2016
Posts: 3
Rep Power: 10
fsouayfane is on a distinguished road
hello,

I am working on modelling phase change including the effect of convection in liquid phase. I want to write my program on MATLAB.

Actually I am a beginner in MATLAB.
I need help to write the 2D line by line TDMA iterative solution of my equations ( 2D transient)
Can any one provide me with a code to 2D TDMA line by line iterative algorithm for the solution of 2D discretized equations.

Thank you in advance
fsouayfane is offline   Reply With Quote

Old   March 2, 2016, 11:12
Default
  #5
New Member
 
Join Date: Mar 2014
Posts: 14
Rep Power: 12
j_02 is on a distinguished road
I cannot provide a Matlab code, but I can provide some advice.

If your stencil looks like this
,

you can treat the W and E values explicitly (use the values from the previous time-/iteration step). If you are solving Ax = b, include the W and E values in your b solution vector and fill your A matrix with the coefficients of unknowns along a North-South line.

You can step W to E in the domain along N-S lines.
j_02 is offline   Reply With Quote

Old   March 4, 2016, 03:02
Default 2D line by line iterative TDMA
  #6
New Member
 
farah souayfane
Join Date: Jan 2016
Posts: 3
Rep Power: 10
fsouayfane is on a distinguished road
Thank you for your reply,

Actually I have a problem in how to define the temperature field in Matlab code,
is it a vector or a matrix??

for example in my 2D grid I have T(1,1) T(1,2) .....T(n,m)

When I use A line by line TDMA the pentadiagonal matrix became tridiagonal.

I am sweeping from west to east along N-S lines so for a determined line ( for a specified n ) I have to calculate all the values of temperature for m going from 2 to m ( for example if n=2 (x direction ) so I calculate T(2,2) T(2,3) T(2,4) T(2,5).....(T2,m) and then I sweep to n=3 ....) . here I don't understand how can I define the temperature field in Matlab ??

Last edited by fsouayfane; March 4, 2016 at 03:05. Reason: add title
fsouayfane is offline   Reply With Quote

Old   March 4, 2016, 10:42
Default
  #7
New Member
 
Join Date: Mar 2014
Posts: 14
Rep Power: 12
j_02 is on a distinguished road
Quote:
Originally Posted by fsouayfane View Post
Actually I have a problem in how to define the temperature field in Matlab code,
is it a vector or a matrix??

for example in my 2D grid I have T(1,1) T(1,2) .....T(n,m)
Using an n\times m matrix will work just fine.

Quote:
When I use A line by line TDMA the pentadiagonal matrix became tridiagonal.
Forgive me, I do not understand... do you mean because you treat two of the coefficients in an explicit manner?

Quote:

I am sweeping from west to east along N-S lines so for a determined line ( for a specified n ) I have to calculate all the values of temperature for m going from 2 to m ( for example if n=2 (x direction ) so I calculate T(2,2) T(2,3) T(2,4) T(2,5).....(T2,m) and then I sweep to n=3 ....) . here I don't understand how can I define the temperature field in Matlab ??
This procedure sounds great. I am not sure what you mean by being unable to define the temperature field in matlab... when I implemented a similar code in FORTRAN, I used two data structures, something like: \texttt{temperature} and \texttt{temperature\_prev}, where I start by filling \texttt{temperature\_prev} with an initial guess for the field, and then store the solved temperature distribution in \texttt{temperature}, and then overwrite \texttt{temperature\_prev} with the solved temperature before the next iteration.

Does that make any sense? Does that help?
j_02 is offline   Reply With Quote

Old   February 22, 2022, 01:18
Default
  #8
Senior Member
 
Lucky
Join Date: Apr 2011
Location: Orlando, FL USA
Posts: 5,762
Rep Power: 66
LuckyTran has a spectacular aura aboutLuckyTran has a spectacular aura aboutLuckyTran has a spectacular aura about
The wikipedia article tells you exactly what you need to do and even provides a full analytical derivation. If there's a specific step you don't understand, ask it. If there's a specific non-English language you understand better, ask for a translation.
LuckyTran is offline   Reply With Quote

Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
The FOAM Documentation Project - SHUT-DOWN holger_marschall OpenFOAM 242 March 7, 2013 13:30
How to make code run in parallel? cwang5 OpenFOAM Programming & Development 1 May 30, 2011 05:47
Open Source Vs Commercial Software MechE OpenFOAM 28 May 16, 2011 12:02
Error in CFX Solver Leuchte CFX 5 November 6, 2010 07:12
Small 3-D code Zdravko Stojanovic Main CFD Forum 2 July 19, 2010 11:11


All times are GMT -4. The time now is 04:49.