Islamic University of Technology (IUT)
Organization of Islamic Cooperation (OIC)
               Department of Electrical and Electronic Engineering (EEE)
Course No.           : Math 4522
Course Name          : Numerical Methods Lab.
Experiment No.       : 11
Experiment Name      : Trapezoidal method and Simpson’s 1/3rd rule method.
         Objective:
        To get familiarized with the different types of numerical integration technique.
         Theory:
        The Newton-Cotes formulas are the most common numerical integration schemes. They
        are based on the strategy of replacing a complicated function or tabulated data with an
        approximating function that is easy to integrate:
                                                     b            b
                                            I =      ∫ f (x) dx ≅ ∫ f
                                                     a            a
                                                                        n   (x) dx
        Where, f n (x) = a polynomial of the form f n (x) = a 0 + a1 x + ...+ a n −1 x n −1 + a n x n
        where n is the order of the polynomial.
        Closed and open forms of the Newton-Cotes formulas are available. The closed forms are those where
        the data points at the beginning and end of the limits of integration are known (Fig. 11.1(a)). The open
        forms have integration limits that extend beyond the range of the data (Fig. 11.1(b)).
                                                        Fig. 11.1
        The trapezoidal rule is the first of the Newton-Cotes closed integration formulas.
             First of the Newton-Coates formulas; corresponds to 1st order polynomial
                                                 b                b
                                           I =   ∫
                                                 a
                                                     f (x) dx ≅   ∫ f (x) dx
                                                                  a
                                                                        1
      Recall from “INTERPOLATION” that a straight line can be represented:
                                                              f (b) − (a)
                                     f1=
                                       (x)          f (a) +               x
                                                                 b−a
      Area under line is an estimate of the integral b/w the limits          ' a ' and 'b'
                                                             f (b) − f (a) 
                                            b
                                =I         ∫  f (a) +
                                            a
                                                                  b−a
                                                                           x  dx
                                                                             
       One way to improve the accuracy of the trapezoidal rule is to divide the integration
       interval from a to b into a number of segments and apply the method to each segment.
       The areas of individual segments can then be added to yield the integral for the entire
       interval. There are n + 1 equally spaced base points (x0, x1, x2, . . . , xn).
       Consequently, there are n segments of equal width:
                                                               b−a
                                                         h=
                                                                n
       If a and b are designated as x 0 and x n , respectively, the total integral can be represented
       as
                           x1             x2                   x3                       xn
                    I =    ∫ f (x) dx + ∫ f (x) dx + ∫ f (x) dx + ........ + ∫
                           x0             x1                   x2                      xn−1
                                                                                              f (x) dx
       Substituting the trapezoidal rule for each integral yield
                           f (x 0 ) + f(x1 )    f (x1 ) + f(x 2 )               f (x n −1 ) + f(x n )
       =I              h                     +h                   + ....... + h
                                   2                   2                                  2
       After simplification and grouping
    Demonstration
Example 21.1 from the book.
Problem statement
Using 10 iterations numerically integrate
f(x) = 0.2 + 25x - 200x 2 + 675 x 3 − 900 x 4 + 400 x 5 from x = 0 to 0.8. The exact value is
1.640533
  clear all
  clc
  n = 20;                                              % number iterations
  intervalBegin = 0;                                   % lower limit
  intervalEnd = 0.8;                                   % upper limit
  exact_val = 1.640533;                                    % Exact value
  integral = 0.0;
  dx = (intervalEnd-intervalBegin)/n;                  % step size
  y = @(x) 0.2 + 25 * x - 200 * (x^2) + 675 * (x^3) - 900
  *(x^4) + 400 * (x^5);
  integral = integral + (1/2) * y(intervalBegin);
  integral = integral + (1/2) * y(intervalEnd);
  for i = 1:(n-1)
          integral = integral + y( intervalBegin + i*dx); %
  for the rest of data
  end
  result = integral * dx;
  error = (exact_val - result) ./ exact_val;
  fprintf ('Value of h is : %.3f\n',dx);
  fprintf ('The final result is: %.4f \n', result);
  fprintf ('Error: %.2f % \n', (error *100));
Verification
           n              h                   I              E t (%)
           2             0.4                1.0688            34.85
           3            0.267               1.3696            16.52
           4            0.200               1.4848            9.49
           5             0.16               1.5399            6.14
           10            0.08               1.615             1.55
 Task in the lab
  o   Modify the Matlab code in a way that it will prompt the user to provide the upper
      limit, lower limit, number of iterations. Then it will print n, h, I and E r in a table
      starting the value of n from 1, then increasing 1 every time upto max. number of
      iterations inserted by the user.
  o   Now change the code and ask the user to insert the function from the console.
 Task for the lab report
  o   Implement the Simpson’s 1/3rd rule in Matlab.
                        ∆x
       b
       ∫ f (x) dx
       a
                    ≈
                        3
                           (y 0 + 4 y1 + 2 y 2 + 4 y3 + 2 y 4 + ........ + 4 y n −1 + y n )
      Using 4 iterations numerically integrate
      f(x) = 0.2 + 25x - 200x 2 + 675 x3 − 900 x 4 + 400 x 5 from x = 0 to 0.8. The exact
  value is 1.640533 [Example 21.5]