double func(double x) {
return sqrt(x * x + 5 * log(x * x + x + 3));
}
double simpsonIntegration(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;
sum += (i % 2 == 0) ? 2 * func(x) : 4 * func(x);
}
return (h / 3) * sum;
}
double monteCarloIntegration(double a, double b, int n) {
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<> dis(a, b);
int count = 0;
for (int i = 0; i < n; i++) {
double x = dis(gen);
double y = func(x);
if (y > 0) {
double u = dis(gen);
if (u <= y) {
count++;
}
}
}
double integral = (b - a) * count / n;
return integral;
}
int main() {
double a = 0.0; // Boshlang'ich kesishma
double b = 1.0; // Oxirgi kesishma
int n = 10; // Bo'linish soni
double simpsonResult = simpsonIntegration(a, b, n);
double monteCarloResult = monteCarloIntegration(a, b, n * 110);
std::cout << "Simpson: " << simpsonResult << std::endl;
std::cout << "Monte Carlo: " << monteCarloResult << std::endl;
return 0;
}