-
Notifications
You must be signed in to change notification settings - Fork 0
/
Simpson(3_8)
39 lines (32 loc) · 814 Bytes
/
Simpson(3_8)
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
#include <stdio.h>
// Given function to be integrated
float func( float x)
{
return (1 / ( 1 + x * x ));
}
// Function to perform calculations
float calculate(float a, float b, int n)
{
float h = (b - a)/ n;
printf("Value of h: %f\n", h);
// Calculating result
float res = func(a) + func(b);
for(int i = 1 ; i < n ; i++)
{
if (i % 3 == 0)
res = res + 2 * func(a+i*h);
else
res = res + 3 * func(a+i*h);
}
return (3.0/8.0 * h) * res;
}
void main()
{
int interval_limit;
float lower_limit, upper_limit;
printf("Enter the values of lower limit and upper limit: ");
scanf("%f %f", &lower_limit, &upper_limit);
printf("Enter the interval_limit: ");
scanf("%d", &interval_limit);
printf("Result is: %f", calculate(lower_limit, upper_limit, interval_limit));
}