Page 247 - MATLAB an introduction with applications
P. 247
232 ——— MATLAB: An Introduction with Applications
The following MATLAB code is written for this problem
% This will perform Gaussian elmination
% on the matrix that you pass to it.
% i.e., given A and b it can be used to find x,
% Ax = b
%
% A - matrix for the left hand side.
% b - vector for the right hand side
% This performs Gaussian elminiation to find x.
% MATRIX DEFINITION
A = [1 1 1 –1;4 3 1 1;1 –1 –1 2;2 1 2 –2];
b = [2;11;0;2];
% Perform Gaussian Elimination
for j = 2:N,
for i = j:N,
m = A(i,j–1)/A(j–1, j–1);
A(i,:) = A(i,:) – A(j–1,:) m;
*
b(i) = b(i) – m b(j–1);
*
end
end
disp(‘Upper triangular form of given matrix is=’)
disp(A)
disp(‘b =’)
disp(b)
% BACK-SUBSTITUTION
% Perform back substitution
x = zeros(N,1);
x(N) = b(N)/A(N,N);
for j = N–1:–1:1,
x(j) = (b(j)–A(j,j+1:N) x(j+1:N))/A(j,j);
*
end
disp(‘final solution is’);
disp(x);
Output appears like this:
N
= 4