Skip to content

Commit c784f85

Browse files
authored
simpsons 1/3rd rule implementation
1 parent adc56cb commit c784f85

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

simpsons_method.c

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include <math.h>
2+
#include <stdio.h>
3+
4+
float f(float x)
5+
{
6+
return 1.0 +
7+
x * x * x; // This is the expresion of the function to integrate?
8+
}
9+
10+
int main()
11+
{
12+
int i, n;
13+
float a, b, h, x, s2, s3, sum, integral;
14+
15+
printf("enter the lower limit of the integration:");
16+
scanf("%f", &a);
17+
printf("enter the upper limit of the integration:");
18+
scanf("%f", &b);
19+
printf("enter the number of intervals:");
20+
scanf("%d", &n);
21+
22+
h = (b - a) / n;
23+
sum = f(a) + f(b);
24+
s2 = s3 = 0.0;
25+
26+
for (i = 1; i < n; i += 3)
27+
{
28+
x = a + i * h;
29+
s3 = s3 + f(x) + f(x + h);
30+
}
31+
32+
for (i = 3; i < n; i += 3)
33+
{
34+
x = a + i * h;
35+
s2 = s2 + f(x);
36+
}
37+
38+
integral = (h / 3.0) * (sum + 2 * s2 + 4 * s3);
39+
printf("\nValue of the integral = %9.4f\n", integral);
40+
41+
return 0;
42+
}

0 commit comments

Comments
 (0)