分别使用复合梯形公式和复合辛普森公式计算如下积分:
∫
2
6
2
x
4
+
x
2
d
x
?
\int_{2}^{6} \frac{2x}{4+x^2}dx\,
∫26?4+x22x?dx 并于该积分的准确值进行比较。注意,采用复合梯形公式和复合辛普森公式时,所使用的等分步长为h=0.5。 以下是代码,用python实现:
fx1 = []
fx2 = []
sx = []
sx2 = []
for x1 in (2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6):
y = (2 * x1) / (4 + x1 ** 2)
print("{0:.6f}".format(y))
fx1.append(float("{0:.6f}".format(y)))
print(fx1)
for x2 in (2.25, 2.75, 3.25, 3.75, 4.25, 4.75, 5.25, 5.75):
z = (2 * x2) / (4 + x2 ** 2)
print("{0:.6f}".format(z))
fx2.append(float("{0:.6f}".format(z)))
print(fx2)
print("-----------------------------------------")
s = 0
for xk in fx1[1: 8]:
s = s + xk
print("{0:.6f}".format(s))
sx2.append(float("{0:.6f}".format(s)))
print(sx2)
print(s)
print("-----------------------------------------")
h = 0.5
Tn = (h / 2) * (fx1[0] + 2 * s + fx1[8])
print(Tn)
print("-----------------------------------------")
s2 = 0
for xh in fx2:
s2 = s2 + xh
print("{0:.6f}".format(s2))
sx2.append(float("{0:.6f}".format(s2)))
print(sx2)
print(s2)
print("-----------------------------------------")
h = 0.5
Sn = (h / 6) * (fx1[0] + 4 * s2 + 2 * s + fx1[8])
print(Sn)
通过 https://www.wolframalpha.com 进行积分准确值计算。
|