172
6 Computing Integrals and Testing Code
exercise). Therefore, we are forced to compute the integral by numerical methods.
Compute a result that is right to four digits.
Hint Use ideas from Exercise 6.9.
Filename: integrate_x2x.py.
Exercise 6.11: Integrate Products of Sine Functions
In this exercise we shall integrate
I j,k =
π
−π
sin(j x) sin(kx)dx,
where j and k are integers.
a) Plot sin(x) sin(2x) and sin(2x) sin(3x) for x ∈ [−π, π] in separate plots. Explain
why you expect
π
−π sin x sin 2x dx = 0 and
π
−π sin 2x sin 3x dx = 0.
b) Use the trapezoidal rule to compute I j,k for j = 1, . . . , 10 and k = 1, . . . , 10.
Filename: products_sines.py.
Exercise 6.12: Revisit Fit of Sines to a Function
This is a continuation of Exercise 4.13. The task is to approximate a given function
f (t) on [−π, π] by a sum of sines,
S N (t) =
N
n=1
b n sin(nt) .
(6.31)
We are now interested in computing the unknown coefficients b n such that S N (t) is
in some sense the best approximation to f (t). One common way of doing this is to
first set up a general expression for the approximation error, measured by “summing
up” the squared deviation of S N from f :
E =
π
−π
(S N (t) − f (t))
2 dt .
We may view E as a function of b 1 , . . . , b N . Minimizing E with respect to
b 1 , . . . , b N will give us a best approximation, in the sense that we adjust b 1 , . . . , b N
such that S N deviates as little as possible from f .
6 Computing Integrals and Testing Code
exercise). Therefore, we are forced to compute the integral by numerical methods.
Compute a result that is right to four digits.
Hint Use ideas from Exercise 6.9.
Filename: integrate_x2x.py.
Exercise 6.11: Integrate Products of Sine Functions
In this exercise we shall integrate
I j,k =
π
−π
sin(j x) sin(kx)dx,
where j and k are integers.
a) Plot sin(x) sin(2x) and sin(2x) sin(3x) for x ∈ [−π, π] in separate plots. Explain
why you expect
π
−π sin x sin 2x dx = 0 and
π
−π sin 2x sin 3x dx = 0.
b) Use the trapezoidal rule to compute I j,k for j = 1, . . . , 10 and k = 1, . . . , 10.
Filename: products_sines.py.
Exercise 6.12: Revisit Fit of Sines to a Function
This is a continuation of Exercise 4.13. The task is to approximate a given function
f (t) on [−π, π] by a sum of sines,
S N (t) =
N
n=1
b n sin(nt) .
(6.31)
We are now interested in computing the unknown coefficients b n such that S N (t) is
in some sense the best approximation to f (t). One common way of doing this is to
first set up a general expression for the approximation error, measured by “summing
up” the squared deviation of S N from f :
E =
π
−π
(S N (t) − f (t))
2 dt .
We may view E as a function of b 1 , . . . , b N . Minimizing E with respect to
b 1 , . . . , b N will give us a best approximation, in the sense that we adjust b 1 , . . . , b N
such that S N deviates as little as possible from f .
