定积分的估算方法
在定积分计算中,常用的估算方法包括矩形法、梯形法、辛普森法和蒙特卡洛方法。
1. 矩形法
左矩形法
将区间 [ 0 , 1 ] [0, 1] [0,1] 分为4个小区间,长度为 Δ x = 0.25 \Delta x = 0.25 Δx=0.25。
- 左端点为 x 0 = 0 x_0 = 0 x0=0, f ( 0 ) = 0 2 = 0 f(0) = 0^2 = 0 f(0)=02=0
- 左端点为 x 1 = 0.25 x_1 = 0.25 x1=0.25, f ( 0.25 ) = 0.2 5 2 = 0.0625 f(0.25) = 0.25^2 = 0.0625 f(0.25)=0.252=0.0625
- 左端点为 x 2 = 0.5 x_2 = 0.5 x2=0.5, f ( 0.5 ) = 0. 5 2 = 0.25 f(0.5) = 0.5^2 = 0.25 f(0.5)=0.52=0.25
- 左端点为 x 3 = 0.75 x_3 = 0.75 x3=0.75, f ( 0.75 ) = 0.7 5 2 = 0.5625 f(0.75) = 0.75^2 = 0.5625 f(0.75)=0.752=0.5625
左矩形法的积分估计为:
估计值
=
Δ
x
⋅
(
f
(
0
)
+
f
(
0.25
)
+
f
(
0.5
)
+
f
(
0.75
)
)
=
0.25
⋅
(
0
+
0.0625
+
0.25
+
0.5625
)
=
0.21875
\text{估计值} = \Delta x \cdot (f(0) + f(0.25) + f(0.5) + f(0.75)) = 0.25 \cdot (0 + 0.0625 + 0.25 + 0.5625) = 0.21875
估计值=Δx⋅(f(0)+f(0.25)+f(0.5)+f(0.75))=0.25⋅(0+0.0625+0.25+0.5625)=0.21875
右矩形法
将区间 [ 0 , 1 ] [0, 1] [0,1] 分为4个小区间,长度为 Δ x = 0.25 \Delta x = 0.25 Δx=0.25。
- 右端点为 x 1 = 0.25 x_1 = 0.25 x1=0.25, f ( 0.25 ) = 0.0625 f(0.25) = 0.0625 f(0.25)=0.0625
- 右端点为 x 2 = 0.5 x_2 = 0.5 x2=0.5, f ( 0.5 ) = 0.25 f(0.5) = 0.25 f(0.5)=0.25
- 右端点为 x 3 = 0.75 x_3 = 0.75 x3=0.75, f ( 0.75 ) = 0.5625 f(0.75) = 0.5625 f(0.75)=0.5625
- 右端点为 x 4 = 1 x_4 = 1 x4=1, f ( 1 ) = 1 2 = 1 f(1) = 1^2 = 1 f(1)=12=1
右矩形法的积分估计为:
估计值
=
Δ
x
⋅
(
f
(
0.25
)
+
f
(
0.5
)
+
f
(
0.75
)
+
f
(
1
)
)
=
0.25
⋅
(
0.0625
+
0.25
+
0.5625
+
1
)
=
0.46875
\text{估计值} = \Delta x \cdot (f(0.25) + f(0.5) + f(0.75) + f(1)) = 0.25 \cdot (0.0625 + 0.25 + 0.5625 + 1) = 0.46875
估计值=Δx⋅(f(0.25)+f(0.5)+f(0.75)+f(1))=0.25⋅(0.0625+0.25+0.5625+1)=0.46875
2. 梯形法
将区间 [ 0 , 1 ] [0, 1] [0,1] 分为4个小区间,长度为 Δ x = 0.25 \Delta x = 0.25 Δx=0.25,计算梯形面积:
- f ( 0 ) = 0 f(0) = 0 f(0)=0
- f ( 0.25 ) = 0.0625 f(0.25) = 0.0625 f(0.25)=0.0625
- f ( 0.5 ) = 0.25 f(0.5) = 0.25 f(0.5)=0.25
- f ( 0.75 ) = 0.5625 f(0.75) = 0.5625 f(0.75)=0.5625
- f ( 1 ) = 1 f(1) = 1 f(1)=1
梯形法的积分估计为:
估计值
=
Δ
x
2
⋅
(
f
(
0
)
+
2
⋅
f
(
0.25
)
+
2
⋅
f
(
0.5
)
+
2
⋅
f
(
0.75
)
+
f
(
1
)
)
\text{估计值} = \frac{\Delta x}{2} \cdot (f(0) + 2 \cdot f(0.25) + 2 \cdot f(0.5) + 2 \cdot f(0.75) + f(1))
估计值=2Δx⋅(f(0)+2⋅f(0.25)+2⋅f(0.5)+2⋅f(0.75)+f(1))
=
0.25
2
⋅
(
0
+
2
⋅
0.0625
+
2
⋅
0.25
+
2
⋅
0.5625
+
1
)
=
0.34375
= \frac{0.25}{2} \cdot (0 + 2 \cdot 0.0625 + 2 \cdot 0.25 + 2 \cdot 0.5625 + 1) = 0.34375
=20.25⋅(0+2⋅0.0625+2⋅0.25+2⋅0.5625+1)=0.34375
3. 辛普森法
辛普森法将区间分为偶数个小区间(这里也是4个),使用抛物线拟合:
估计值
=
Δ
x
3
⋅
(
f
(
0
)
+
4
⋅
f
(
0.25
)
+
2
⋅
f
(
0.5
)
+
4
⋅
f
(
0.75
)
+
f
(
1
)
)
\text{估计值} = \frac{\Delta x}{3} \cdot (f(0) + 4 \cdot f(0.25) + 2 \cdot f(0.5) + 4 \cdot f(0.75) + f(1))
估计值=3Δx⋅(f(0)+4⋅f(0.25)+2⋅f(0.5)+4⋅f(0.75)+f(1))
=
0.25
3
⋅
(
0
+
4
⋅
0.0625
+
2
⋅
0.25
+
4
⋅
0.5625
+
1
)
=
0.3333333
= \frac{0.25}{3} \cdot (0 + 4 \cdot 0.0625 + 2 \cdot 0.25 + 4 \cdot 0.5625 + 1) = 0.3333333
=30.25⋅(0+4⋅0.0625+2⋅0.25+4⋅0.5625+1)=0.3333333
4. 蒙特卡洛方法
假设我们随机生成了10个点在 [ 0 , 1 ] [0, 1] [0,1] 区间:
- 随机点: x = [ 0.1 , 0.15 , 0.35 , 0.5 , 0.65 , 0.8 , 0.9 , 0.95 , 0.3 , 0.7 ] x = [0.1, 0.15, 0.35, 0.5, 0.65, 0.8, 0.9, 0.95, 0.3, 0.7] x=[0.1,0.15,0.35,0.5,0.65,0.8,0.9,0.95,0.3,0.7]
- 对应的函数值: f ( x ) = [ 0.01 , 0.0225 , 0.1225 , 0.25 , 0.4225 , 0.64 , 0.81 , 0.9025 , 0.09 , 0.49 ] f(x) = [0.01, 0.0225, 0.1225, 0.25, 0.4225, 0.64, 0.81, 0.9025, 0.09, 0.49] f(x)=[0.01,0.0225,0.1225,0.25,0.4225,0.64,0.81,0.9025,0.09,0.49]
蒙特卡洛法的积分估计为:
估计值
=
1
n
∑
i
=
1
n
f
(
x
i
)
=
1
10
⋅
(
0.01
+
0.0225
+
0.1225
+
0.25
+
0.4225
+
0.64
+
0.81
+
0.9025
+
0.09
+
0.49
)
=
0.376
\text{估计值} = \frac{1}{n} \sum_{i=1}^{n} f(x_i) = \frac{1}{10} \cdot (0.01 + 0.0225 + 0.1225 + 0.25 + 0.4225 + 0.64 + 0.81 + 0.9025 + 0.09 + 0.49) = 0.376
估计值=n1i=1∑nf(xi)=101⋅(0.01+0.0225+0.1225+0.25+0.4225+0.64+0.81+0.9025+0.09+0.49)=0.376
总结
- 左矩形法:0.21875
- 右矩形法:0.46875
- 梯形法:0.34375
- 辛普森法:0.3333333
- 蒙特卡洛法:0.376
实际的积分值为:
∫
0
1
x
2
d
x
=
[
x
3
3
]
0
1
=
1
3
≈
0.3333333
\int_0^1 x^2 \, dx = \left[ \frac{x^3}{3} \right]_0^1 = \frac{1}{3} \approx 0.3333333
∫01x2dx=[3x3]01=31≈0.3333333
辛普森法的实现与思想
辛普森法是一种数值积分的方法,属于复合数值积分技术的一种。它通过将被积函数在区间上的值使用抛物线进行近似,从而计算出积分值。辛普森法能够有效提高数值积分的精度,特别是在被积函数是连续且光滑的情况下。
辛普森法的思想
辛普森法的核心思想是利用抛物线来近似曲线。具体来说,它在积分区间上选取若干个点,将这些点之间的曲线用二次多项式(即抛物线)来代替。相比于梯形法将区间划分为直线,辛普森法用抛物线能够更好地逼近函数的实际形态,尤其是在函数曲线较平滑时。
一般形式
考虑函数 f ( x ) f(x) f(x) 在区间 [ a , b ] [a, b] [a,b] 上的积分:
I = ∫ a b f ( x ) d x I = \int_a^b f(x) \, dx I=∫abf(x)dx
辛普森法通过以下方式进行近似:
-
将区间 [ a , b ] [a, b] [a,b] 分为偶数个子区间,每个子区间的宽度为 h h h:
h = b − a n h = \frac{b - a}{n} h=nb−a
其中 n n n 为偶数。 -
在区间上选取划分点 x 0 = a , x 1 = a + h , … , x n = b x_0 = a, x_1 = a + h, \dots, x_n = b x0=a,x1=a+h,…,xn=b。
-
对于每三个点,使用二次多项式近似函数值,具体公式为:
∫ x 2 i x 2 i + 2 f ( x ) d x ≈ h 3 [ f ( x 2 i ) + 4 f ( x 2 i + 1 ) + f ( x 2 i + 2 ) ] \int_{x_{2i}}^{x_{2i+2}} f(x) \, dx \approx \frac{h}{3} \left[ f(x_{2i}) + 4f(x_{2i+1}) + f(x_{2i+2}) \right] ∫x2ix2i+2f(x)dx≈3h[f(x2i)+4f(x2i+1)+f(x2i+2)]
将整个区间的积分近似为这些部分积分的和,得到复合辛普森法公式:
I ≈ h 3 [ f ( x 0 ) + 4 ∑ i = 1 , 3 , 5 , … n − 1 f ( x i ) + 2 ∑ i = 2 , 4 , 6 , … n − 2 f ( x i ) + f ( x n ) ] I \approx \frac{h}{3} \left[ f(x_0) + 4 \sum_{i=1,3,5,\dots}^{n-1} f(x_i) + 2 \sum_{i=2,4,6,\dots}^{n-2} f(x_i) + f(x_n) \right] I≈3h[f(x0)+4i=1,3,5,…∑n−1f(xi)+2i=2,4,6,…∑n−2f(xi)+f(xn)]
实现步骤
-
划分区间: 将积分区间 [ a , b ] [a, b] [a,b] 划分为 n n n 个子区间,确保 n n n 为偶数。
-
计算步长 h h h:
h = b − a n h = \frac{b - a}{n} h=nb−a -
计算积分值:
- 首先计算端点函数值 f ( x 0 ) f(x_0) f(x0) 和 f ( x n ) f(x_n) f(xn)。
- 计算奇数索引处函数值 f ( x 1 ) , f ( x 3 ) , … , f ( x n − 1 ) f(x_1), f(x_3), \dots, f(x_{n-1}) f(x1),f(x3),…,f(xn−1) 的和。
- 计算偶数索引处函数值 f ( x 2 ) , f ( x 4 ) , … , f ( x n − 2 ) f(x_2), f(x_4), \dots, f(x_{n-2}) f(x2),f(x4),…,f(xn−2) 的和。
- 使用辛普森法公式将结果组合,得到积分值。
-
输出结果: 得到近似的积分值。
Python 实现
def simpson(f, a, b, n):
if n % 2 != 0:
raise ValueError("区间划分数 n 必须为偶数")
h = (b - a) / n
x0 = f(a) + f(b)
x1 = sum(f(a + i * h) for i in range(1, n, 2))
x2 = sum(f(a + i * h) for i in range(2, n, 2))
return (h / 3) * (x0 + 4 * x1 + 2 * x2)
# 示例使用
import math
result = simpson(math.sin, 0, math.pi, 10)
print("积分结果:", result)
C语言实现
#include <stdio.h>
#include <math.h>
double f(double x) {
// 被积函数,例如 f(x) = sin(x)
return sin(x);
}
double simpson(double (*f)(double), double a, double b, int n) {
if (n % 2 != 0) {
printf("区间划分数 n 必须为偶数\n");
return -1;
}
double h = (b - a) / n;
double sum_odd = 0.0, sum_even = 0.0, result;
// 计算奇数索引处的函数值和偶数索引处的函数值
for (int i = 1; i < n; i += 2) {
sum_odd += f(a + i * h);
}
for (int i = 2; i < n; i += 2) {
sum_even += f(a + i * h);
}
// 使用辛普森公式计算积分
result = (h / 3.0) * (f(a) + 4.0 * sum_odd + 2.0 * sum_even + f(b));
return result;
}
int main() {
double a = 0.0;
double b = M_PI; // 积分区间 [0, π]
int n = 10; // 划分为10个区间
double result = simpson(f, a, b, n);
if (result != -1) {
printf("积分结果: %f\n", result);
}
return 0;
}
方法优缺点
优点:
- 比梯形法和矩形法精度更高,特别是在被积函数平滑时。
- 计算复杂度适中。
缺点:
- 需要划分区间数 n n n 为偶数,否则无法应用。
- 对于非常复杂或非光滑的函数,仍可能出现误差。
辛普森法的思想核心是通过二次插值来提高逼近精度,因此在实际应用中,尤其适合处理具有一定平滑度的函数积分问题。