Integration is an integral part in science and engineering to calculate things such as area, volume, total flux, electric field, magnetic field and many more. Here, we are going to take a look at numerical integration method (Simpson’s 1/3 rule in particular using C language) to solve such complex integration problems.

For this, let’s discuss the **C program for Simpson 1/3 rule** for easy and accurate calculation of numerical integration of any function which is defined in program.

In the source code below, a function *f(x) = 1/(1+x)* has been defined. The calculation using **Simpson 1/3 rule in C** is based on the fact that the small portion between any two points is a parabola. The program follows the following steps for calculation of the integral.

- As the program gets executed, first of all it asks for the value of lower boundary value of x i.e. x
_{0}, upper boundary value of x i.e. x_{n }and width of the strip, h. - Then the program finds the value of number of strip as n=( x
_{n }– x_{0 })/h and checks whether it is even or odd. If the value of ‘n’ is odd, the program refines the value of ‘h’ so that the value of ‘n’ comes to be even. - After that, this C program calculates value of f(x) i.e ‘y’ at different intermediate values of ‘x’ and displays values of all intermediate values of ‘y’.
- After the calculation of values of ‘c’, the program uses the following formula to calculate the value of integral in loop.

Integral = *((y_{0 }+ y_{n }) +4(y_{1 }+ y_{3 }+ ……….+ y_{n-1 }) + 2(y_{2 }+ y_{4 }+……….+ y_{n-2 }))

- Finally, it prints the values of integral which is stored as ‘ans’ in the program.

If f(x) represents the length, the value of integral will be area, and if f(x) is area, the output of Simpson 1/3 rule C program will be volume. Hence, numerical integration can be carried out using the program below; it is very easy to use, simple to understand, and gives reliable and accurate results.

**f(x) = 1/(1+x)**

## Source Code for Simpson 1/3 Rule in C:

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 |
#include<stdio.h> #include<conio.h> float f(float x) { return(1/(1+x)); } void main() { int i,n; float x0,xn,h,y[20],so,se,ans,x[20]; printf("\n Enter values of x0,xn,h: "); scanf("%f%f%f",&x0,&xn,&h); n=(xn-x0)/h; if(n%2==1) { n=n+1; } h=(xn-x0)/n; printf("\n Refined value of n and h are:%d %f\n",n,h); printf("\n Y values: \n"); for(i=0; i<=n; i++) { x[i]=x0+i*h; y[i]=f(x[i]); printf("\n %f\n",y[i]); } so=0; se=0; for(i=1; i<n; i++) { if(i%2==1) { so=so+y[i]; } else { se=se+y[i]; } } ans=h/3*(y[0]+y[n]+4*so+2*se); printf("\n Final integration is %f",ans); getch(); } |

### Input/Output:

Also see,

Simpson 1/3 Rule in Matlab Numerical Methods Tutorial Compilation

Compared to the numerical integration methods, like the program of Simpson 1/3 rule in C given above, the analytical method of integration is quite difficult and time consuming while applying to complex engineering problems. Sometimes, the analytical method even fails to be applied.

So, as the solution of such complex problems numerical integration, methods such as Gauss’s two point and three point integration, Romberg’s method, Simpson’s trapezoidal, Simpson’s 1/3 (discussed here) Simpson’s 3/8 rules, etc. have been developed.

This program not run for different type of equations.

What equations did you try?

Thank you for this very clear explanation. It was really helpful for me, since I don’t have much experience in C. As a suggestion, I would like to see something on the derivation of the integration formula or a link to a ressource.

Good to know you liked the post Gerret 🙂 As for the derivation of the integration formula, you can find it on any Numerical Methods book. And, you can download one of the books in pdf format for free here – Downloads.

y do we have to refine the values of n and h?