102
4 Functions and the Writing of Code
argument f is a Python function for f (t) and N is the number of terms N in the
sum S N (t). The trial function can run a loop where the user is asked for the b n
values in each pass of the loop and the corresponding plot is shown. You must
find a way to terminate the loop when the experiments are over. Use M=500 in
the calls to plot_compare and error.
f) Choose f (t) to be a straight line f (t) =
1
π t on [−π, π]. Call trial(f, 3) and
try to find through experimentation some values b 1 , b 2 , and b 3 such that the sum
of sines S N (t) is a good approximation to the straight line.
g) Now we shall try to automate the procedure in f). Write a function that has
three nested loops over values of b 1 , b 2 , and b 3 . Let each loop cover the interval
[−1, 1] in steps of 0.1. For each combination of b 1 , b 2 , and b 3 , the error in the
approximation S N should be computed. Use this to find, and print, the smallest
error and the corresponding values of b 1 , b 2 , and b 3 . Let the program also plot f
and the approximation S N corresponding to the smallest error.
Filename: fit_sines.py.
Remarks
1. The function S N (x) is a special case of what is called a Fourier series. At the
beginning of the nineteenth century, Joseph Fourier (1768–1830) showed that
any function can be approximated analytically by a sum of cosines and sines.
The approximation improves as the number of terms (N) is increased. Fourier
series are very important throughout science and engineering today.
(a) Finding the coefficients b n is solved much more accurately in Exercise 6.12,
by a procedure that also requires much less human and computer work!
(b) In real applications, f (t) is not known as a continuous function, but function
values of f (t) are provided. For example, in digital sound applications,
music in a CD-quality WAV file is a signal with 44,100 samples of the
corresponding analog signal f (t) per second.
Open Access This chapter is licensed under the terms of the Creative Commons Attribution 4.0
International License (http://creativecommons.org/licenses/by/4.0/), which permits use, sharing,
adaptation, distribution and reproduction in any medium or format, as long as you give appropriate
credit to the original author(s) and the source, provide a link to the Creative Commons licence and
indicate if changes were made.
The images or other third party material in this chapter are included in the chapter’s Creative
Commons licence, unless indicated otherwise in a credit line to the material. If material is not
included in the chapter’s Creative Commons licence and your intended use is not permitted by
statutory regulation or exceeds the permitted use, you will need to obtain permission directly from
the copyright holder.
4 Functions and the Writing of Code
argument f is a Python function for f (t) and N is the number of terms N in the
sum S N (t). The trial function can run a loop where the user is asked for the b n
values in each pass of the loop and the corresponding plot is shown. You must
find a way to terminate the loop when the experiments are over. Use M=500 in
the calls to plot_compare and error.
f) Choose f (t) to be a straight line f (t) =
1
π t on [−π, π]. Call trial(f, 3) and
try to find through experimentation some values b 1 , b 2 , and b 3 such that the sum
of sines S N (t) is a good approximation to the straight line.
g) Now we shall try to automate the procedure in f). Write a function that has
three nested loops over values of b 1 , b 2 , and b 3 . Let each loop cover the interval
[−1, 1] in steps of 0.1. For each combination of b 1 , b 2 , and b 3 , the error in the
approximation S N should be computed. Use this to find, and print, the smallest
error and the corresponding values of b 1 , b 2 , and b 3 . Let the program also plot f
and the approximation S N corresponding to the smallest error.
Filename: fit_sines.py.
Remarks
1. The function S N (x) is a special case of what is called a Fourier series. At the
beginning of the nineteenth century, Joseph Fourier (1768–1830) showed that
any function can be approximated analytically by a sum of cosines and sines.
The approximation improves as the number of terms (N) is increased. Fourier
series are very important throughout science and engineering today.
(a) Finding the coefficients b n is solved much more accurately in Exercise 6.12,
by a procedure that also requires much less human and computer work!
(b) In real applications, f (t) is not known as a continuous function, but function
values of f (t) are provided. For example, in digital sound applications,
music in a CD-quality WAV file is a signal with 44,100 samples of the
corresponding analog signal f (t) per second.
Open Access This chapter is licensed under the terms of the Creative Commons Attribution 4.0
International License (http://creativecommons.org/licenses/by/4.0/), which permits use, sharing,
adaptation, distribution and reproduction in any medium or format, as long as you give appropriate
credit to the original author(s) and the source, provide a link to the Creative Commons licence and
indicate if changes were made.
The images or other third party material in this chapter are included in the chapter’s Creative
Commons licence, unless indicated otherwise in a credit line to the material. If material is not
included in the chapter’s Creative Commons licence and your intended use is not permitted by
statutory regulation or exceeds the permitted use, you will need to obtain permission directly from
the copyright holder.
