-
Notifications
You must be signed in to change notification settings - Fork 0
/
09_Simpsons_1_3_Rule.c
52 lines (43 loc) · 1.14 KB
/
09_Simpsons_1_3_Rule.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
46
47
48
49
50
51
52
#include <stdio.h>
#include <math.h>
/* Define function here */
#define f(x) 1 / (1 + x * x)
int main()
{
float lower, upper, integration = 0.0, stepSize, k;
int i, subInterval;
/* Input */
printf("Enter lower limit of integration: ");
scanf("%f", &lower);
printf("Enter upper limit of integration: ");
scanf("%f", &upper);
printf("Enter number of sub intervals: ");
scanf("%d", &subInterval);
/* Calculation */
/* Finding step size */
stepSize = (upper - lower) / subInterval;
/* Finding Integration Value */
integration = f(lower) + f(upper);
for (i = 1; i <= subInterval - 1; i++)
{
k = lower + i * stepSize;
if (i % 2 == 0)
{
integration = integration + 2 * f(k);
}
else
{
integration = integration + 4 * f(k);
}
}
integration = integration * stepSize / 3;
printf("\nRequired value of integration is: %.3f", integration);
return 0;
}
/*
Output :
Enter lower limit of integration: 0
Enter upper limit of integration: 1
Enter number of sub intervals: 6
Required value of integration is: 0.785
*/