Simpson's Rule Formula
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. x0, upper boundary value of x i.e. xn and width of the strip, h.
- Then the program finds the value of number of strip as n=( xn – x0 )/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 = *((y0 + yn ) +4(y1 + y3 + ……….+ yn-1 ) + 2(y2 + y4 +……….+ yn-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)
Simpson's Rule Formula is used to calculate the integral value of any function. It calculates the value of the area under any curve over a given interval by dividing the area into equal parts. It follows the method similar to integration by parts.
In order to integrate any function f(x) in the interval (a,b), follow the steps given below:
- Select a value for n, which is the number of parts the interval is divided into. Let the value of n be an even number.
- Calculate the width, h = b−an.
- Calculate the values of x0 to xn as x0 = a, x1 = x0 + h,.....xn-1 = xn-2 + h, xn = b.
- Consider y = f(x). Now find the values of y(y0 to yn) for the corresponding x(x0 to xn) values.
- Substitute all the above found values in the Simpson's Rule Formula to calculate the integral value.
Simpson's One Third Rule Formula is,
Question 1: Calculate the integral of the function, f(x) = 2x in the interval (0,1) using Simpson's one third rule ?
Solution:
Given,
f(x) = 2x
a = 0
b = 1
Let, n = 6
h = b−an
h = 1−06
h = 16
x0 = a = 0
x1 = x0 + h = 0 + 16 = 16
x2 = x1 + h = 16 + 16 = 26 = 13
x3 = x2 + h = 26 + 16 = 36 = 12
x4 = x3 + h = 36 + 16 = 46 = 23
x5 = x4 + h = 46 + 16 = 56
x6 = b = 1
y0 = f(0) = 2(0) = 0
y1 = f(16) = 2(16) = 13
y2 = f(13) = 2(13) = 23
y3 = f(12) = 2(12) = 1
y4 = f(23) = 2(23) = 43
y5 = f(56) = 2(56) = 53
y6 = f(1) = 2(1) = 2
Simpson's one third rule is,
∫baf(x)dx = h3 [(y0 + yn) + 4(y1 + y3 +...+ yn-1) + 2(y2 + y4 +....+ yn-2)]
∫102x dx = 163 [(y0 + y6) + 4(y1 + y3 + y5) + 2(y2 + y4)]
∫102x dx = 19 [(0 + 2) + 4(13 + 1 + 53) + 2(23 + 43)]
∫102x dx = 19 [2 + 4(3) + 2(2)]
∫102x dx = 19 [2 + 12 + 4]
∫102x dx = 19 × 18
∫102x dx = 2
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();
}
|
OUTPUT
PLZ... Share This Site To Your Contacts.....
With regards
Team EveTech