|
[Sponsors] |
February 15, 2012, 13:34 |
TDMA code for MATLAB
|
#1 |
Member
Amin Shariat KHah
Join Date: Apr 2011
Location: Shiraz
Posts: 86
Rep Power: 15 |
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 |
|
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
The FOAM Documentation Project - SHUT-DOWN | holger_marschall | OpenFOAM | 242 | March 7, 2013 12:30 |
How to make code run in parallel? | cwang5 | OpenFOAM Programming & Development | 1 | May 30, 2011 04:47 |
Open Source Vs Commercial Software | MechE | OpenFOAM | 28 | May 16, 2011 11:02 |
Error in CFX Solver | Leuchte | CFX | 5 | November 6, 2010 06:12 |
Small 3-D code | Zdravko Stojanovic | Main CFD Forum | 2 | July 19, 2010 10:11 |