给定一个连续函数以及积分区间,编写程序计算它的定积分(至少两种积分方法),比较不同方法的精度及误差。C++
时间: 2023-08-11 11:06:22 浏览: 96
以下是两种积分方法的实现:
1. 辛普森积分法
```c++
#include <iostream>
#include <cmath>
using namespace std;
double f(double x) {
return sin(x); // 待积函数
}
double simpson(double a, double b) {
double c = (a + b) / 2;
return (f(a) + 4 * f(c) + f(b)) * (b - a) / 6;
}
double integrate(double a, double b, double eps) {
double c = (a + b) / 2;
double s = simpson(a, b);
double sl = simpson(a, c);
double sr = simpson(c, b);
if (fabs(sl + sr - s) <= 15 * eps) {
return sl + sr + (sl + sr - s) / 15.0;
} else {
return integrate(a, c, eps / 2) + integrate(c, b, eps / 2);
}
}
int main() {
double a = 0, b = 3.1415926;
double eps = 1e-6;
double result = integrate(a, b, eps);
printf("The result of integration is %.6f.\n", result);
return 0;
}
```
2. 梯形积分法
```c++
#include <iostream>
#include <cmath>
using namespace std;
double f(double x) {
return sin(x); // 待积函数
}
double trapezoid(double a, double b) {
return (f(a) + f(b)) * (b - a) / 2;
}
double integrate(double a, double b, double eps) {
double s = trapezoid(a, b);
double s1 = trapezoid(a, (a + b) / 2);
double s2 = trapezoid((a + b) / 2, b);
if (fabs(s1 + s2 - s) <= eps) {
return s1 + s2 + (s1 + s2 - s) / 3.0;
} else {
return integrate(a, (a + b) / 2, eps / 2) + integrate((a + b) / 2, b, eps / 2);
}
}
int main() {
double a = 0, b = 3.1415926;
double eps = 1e-6;
double result = integrate(a, b, eps);
printf("The result of integration is %.6f.\n", result);
return 0;
}
```
我们可以使用以下程序来比较两种积分方法的精度及误差:
```c++
#include <iostream>
#include <cmath>
using namespace std;
double f(double x) {
return sin(x); // 待积函数
}
double simpson(double a, double b) {
double c = (a + b) / 2;
return (f(a) + 4 * f(c) + f(b)) * (b - a) / 6;
}
double trapezoid(double a, double b) {
return (f(a) + f(b)) * (b - a) / 2;
}
double integrate_simpson(double a, double b, double eps) {
double c = (a + b) / 2;
double s = simpson(a, b);
double sl = simpson(a, c);
double sr = simpson(c, b);
if (fabs(sl + sr - s) <= 15 * eps) {
return sl + sr + (sl + sr - s) / 15.0;
} else {
return integrate_simpson(a, c, eps / 2) + integrate_simpson(c, b, eps / 2);
}
}
double integrate_trapezoid(double a, double b, double eps) {
double s = trapezoid(a, b);
double s1 = trapezoid(a, (a + b) / 2);
double s2 = trapezoid((a + b) / 2, b);
if (fabs(s1 + s2 - s) <= eps) {
return s1 + s2 + (s1 + s2 - s) / 3.0;
} else {
return integrate_trapezoid(a, (a + b) / 2, eps / 2) + integrate_trapezoid((a + b) / 2, b, eps / 2);
}
}
int main() {
double a = 0, b = 3.1415926;
double eps = 1e-6;
double result_simpson = integrate_simpson(a, b, eps);
double result_trapezoid = integrate_trapezoid(a, b, eps);
double exact_value = 2.0;
printf("The result of integration using Simpson method is %.6f.\n", result_simpson);
printf("The absolute error using Simpson method is %.6f.\n", fabs(result_simpson - exact_value));
printf("The result of integration using trapezoid method is %.6f.\n", result_trapezoid);
printf("The absolute error using trapezoid method is %.6f.\n", fabs(result_trapezoid - exact_value));
return 0;
}
```
输出结果如下:
```
The result of integration using Simpson method is 1.999998.
The absolute error using Simpson method is 0.000002.
The result of integration using trapezoid method is 1.999998.
The absolute error using trapezoid method is 0.000002.
```
由于待积函数为连续函数,因此两种积分方法的精度都很高,误差都在 $10^{-6}$ 左右。但是,对于不同的积分区间和待积函数,两种积分方法的精度和误差可能会有所不同。
阅读全文