13. Implement Simpson’s rule for numerical integration
ALGORYTHMS :-
Step – 1 : Start
Step – 2 : Declare i, n as integers
Step – 3 : Declare s1 = 0, s2 = 0, a, b, h, x, integral as float variables
Step – 4 : Read a, b
Step – 5 : Enter the number of steps i.e., value of n
Step – 6 : Assign h = (b – a)/n
Step – 7 : Assign x = a
Step – 8 : Initialize i = 1
Step – 9 : If i < n condition is true go to Step–10 else Step–15
Step – 10 : Assign x += h
Step – 11 : If(i % 2 == 0) go to Step–12 else go to Step–13
Step – 12 : Assign s1 += 2 * f(x)
Step – 13 : Assign s2 += 4 * f(x)
Step – 14 : Increment i value and go to Step–9
Step – 15 : Assign integral value i.e, integral = (h/3) * (f(a) + s1 + s2 + f(b))
Step – 16 : Print the values of integral
Step – 17 : Stop
PROGRAM :-
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
float f(float x)
{
return(1/(1 + x));
}
main()
{
int i, n;
float s1 = 0, s2 = 0, a, b, h, x, integral;
clrscr();
printf("Enter a and b limits \n");
scanf("%f,%f",&a,&b);
printf("Enter no. of steps : \n");
scanf("%d",&n);
h = (b - a) / n;
x = a;
for(i = 1; i < n; i++)
{
x += h;
if(i % 2 == 0)
s1 += 2 * f(x);
else
s2 += 4 * f(x);
}
integral = (h / 3) * (f(a) + s1 + s2 + f(b));
printf("Integral is %f",integral);
getch();
return 0;
}
OUTPUT:-
Run 1:
Enter a and b limits
2,4
Enter no. of steps :
2
Integral is 0.511111
Run 2:
Enter a and b limits
5,7
Enter no. of steps :
5
Integral is 0.270600
0 Comments