6.8 Exercises
173
Minimization of a function of N variables, E(b 1 , . . . , b N ) is mathematically
performed by requiring all the partial derivatives to be zero:
∂E
∂b 1
= 0,
∂E
∂b 2
= 0,
. . .
∂E
∂b N
= 0 .
a) Compute the partial derivative ∂E/∂b 1 and generalize to the arbitrary case
∂E/∂b n , 1 ≤ n ≤ N.
b) Show that
b n =
1
π
π
−π
f (t) sin(nt) dt .
c) Write a function integrate_coeffs(f, N, M) that computes b 1 , . . . , b N by
numerical integration, using M intervals in the trapezoidal rule.
d) A remarkable property of the trapezoidal rule is that it is exact for integrals
π
−π sin nt dt (when subintervals are of equal size). Use this property
to create a function test_integrate_coeff to verify the implementation of
integrate_coeffs.
e) Implement the choice f (t) =
1
π t as a Python function f(t) and call
integrate_coeffs(f, 3, 100) to see what the optimal choice of b 1 , b 2 , b 3
is.
f) Make a function plot_approx(f, N, M, filename) where you plot f(t)
together with the best approximation S N as computed above, using M intervals
for numerical integration. Save the plot to a file with name filename.
g) Run plot_approx(f, N, M, filename) for f (t) =
1
π t for N = 3, 6, 12, 24.
Observe how the approximation improves.
h) Run plot_approx for f (t) = e −(t −π) and N = 100. Observe a fundamental
problem: regardless of N, S N (−π) = 0, not e 2π ≈ 535. (There are ways to fix
this issue.)
Filename: autofit_sines.py.
Exercise 6.13: Derive the Trapezoidal Rule for a Double Integral
Use ideas in Sect. 6.7.1 to derive a formula for computing a double integral
b
a
d
c f (x, y)dydx by the trapezoidal rule. Implement and test this rule.
Filename: trapezoidal_double.py.
Précédent

- 194/350

Suivant