Use the function program tridiagm to solve the tridiagonal s

Use the function program tridiag.m to solve the tridiagonal system x_1 + 2x_2 = 4 -x_1 + 2x_2 + x_3 = 3 2x_2 - x_3 + 2x_4 = -3 3x_3 + x_4 + x_5 = 10 3x_4 + 4x_5 + 2x_6 = 7 -2x_5 + 2x_6 = -2 Submit only your main program and the output of the solution.

Solution

function x = Tridiag(e,f,g,r)
% Tridiag: Tridiagonal equation solver banded system
% x = Tridiag(e,f,g,r): Tridiagonal system solver.
% input:

% Solve the n x n tridiagonal system for x:
%
% [ f(1) g(1) ] [ x(1) ] [ r(1) ]
% [ e(2) f(2) g(2) ] [ x(2) ] [ r(2) ]
% [ e(3) f(3) g(3) ] [ ] [ ]
% [ ... ... ... ] [ ... ] = [ ... ]
% [ ... ... ... ] [ ] [ ]
% [ e(n-1) f(n-1) g(n-1) ] [ x(n-1) ] [ r(n-1) ]
% [ e(n) f(n) ] [ x(n) ] [ r(n) ]
%
% e = subdiagonal vector
% f = diagonal vector
% g = superdiagonal vector
% r = right hand side vector
% output:
% x = solution vector
n=length(f);
% forward elimination
for k = 2:n
factor = e(k)/f(k-1);
f(k) = f(k) - factor*g(k-1);
r(k) = r(k) - factor*r(k-1);
end
% back substitution
x(n) = r(n)/f(n);
for k =n-1:-1:1
x(k) = (r(k)-g(k)*x(k+1))/f(k);
end

OUTPUT:

>> e = [0 -1 2 3 3 -2];

>> f = [1 2 -1 1 4 2];
>> g = [1 2 1 2 2 0];
>> r = [4 3 -3 10 7 -2];
>> Tridiag(e,f,g,r)

ans =

Columns 1 through 5

3.2222 0.7778 2.3333 -2.2222 2.6111

Column 6

1.6111

 Use the function program tridiag.m to solve the tridiagonal system x_1 + 2x_2 = 4 -x_1 + 2x_2 + x_3 = 3 2x_2 - x_3 + 2x_4 = -3 3x_3 + x_4 + x_5 = 10 3x_4 + 4x_

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site