Page 249 - MATLAB an introduction with applications
P. 249

234 ———  MATLAB: An Introduction with Applications


                   [L,U] = lu(A);
                   % solution of y
                   y = L\b;
                   %final solution x
                   x = U\y;
                   fprintf(‘Solution of the equations is\n’);
                   disp(x)
                   Output is as follows:
                   Solution of the equations is
                         1.0000
                         2.0000
                        –1.0000
                         0.0000

                   Check with MATLAB built-in function:

                   >> A=[1 1 1 –1;1 –1 –1 2;4 4 1 1;2 1 2 –2];
                   b = [2;0;11;2];
                   >> x = A\b
                   x =
                         1.0000
                         2.0000
                        –1.0000
                          0.0000

                   Example E4.15:  Using the Gauss-Seidel method, solve the system of equations given below:
                            x + 2y + z = 0
                             3x + y –z = 0
                             x – y + 4z =3
                   Solution:
                   The Gauss-Seidel method is a technique used to solve a linear system of equations. In solving equations
                   AX = b, first the matrix A is written as: A = D + L + U where the matrices D, L, and U represent the diagonal,
                   negative strictly lower triangular, and negative strictly upper triangular parts of the coefficient matrix A.
                   Then the solution is given for every iteration counter k as:

                                               (k)
                                         –1
                            X (k + 1)  = (D + L) (–U X + b)     Gauss-Seidel Method
                                    –1
                                               (k)
                            X (k + 1)  = D (–(L + U) X + b)     Jacobi Method
                   Disadvantages:
                   1.  The matrix (D + L) is not always invertible. One must check that the values on the diagonal are non-
                       zero before starting the iteration because it can lead to unpredictable results.
   244   245   246   247   248   249   250   251   252   253   254