-
Notifications
You must be signed in to change notification settings - Fork 0
/
Simpson(1_3)
43 lines (36 loc) · 852 Bytes
/
Simpson(1_3)
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>
// Function to calculate f(x)
float func(float x)
{
return log(x);
}
// Function for approximate integral
float simpsons(float ll, float ul, int n)
{
// Calculating the value of h
float h = (ul - ll) / n;
printf("Value of h: %f\n", h);
// Calculating result
float res = func(ll) + func(ul);
for(int i = 1; i < n; i++)
{
if(i % 2 == 0)
res = res + 2 * func(ll+i*h);
else
res = res + 4 * func(ll+i*h);
}
res = res * (h / 3);
return res;
}
int main()
{
float lower_limit, upper_limit;
int n; // Number of interval
printf("Enter the lower limit and upper limit: ");
scanf("%f %f", &lower_limit, &upper_limit);
printf("Enter the number of intervals: ");
scanf("%d", &n);
printf("The result is: %f\n", simpsons(lower_limit, upper_limit, n));
return 0;
}