-
Notifications
You must be signed in to change notification settings - Fork 0
/
Trapezoidal
41 lines (33 loc) · 844 Bytes
/
Trapezoidal
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
#include<stdio.h>
float y(float x)
{
return 1/(1+x*x);
}
// Function to evaluate the value of integral
float trapezoidal(float a, float b, float n)
{
float h = (b-a)/n;
// Computing sum of first and last terms in above formula
float s = y(a)+y(b);
printf("\n%f\n", (h/2)*s);
// Adding middle terms in above formula
for (int i = 1; i < n; i++)
{
s += 2*y(a+i*h);
printf("%f\n", (h/2)*s);
}
// h/2 indicates (b-a)/2n. Multiplying h/2
// with s.
return (h/2)*s;
}
int main()
{
float x0, xn; // Range of definite integral
int n; // Number of grids. Higher value means more accuracy
printf("Enter the range of definite integral: ");
scanf("%f %f", &x0, &xn);
printf("Enter the number of grids: ");
scanf("%d", &n);
printf("\nValue of integral is %6.4f\n", trapezoidal(x0, xn, n));
return 0;
}