include stdio include math include iostream include string include tim

  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
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <stdio.h>
#include <math.h>
#include <iostream>
#include <string>
#include <time.h>
#define EPS 0.01
using std::cout;
using std::cin;
using std::endl;
class NumericalIntegration {
private:
double (*func)(double); //функция
double left, right; //границы
double real_value; //эталон
double min, max; //минимальное и максимальное
//значения функции
double (NumericalIntegration::*tek_method)(int);
double fRand(double fMin, double fMax);
//функции численного интегрирования
double MiddRect(int count);
double Trapeze(int count);
double Simpson(int count);
double MonteKarlo(int count);
public:
enum methods{MIDDRECT, TRAPEZE, SIMPSON, MONTEKARLO};
NumericalIntegration(double (*f)(double),
double l, double r, double val, double min, double max);
~NumericalIntegration() {};
//аксессоры
void SetFunction(double (*f)(double)) { func = f; };
void SetBoards(double l, double r) { left = l; right = r; };
void SetRealValue(double v) { real_value = v; };
void SetMinValue(double v) { min = v; }
void SetMaxValue(double v) { max = v; }
void Calc(methods method, double eps);
};
NumericalIntegration::NumericalIntegration(double (*f)(double),
double l, double r, double val, double min, double max)
{
func = f;
left = l;
right = r;
real_value = val;
this->min = min;
this->max = max;
}
double NumericalIntegration::fRand(double fMin, double fMax)
{
double f = (double)rand() / RAND_MAX;
return fMin + f * (fMax - fMin);
}
double NumericalIntegration::MiddRect(int count)
{
double step = (right - left) / count;
double res = 0;
for (int i = 0; i < count; i++) {
res += func(left + step * (2 * i + 1) / 2);
}
return res * step;
}
double NumericalIntegration::Trapeze(int count)
{
double step = (right - left) / count;
double res = 0;
for (int i = 1; i <= count; i++) {
res += func(left + step * (i - 1)) + func(left + step * i);
}
return res * step / 2;
}
double NumericalIntegration::Simpson(int count)
{
double step = (right - left) / count;
double res = 0;
for (int i = 1; i < count; i += 2) {
res += func(left + step * (i - 1)) + 4 * func(left + step * i) + func(left + step * (i + 1));
}
return res * step / 3;
}
double NumericalIntegration::MonteKarlo(int count)
{
int k = 0;
for (int i = 0; i < count; i++) {
double x = fRand(left, right);
double y = fRand(min, max);
if (func(x) > y) k++;
}
return (right - left) * (max - min) * k / count;
}
void NumericalIntegration::Calc(methods method, double eps)
{
int err, k, count = 1;
std::string method_name;
switch (method) {
case MIDDRECT:
tek_method = &NumericalIntegration::MiddRect;
err = 3;
k = 2;
method_name = "MIDDRECT";
break;
case TRAPEZE:
tek_method = &NumericalIntegration::Trapeze;
err = 3;
k = 2;
method_name = "TRAPEZE ";
break;
case SIMPSON:
tek_method = &NumericalIntegration::Simpson;
err = 15;
k = 4;
method_name = "SIMPSON ";
break;
case MONTEKARLO:
tek_method = &NumericalIntegration::MonteKarlo;
err = 1;
count = 16;
method_name = "MONTEKARLO";
break;
default:
cout << "Unknown method" << endl;
return;
}
double I1;
double I2 = (this->*tek_method)(count);
do {
count *= 2;
I1 = I2;
I2 = (this->*tek_method)(count);
} while (abs(I1 - I2) / err > eps);
double richardson = (method == MONTEKARLO) ? I2 :
((1 << k) * I2 - I1) / ((1 << k) - 1);
cout << method_name << '\t'
<< count << '\t';
if (method != MONTEKARLO) cout << I2 << '\t'
<< richardson << '\t';
cout << abs(real_value - richardson) << endl;
}
double Function(double x) { return exp(x); }
int main()
{
srand(time(NULL));
NumericalIntegration integrate(Function, 0, 1,
exp(1.0) - 1, 0, Function(1));
integrate.Calc(NumericalIntegration::MIDDRECT, EPS);
integrate.Calc(NumericalIntegration::TRAPEZE, EPS);
integrate.Calc(NumericalIntegration::SIMPSON, EPS);
integrate.Calc(NumericalIntegration::MONTEKARLO, EPS);
getchar();
return 0;
}