14.2. USING MATLAB TO ITERATE 335

14.2 Using MATLAB To IterateThe syntax you can use to accomplish this iteration is as follows. This is for the purposesof illustration. In fact, you would not take an inverse of one of the matrices in practice.

D=[3 0 0 0;0 4 0 0;0 0 5 0;0 0 0 4];O=[0 1 0 0;1 0 1 0;0 2 0 1;0 0 2 0];d=1; x=[0;0;0;0]; b=[1;2;3;4]; k=1; F=inv(D);while d >.0000001 & k <1000y=-F*O*x+F*b; d=(y-x)’*(y-x); k=k+1;x=y;endxk(((D+O)*x-b)’*((D+O)*x-b))ˆ(1/2)

It is going to iterate till |y− x|2 is smaller than 10−7 or 1000 iterations, whichever comesfirst. Of course y = xr+1 and x = xr. The next to last line which has k tells you how manyiterations it took to get there and the bottom line tells you how close x is to solving theequation. This yields

x =

.2069.3793.2759.8621

 , k = 14, 6.1753×10−4

14.2.1 The Gauss Seidel Method

The Gauss Seidel method differs from the Jacobi method in using xk+1j for all j < i in going

from xk to xk+1. This is why it is called the method of successive corrections. The precisedescription of this method is in the following definition.

Definition 14.2.1 The Gauss Seidel method, also called the method of successive correc-tions is given as follows. For A = (ai j) , the iterates for the problem Ax = b are obtainedaccording to the formula

i

∑j=1

ai jxr+1j =−

n

∑j=i+1

ai jxrj +bi. (14.4)

In terms of matrices, letting

A =

a11 · · · a1n

.... . .

...an1 · · · ann

