-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAreaProblem2014-15-Question-5.c
79 lines (60 loc) · 1.34 KB
/
AreaProblem2014-15-Question-5.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <stdio.h>
#include <math.h>
#define ep 1e-9
#define f(x) sqrt(x)
#define g(x) x*x
#define h(x) f(x)-g(x)
double bisection(double a, double b){
double c;
if(h(a)*h(b)>0){
printf("No root");
return -1;
}
while(1){
c = (a+b)/2;
if( fabs(h(c)) <= ep ){
break;
}
if(h(a)*h(c) < 0.0)
b = c;
else
a = c;
}
return c;
}
double simpson(double a, double b, int n){
double h = (b-a)/n;
double area, fx[n+1];
double sum_odd = 0.0, sum_even = 0.0;
int i=0;
double x = a;
while(x<=b){
fx[i++] = fabs(h(x));
x += h;
}
for(int i=1; i<(sizeof(fx)/sizeof(fx[0]))-1; i++){
if(i%2){
sum_odd += fx[i];
}
else{
sum_even += fx[i];
}
}
area = fx[0] + fx[(sizeof(fx)/sizeof(fx[0]))-1] + (4*sum_odd) + (2*sum_even);
area = (area*h)/3;
return area;
}
int main(void){
int n;
printf("Input interval: ");
scanf("%d", &n);
if(n%2 != 0){
printf("\nSimpson Method can't be applied. Interval must be even");
return 0;
}
double a, area;
a = bisection(1.0, 2.5); // parameter value is given in question
area = simpson(0.0, a, n);
printf("Area is: %.3lf\n", area);
return 0;
}