Page 259 - MATLAB an introduction with applications
P. 259

244 ———  MATLAB: An Introduction with Applications


                           b(i) = b(i) – m*b(j–1);
                        end
                    end
                   disp(‘Upper triangular form of given matrix is =’)
                   disp(A);
                   disp(‘b = ’);
                   disp(b);
                   % BACK-SUBSTITUTION
                   % Perform back substitution
                    x = zeros(N,1);
                    x(N) = b(N)/A(N,N);
                    for j = N–1:–1:1,
                      x(j) = (b(j)–A(j,j+1:N) x(j+1:N))/A(j,j);
                                                *
                    end
                   disp(‘final solution is’);disp(x);
                   Output is as follows:
                   The final solution is
                         3
                        –1
                        –2
                   Check with MATLAB built-in function:
                   >> A = [2 1 –3;4 –2 3;–2 2 –1];
                   b = [11;8;–6];
                   >> x = A\b
                   x =
                         3
                        –1
                        –2
                   Example E4.21: Solve the system of linear equations by Gaussian elimination method:
                            6x + 3x + 6x = 30
                                       3
                                  2
                             1
                            2x + 3x + 3x = 17
                             1
                                  2
                                       3
                              x + 2x + 2x = 11
                             1
                                  2
                                       3
                   Solution:
                   Writing the equation in the form of [A]X =B and apply forward elimination and back-substitution
                   The complete MATLAB program and output are given below:
                   %   A – matrix for the left hand side.
                   %   b – vector for the right hand side.
                   % This performs Gaussian elminiation to find x.
                   % MATRIX DEFINITION
   254   255   256   257   258   259   260   261   262   263   264