Page 264 - MATLAB an introduction with applications
P. 264
Numerical Methods ——— 249
x =
1.1556
–0.3111
0.0889
–0.0444
Example E4.25: Solve the system of equations given by [A]{X}={b} using Gauss-Seidel method. The
matrices [A] and {b} are given below:
4
42 00
28 2 0
0
[] = and {b}=
A
02 8 2
0
14
0024
Solution:
The following program and output are presented:
A = [4 2 0 0; 2 8 2 0; 0 2 8 2; 0 0 2 4];b = [4;0;0;14];
X0 = zeros(size(b)); % starting vector
tole = 1e–6;kstop = 30;% error tolerance and max.iterations
[n,n] = size(A);
P = tril(A);% lower triangular form
k = 0;r = b–A*X0;
r0 = norm(r);er = norm(r);
X = X0;
[L,U] = lu(P);
fprintf(‘iter#\tX(1)\t\tX(2)\t\tX(3)\t\tX(4)\n’);
while er>tole & k<kstop
fprintf(‘%d\t%f\t%f\t%f\t%f\n’,k,X(1),X(2),X(3),X(4));
k = k+1;
dx = L\r;
dx = U\dx;
X = X+dx;
r = b–A*X;
er = norm(r)/r0;
erp(k) = norm(r)/r0;
end
X
The output is as follows:
X =
1.0000
– 0.0000
– 1.0000
4.0000