Please help with providing a correct code for MATLAB (or Octave)on a tri-diagona
ID: 3883592 • Letter: P
Question
Please help with providing a correct code for MATLAB (or Octave)on a tri-diagonal Gaussian elimination .
I have tried several different codes, but no outcome. If you can , please provide the code and run it. thank you a lot .
Write your own code for tri-diagonal Gaussian elimination. Your input should be a random strictly diagonally dominant matrix of size n., and a random right hand side vector. Test your run times for n = 10 * k, k = 1, , 15. Compare them with the rune times of the standard solver in Matlab. Submit a table with run times comparison and your code.Explanation / Answer
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(d); % 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);
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