Using Matlab create a tridagonal matrix algorithm using the template below funct
ID: 3793015 • Letter: U
Question
Using Matlab create a tridagonal matrix algorithm using the template below
function x = Tdma(A,b)
% This function solves a tridiagonal system of linear equations [A][x]=[b]
% using the tri-diagonal matrix algorithm.
% Input:
% A = matrix of coefficients
% b = right hand side column vector
% Output:
% x = column vector for solution
[nR,nC]=size(A);
% create vector d containing all elements in the diagonal
% create vector c containing all elements above the diagonal
% create vector a containing all elements below the diagonal
% calculate c1' and b1'
% calculate c2' to cn-1' and b2' to bn-1'
% calculate bn'
% calculate xn
% calculate x from n-1 to 1
% transpose x into a column vector
x=x'
end
Explanation / Answer
%Using Matlab create a tridagonal matrix algorithm using the template below
function x = Tdma(A,d)
% This function solves a tridiagonal system of linear equations [A][x]=[d]
% using the tri-diagonal matrix algorithm.
% Input:
% A = matrix of coefficients
% d = right hand side column vector
% Output:
% x = column vector for solution
[nR,nC]=size(A);
% create vector d containing all elements in the diagonal
for i=1:nR
b(i)=A(i,i);
end
% create vector c containing all elements above the diagonal
for i=1:nR-1
c(i)=A(i,i+1);
end
c(nR)=0;
% create vector a containing all elements below the diagonal
a(1)=0;
for i=1:nR-1
a(i+1)=A(i+1,i);
end
% calculate c1' and b1'
c1(1)=c(1)/b(1);
d1(1)=d(1)/b(1);
% calculate c2' to cn-1' and b2' to bn-1'
for i=2:nR-1
c1(i)=c(i)/(b(i)-(a(i)*c1(i-1)));
end
for i=2:nR
d1(i)=(d(i)-(a(i)*d1(i-1)))/(b(i)-(a(i)*c1(i-1))) ;
end
% calculate xn
xn(nR)=d1(nR);
% calculate x from n-1 to 1
for i=nR-1:1
xn(i)=d1(i)-(c1(i)*xn(i+1));
end
% transpose x into a column vector
x=xn';
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.