Page 241 - MATLAB an introduction with applications
P. 241
226 ——— MATLAB: An Introduction with Applications
for j = 1:n
dy(i) = dy(i) – A(i,j)*Y(j)/A(i,i);
end
Y(i) = Y(i) + dy(i);
end
iter = iter + 1;
if iter>1000; error(‘not converging in 1000 steps’);end
end
Example E4.10: Solve the system of equations given by [A]{x} = {b} using Gauss-Seidel method. The
matrices [A] and {b} are given below.
42 00 4
28 2 0
0
[] = , { } =
b
A
02 8 2
0
14
0024
Solution:
MATLAB Solution [Using built-in function]:
>> A = [4 2 0 0;2 8 2 0;0 2 8 2;0 0 2 4];
>> b = [4;0;0;14];
>> x = A\b
x =
1.0000
0
–1.0000
4.0000
>> x = inv(A)*b
x =
1.0000
–0.0000
–1.0000
4.0000
>> A= [4 2 0 0;2 8 2 0;0 2 8 2;0 0 2 4];
>> b = [4;0;0;14];
>> xguess = [1 –0.5 0.1 –0.2];
>> tol = 1.0e–9;
>> format long;
>> [x,iter] = GAUSSD(A,b,xguess,tol)
x =
1.00000000075670 –0.00000000037835 –0.99999999981083 3.99999999990541
iter =
17