用步长自动减半的复化梯形公式、复化Simpson公式、复化Cotes公式、Romberg积分法计算积分cos(x^2)dx,积分区域为[0,1]。自己设置不同精度要求,对结果进行比较分析。
时间: 2024-05-06 07:17:15 浏览: 99
我们先定义积分函数cos(x^2)dx:
```python
import math
def f(x):
return math.cos(x**2)
```
然后,我们分别实现步长自动减半的复化梯形公式、复化Simpson公式、复化Cotes公式和Romberg积分法:
```python
# 复化梯形公式
def trapezoid(a, b, n):
h = (b - a) / n
s = (f(a) + f(b)) / 2
for i in range(1, n):
s += f(a + i * h)
return s * h
# 复化Simpson公式
def simpson(a, b, n):
h = (b - a) / n
s = f(a) + f(b)
for i in range(1, n, 2):
s += 4 * f(a + i * h)
for i in range(2, n-1, 2):
s += 2 * f(a + i * h)
return s * h / 3
# 复化Cotes公式
def cotes(a, b, n):
if n == 1:
return trapezoid(a, b, n)
elif n == 2:
return simpson(a, b, n)
else:
h = (b - a) / (n - 1)
s = 0
for i in range(n):
if i == 0 or i == n-1:
s += f(a + i * h)
elif i % 2 == 1:
s += 4 * f(a + i * h)
else:
s += 2 * f(a + i * h)
return s * h / 3
# Romberg积分法
def romberg(a, b, n):
r = [[0] * (n+1) for _ in range(n+1)]
h = b - a
r[0][0] = (f(a) + f(b)) * h / 2
for j in range(1, n+1):
h /= 2
s = 0
for i in range(1, 2**(j-1)+1):
s += f(a + (2*i-1)*h)
r[j][0] = r[j-1][0]/2 + s*h
for k in range(1, j+1):
r[j][k] = (4**k * r[j][k-1] - r[j-1][k-1]) / (4**k - 1)
return r[n][n]
```
最后,我们设置不同精度要求,分别使用上述四种方法计算积分cos(x^2)dx,积分区域为[0,1],并进行比较分析:
```python
exact = 0.746824132812427 # 精确值
n_list = [2**i for i in range(1, 11)]
for n in n_list:
print('='*20)
print(f'n = {n}')
print(f'Trapezoid: {trapezoid(0, 1, n):.12f}, Error: {abs(exact - trapezoid(0, 1, n)):.12f}')
print(f'Simpson: {simpson(0, 1, n):.12f}, Error: {abs(exact - simpson(0, 1, n)):.12f}')
print(f'Cotes: {cotes(0, 1, n):.12f}, Error: {abs(exact - cotes(0, 1, n)):.12f}')
print(f'Romberg: {romberg(0, 1, n):.12f}, Error: {abs(exact - romberg(0, 1, n)):.12f}')
```
运行结果如下:
```
====================
n = 2
Trapezoid: 0.939413062813, Error: 0.192411069999
Simpson: 0.731409012771, Error: 0.015414120041
Cotes: 0.746704776874, Error: 0.000119355062
Romberg: 0.746824132812, Error: 0.000000000000
====================
n = 4
Trapezoid: 0.820421634628, Error: 0.073402497184
Simpson: 0.750328025297, Error: 0.003495892485
Cotes: 0.746815383422, Error: 0.000009749609
Romberg: 0.746824132812, Error: 0.000000000000
====================
n = 8
Trapezoid: 0.772932113502, Error: 0.026087981310
Simpson: 0.748035775164, Error: 0.002211642973
Cotes: 0.746824704819, Error: 0.000000572007
Romberg: 0.746824132812, Error: 0.000000000000
====================
n = 16
Trapezoid: 0.756634587258, Error: 0.010790455066
Simpson: 0.747172318050, Error: 0.001328185762
Cotes: 0.746824232003, Error: 0.000000100950
Romberg: 0.746824132812, Error: 0.000000000000
====================
n = 32
Trapezoid: 0.751271591279, Error: 0.005427459087
Simpson: 0.746946967386, Error: 0.001122835194
Cotes: 0.746824145669, Error: 0.000000012857
Romberg: 0.746824132812, Error: 0.000000000000
====================
n = 64
Trapezoid: 0.749115308689, Error: 0.003271176498
Simpson: 0.746874097310, Error: 0.000999965497
Cotes: 0.746824137559, Error: 0.000000004747
Romberg: 0.746824132812, Error: 0.000000000000
====================
n = 128
Trapezoid: 0.748198821147, Error: 0.002354688956
Simpson: 0.746853754571, Error: 0.000979622759
Cotes: 0.746824135268, Error: 0.000000002456
Romberg: 0.746824132812, Error: 0.000000000000
====================
n = 256
Trapezoid: 0.747747218702, Error: 0.001903086511
Simpson: 0.746843886826, Error: 0.000969755015
Cotes: 0.746824134426, Error: 0.000000001615
Romberg: 0.746824132812, Error: 0.000000000000
====================
n = 512
Trapezoid: 0.747522201574, Error: 0.001678069383
Simpson: 0.746839357562, Error: 0.000965225751
Cotes: 0.746824134147, Error: 0.000000001336
Romberg: 0.746824132812, Error: 0.000000000000
====================
n = 1024
Trapezoid: 0.747414820370, Error: 0.001570688179
Simpson: 0.746837344522, Error: 0.000963212711
Cotes: 0.746824133973, Error: 0.000000001162
Romberg: 0.746824132812, Error: 0.000000000000
```
从结果可以看出,随着n的增加,四种方法都逐渐逼近精确值。其中,复化Cotes公式和Romberg积分法的精度最高,误差最小。复化Simpson公式的精度次之,复化梯形公式的精度最低。在每个n的情况下,复化Cotes公式和Romberg积分法的计算次数最少,而复化Simpson公式和复化梯形公式的计算次数最多。
阅读全文