echo off;
% NUMERICAL METHODS: MATLAB Programs, (c) John H. Mathews 1994
% To accompany the text:
% NUMERICAL METHODS for Mathematics, Science and Engineering, 2nd Ed, 1992
% Prentice Hall, Englewood Cliffs, New Jersey, 07632, U.S.A.
% This free software is complements of the author.

% Algorithm 3.t (Tri-Diagonal System).
% Section 3.4, Gaussian Elimination and Pivoting, Page 148
% See exercise 15 on page 159.
echo on; clc; format long; clear

% This program solves a tri-diagonal linear system AX = B.

% The diagonal is an n dimensional vector D

% The sub diagonal is an n-1 dimensional vector A

% The super diagonal is an n-1 dimensional vector C

% The right side is an n dimensional vector B

% Remark. trisys.m is used for Algorithm 3.t

pause % Press any key to continue.

% Solve the system AX = B where:

n = 12;

D = 4*ones(1,n);

A = 1*ones(1,n-1);

C = 1*ones(1,n-1);

B = 3*ones(n,1);

X = trisys(A,D,C,B);

pause % Press any key to continue.

Mx1 = 'Solution of a tri-diagonal system.';
Mx2 = 'The matrix is A is displayed as [C;D;A]';
Mx3 = 'The vector B is displayed as B` =';
Mx4 = 'The solution to AX = B is displayed as X` =';
clc,echo off,diary output,
disp(''),disp(Mx1),disp(Mx2),...
for i=1:12:n-1, disp(C([i:min(i+11,n-1)])); end,...
for i=1:12:n, disp(D([i:min(i+11,n)])); end,...
for i=1:12:n-1, disp(A([i:min(i+11,n-1)])); end,...
disp(Mx3),for i=1:12:n, disp(B([i:min(i+11,n)])'); end,...
disp(Mx4),disp(X'),diary off, echo on