-
Notifications
You must be signed in to change notification settings - Fork 0
/
Area.c
57 lines (44 loc) · 1.09 KB
/
Area.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
53
54
55
56
57
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define EPS 10.0e-6
double f(double x) {
return sqrt(x);
}
double g(double x) {
return x * x;
}
double find_intersection() {
double a = 0.5, b = 1.25;
double c;
while (fabs(b - a) > EPS) {
c = (a + b) / 2.0;
double fc = f(c) - g(c);
if (fc == 0 || fabs(b - a) / 2.0 < EPS)
return c;
if (fc * (f(a) - g(a)) < 0)
b = c;
else
a = c;
}
return c;
}
double simpsons(double (*func)(double), double a, double b, int n) {
double h = (b - a) / n;
double sum = func(a) + func(b);
for (int i = 1; i < n; i++) {
double x = a + i * h;
if (i % 2 == 0)
sum += 2 * func(x);
else
sum += 4 * func(x);
}
return h / 3.0 * sum;
}
int main(void) {
double intersection = find_intersection();
double area = simpsons(f, 0.0, intersection, 10000) - simpsons(g, 0.0, intersection, 10000);
printf("Intersection point: %lf\n", intersection);
printf("Area : %lf\n", area);
return 0;
}