Page - 102 - in Programming for Computations β Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Image of the Page - 102 -
Text of the Page - 102 -
102 4 FunctionsandtheWritingofCode
argumentf is a Python function forf(t) andN is the number of termsN in the
sumSN(t).Thetrial functioncanruna loopwhere theuser isaskedfor thebn
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. UseM=500 in
thecalls toplot_compareanderror.
f) Choosef(t) tobeastraight linef(t)= 1
Ο t on [βΟ,Ο].Calltrial(f, 3)and
try tofind throughexperimentationsomevaluesb1,b2, andb3 such that thesum
ofsinesSN(t) is a goodapproximationto thestraight line.
g) Now we shall try to automate the procedure in f). Write a function that has
threenested loopsovervaluesofb1,b2, andb3. Let each loopcover the interval
[β1,1] in steps of 0.1. For each combination ofb1, b2, andb3, the error in the
approximationSN should be computed. Use this to find, and print, the smallest
errorand thecorrespondingvaluesofb1,b2, andb3.Let theprogramalsoplotf
and theapproximationSN correspondingto thesmallest error.
Filename:fit_sines.py.
Remarks
1. The functionSN(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 arevery important throughoutscienceandengineering today.
(a) Finding thecoefficientsbn is solvedmuchmoreaccurately inExercise6.12,
byaprocedure thatalso requiresmuch less humanandcomputerwork!
(b) Inrealapplications,f(t) isnotknownasacontinuousfunction,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
correspondinganalogsignalf(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,distributionandreproduction inanymediumor format, as longasyougiveappropriate
credit to theoriginal author(s) and thesource, provide a link to theCreative Commons licenceand
indicate ifchanges 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 orexceeds thepermitted use, you willneed toobtain permission directly from
thecopyright holder.
Programming for Computations β Python
A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
- Title
- Programming for Computations β Python
- Subtitle
- A Gentle Introduction to Numerical Simulations with Python 3.6
- Volume
- Second Edition
- Authors
- Svein Linge
- Hans Petter Langtangen
- Publisher
- Springer Open
- Date
- 2020
- Language
- English
- License
- CC BY 4.0
- ISBN
- 978-3-319-32428-9
- Size
- 17.8 x 25.4 cm
- Pages
- 356
- Keywords
- Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
- Category
- Informatik