-
Notifications
You must be signed in to change notification settings - Fork 0
/
Simpson1~3.c
43 lines (34 loc) · 830 Bytes
/
Simpson1~3.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
#include <stdio.h>
#include <math.h>
float func(float x)
{
return x * x + 1;
}
int main()
{
float a, b, h, n, sum = 0, x;
printf("enter the lower bound: ");
scanf("%f", &a);
printf("enter the upper bound: ");
scanf("%f", &b);
printf("enter the number of sub internval: ");
scanf("%f", &n);
h = (b - a) / n;
for (int i = 1; i < n; i++)
{
x = a + i * h;
if (i%2 == 0)
{
sum = sum+2*func(x);
}
else
{
sum = sum + 4*func(x);
}
}
sum = h / 3 * (func(a) + func(b) + sum);
printf("Simpson 1/3 Rule - - - - - - - - - -\n");
printf("Integral value = %.6lf\n", sum);
printf("- - - - - - - - - - - - - - - - - - -\n");
return 0;
}