# Tridiagonal Systems in MATLAB | MATLAB Tutorial

### Tridiagonal Systems in MATLAB

Certain matrices have a particular structure that can be exploited to develop efficient solution schemes. For example, a banded matrix is a square matrix that has all elements equal to zero, with the exception of a band centered on the main diagonal.

A tridiagonal system has a bandwidth of 3 and can be expressed generally as

Problem 1: Solve the following tridiagonal system:

As with Gauss elimination, the first step involves transforming the matrix to upper triangular form. This is done by multiplying the first equation by the factor e2/f1 and subtracting the result from the second equation. This creates a zero in place of e2 and transforms the other coefficients to new values,

Notice that g2 is unmodified because the element above it in the first row is zero. After performing a similar calculation for the third and fourth rows, the system is transformed to the upper triangular form

Now back substitution can be applied to generate the final solution:

Matlab m-file:

```
function x = Tridiag(e,f,g,r)
% input:
% 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
for i = 1:length(x) % Disp. xn
fprintf('\nx%d = %f\n', i, x(i));
end
```

##### Gunjan Gupta

I am a Certified MATLAB Associate. I love MATLAB Coding & Enjoy Programming. I can make anything & everything possible with MATLAB.

### Recommended Posts

##### Counters using conditionally executed subsystems | Simulink Tutorial

16 Feb 2018 - Simulink, Tutorial

##### Regulating drum boiler pressure

16 Feb 2018 - Simulink, Tutorial