Exercise 1.29: Simpson’s Rule is a more accurate
method of numerical integration than the method illustrated above. Using
Simpson’s Rule, the integral of a function f between a and b is
approximated as
h
3
(
y
0
+
4
y
1
+
2
y
2
+
4
y
3
+
2
y
4
+
⋯
+
2
y
n
−
2
+
4
y
n
−
1
+
y
n
)
,
where h
=
(
b
−
a
)
/
n, for some even integer n, and
y
k
=
f
(
a
+
k
h
). (Increasing n increases the
accuracy of the approximation.) Define a procedure that takes as arguments
f, a, b, and n and returns the value of the integral, computed
using Simpson’s Rule. Use your procedure to integrate cube between 0
and 1 (with n
=
100 and n
=
1000), and compare the results to those of
the integral procedure shown above.
练习 1.29:辛普森法则 (Simpson's Rule) 是一种比上面所示方法更精确的数值积分方法。用辛普森法则,函数 f 在 a 到 b 之间的积分近似为
h/3 (y₀ + 4y₁ + 2y₂ + 4y₃ + 2y₄ + ⋯ + 2y_{n−2} + 4y_{n−1} + yₙ),
其中 h = (b−a)/n,n 为某个偶整数,y_k = f(a + kh)。(增大 n 可提高近似精度。)定义一个过程,接受 f、a、b 和 n 作为参数,并用辛普森法则返回积分的值。用你的过程对 cube 在 0 到 1 之间进行积分(取 n = 100 和 n = 1000),并将结果与上面的 integral 过程进行比较。