-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimpsons.cpp
More file actions
97 lines (70 loc) · 1.52 KB
/
Copy pathsimpsons.cpp
File metadata and controls
97 lines (70 loc) · 1.52 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include<iostream>
#include<math.h>
#define EPS 1.0e-6
double f(double x)
{
return x*x + 3.0;
}
double g(double x)
{
return 12.0 - x*x;
}
double p(double x)
{
return 9.0 - 2.0*x*x;
}
using namespace std;
int main()
{
double a,b;
double root;
a = 0.0, b = 2.5;
if((p(a) * p(b)) > 0)
cout << "No root found!" << endl;
else
{
while(1)
{
double xnot = (a+b)/2.0;
if(fabs(p(xnot))<=EPS)
{
root = xnot;
break;
}
else if((p(a) * p(xnot)) < 0)
b = xnot;
else
a = xnot;
}
}
int n = 1000;
a = 0, b = root;
double h = (b-a)/(n-1);
double x1[n],f1[n],g1[n];
for(int i=0; i<n; i++)
{
x1[i] = a + i*h;
f1[i] = f(x1[i]);
g1[i] = g(x1[i]);
}
double odd1 = 0, even1 = 0;
double odd2 = 0, even2 = 0;
for(int i=1; i<n-1; i++)
{
if( (i%2)==0 )
{
odd1 = odd1+ 4*f1[i];
odd2 = odd2+ 4*g1[i];
}
else
{
even1 = even1 + 2*f1[i];
even2 = even2 + 2*g1[i];
}
}
double area1 = h/3.0 * ( f1[0] + odd1 + even1 + f1[n-1] );
double area2 = h/3.0 * ( g1[0] + odd2+ even2 + g1[n-1] );
cout << fabs(area2 - area1) << endl;
cout << "Root is: " << root << endl;
return 0;
}