Page 240 - MATLAB an introduction with applications
P. 240

Numerical Methods ———  225

                   Example E4.9: Find the solution to the equations using Gauss-Seidel method.

                                             x
                                 42 00          4
                                           
                                              1
                                            
                                           
                                 28 2 0        
                                                  
                                             x
                                                   0 
                                             =  
                                              2
                                 02 8 2        
                                             x
                                                   0
                                              3
                                          
                                             x
                                  0024         0 
                                             
                                                   
                                              4
                   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;0];
                   >> x = A\B
                   x =
                         1.1556
                        –0.3111
                         0.0889
                        –0.0444
                   >> x=inv(A)*B
                   x =
                         1.1556
                        –0.3111
                         0.0889
                        –0.0444
                   >> A = [4 2 0 0;2 8 2 0;0 2 8 2;0 0 2 4];
                   >> b = [4;0;0;0];
                   >> tol = 1.0e – 9;
                   >> format long;
                   >> xguess = [1– 0.5 0.1 – 0.2];
                   >> [x,iter] = GAUSSD(A,b,xguess,tol)
                   x =
                       1.15555555568491   –0.31111111117579     0.08888888892123   –0.04444444446061
                   iter =
                        16
                   function [Y,iter] = GAUSSD(A,r,yguess,tol)
                   % GAUSSD will iteratively solve Ay = r
                   n = length(r);  Y = yguess;  dy = ones(n,1); iter = 0;
                   while norm(dy)/norm(Y) > tol
                   for i = 1:n
                   if abs(A(i,i))<100*eps;error(‘zero pivot found’);end
                   dy(i) = r(i)/A(i,i);
   235   236   237   238   239   240   241   242   243   244   245