-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalX.cpp
71 lines (54 loc) · 1.38 KB
/
calX.cpp
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
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
double int_mc1(double(*)(double), double&, double, double, int);
double f(double);
int main() {
double a, b, mc, errest;
int i, n;
int ntimes;
const double pi = 3.1415926;
cout.precision(6);
cout.setf(ios::fixed | ios::showpoint);
cout << "Please enter a: ";
cin >> a;
cout << "Please enter b: ";
cin >> b;
cout << "Please enter n: ";
cin >> n;
ntimes = 16;
cout << " Points "<<"Integral " << " error"<< endl;
for (i=0; i <=ntimes; i=i+1) {
mc = int_mc1(f, errest, a, b, n);
cout << setw(10) << n << setw(12) << mc << setw(12) << errest <<endl;
n = n*2;
}
system("pause");
return 0;
}
double f(double x) {
const double pi = 3.1415926;
double y;
y = sin(x);
return y;
}
double int_mc1(double(*f)(double), double& errest, double a, double b, int n) {
double r, x, u, f2s, fs;
srand(time(NULL));
fs = 0.0;
f2s = 0.0;
for (int i = 1; i <= n; i=i+1) {
u = 1.0*rand()/(RAND_MAX+1);
x = a + (b-a)*u;
fs = fs + f(x);
f2s= f2s+ f(x)*f(x);
}
r = fs*(b-a)/n;
fs = fs/n;
f2s = f2s/n;
errest = (b-a)*sqrt((f2s - fs*fs)/n);
return r;
}