Named after mathematician Thomas Simpson, Simpson’s rule or method is a popular technique of numerical analysis for numerical integration of definite integrals. It forms the even number of intervals and fits the parabola in each pair of interval. The method also corresponds to three point Newton – Cotes Quadrature rule.

In earlier tutorials, we’ve already discussed a C program for Simpson’s 1/3 rule. Here, we are going to write a program for **Simpson 1/3 rule in MATLAB**, and go through its mathematical derivation and numerical example.

## Derivation of Simpson 1/3 Rule:

Consider a polynomial equation f(x) = 0 which is to be numerically integrated as shown in the figure below:

In the figure, f(x) implies the actual existing curve and P(x) is parabolic approximation of between two intervals.

Assume, Initial value of abscissa, x_{0} = a and Final value of abscissa, x_{n} = b

Let m be a value of abscissa such that, m = ( a + b ) / 2

By using Lagrange Polynomial Interpolation, the expression for P(x) can be given as:

P(x) = f(a) [ (x-m) ( x-b ) ]/ [ (a-m) ( a-b)] + f(m) [(x-a) (x-b) ]/ [ (m – a) ( m – b) ]

+ f(b) [ (x – a) ( x – m ) ] / [ ( b – a ) ( b – m )]

Substitution of value of ‘m’ and integrating in the interval [a, b],

which is the required expression. The code of Simpson Rule in Matlab is based on this formula.

## Simpson 1/3 Rule in MATLAB Code:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
function I = simpson3_f ( f ) f = @(x) 2 + cos (2*sqrt(2)) % for calculating integrals using Simpson's 1/3 rule when function is known %asking for the range and desired accuracy R= input( 'Enter the limits of integrations [ x_min, x_max] :\n'); tol = input('Error allowed in the final answer: \n'); a= R(1,1); b = R(1,2); %intial h and n n = 100; h = (b -a )/100; %for calculating maximum of f''''(x) in the given region for k = 0:100 x( 1, k+1 ) = a + k*h ; y4( 1, k+1) = feval ( f, x(1,k+1) + 4*h ) - 4*feval( f, x(1,k+1) + 3*h )... + 6*feval( f, x(k+1) + 2*h ) - 4* feval ( f, x(1,k+1) + h ) ... + feval( f, x(k+1) );% fourth order difference end [ y i ] = max( y4); x_opt = x(1,i); % for calculating the desired value of h m=0; ddf = feval ( f, x_opt + 4*h ) - 4*feval( f, x_opt + 3*h )... + 6*feval( f, x_opt + 2*h ) - 4* feval ( f, x_opt + h ) ... + feval( f, x_opt );% fourth order difference % dff defined outside bracket just for convinence while ddf * ( b -a )/180 > tol % error for Simpson's 1by3 rule m = m +1; h = h*10^-m; ddf = feval ( f, x_opt + 4*h ) - 4*feval( f, x_opt + 3*h )... + 6*feval( f, x_opt + 2*h ) - 4* feval ( f, x_opt + h ) ... + feval( f, x_opt );% defined here for looping end %calculating n n = ceil( (b-a)/h ); if rem( n,2) == 0 n = n+1; end h = ( b-a )/n; % calculating matrix X for k = 1:(n+1) X(k,1) = a + (k-1)*h; X(k,2) =feval ( f, X(k,1)); end %calculating integration i= 1; I1 = 0; while ( 2*i ) < (n+1) I1 = I1 + X ( ( 2*i) , 2 ); i = i +1; end j=1; I2 =0; while (2*j + 1) < (n+1) I2 = I2 + X ( ( 2*j + 1) , 2); j = j + 1; end I = h/3 * ( X( 1,2) + 4*I1 + 2*I2 + X(n,2) );% Simpson's 1/3 rule % Display final result h n disp(sprintf(' Using this integration for the given function in the range ( %10f , %10f ) is %10.6f .',a,b,I)) |

The above Matlab code is for Simpson’s 1/3 rule to evaluate the function **f(x) = 2 + cos(2 )**. If the code is to be used to evaluate the numerical integration of other integrands, the value of ‘f’ in the program can be modified as per requirement.

In this MATLAB program, the interval in which the numerical integration is to be carried out and the allowed error or tolerance are the input. After giving these inputs to the program, the value of numerical integration of the function is displayed on the screen.

A sample input/output screenshot of the program is given below:

## Numerical Example of Simpson 1/3 Rule:

Now, let’s analyze mathematically the above program of Simpson rule in Matlab using the same function and limits of integration. The question here is:

*Evaluate the following integral by using Simpson 1/3 rule with m = 1 and 2.*

**Solution:**

The given integrand is : f(x) = 2 + cos(2 )

The graph of f(x) can be shown as:

When m =1, using the expression for Simpson’s 1/3 rule:

I =

When m =2

I =

which is nearly the same as the value obtained from Simpson’s Matlab program.

If you have any questions regarding Simpson’s rule/method, its Matlab code, or mathematical derivation, bring them up from the comments. You can find more Numerical Methods tutorial using Matlab here.

I do not see many comments here, it means you have low visitors. I know how to make your page go viral. If you want to know simply search in google for:

Kimting’s Method To Go Viral