Seite - 101 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
Bild der Seite - 101 -
Text der Seite - 101 -
4.3 Exercises 101
c) Given the measurements 0.5,2.0,1.0,1.5,7.5, at times 0,1,2,3,4, use the
function inb) to interactivelysearchfora andb such thate isminimized.
Filename:fit_straight_line.py.
Remarks Fittingastraightlinetomeasureddatapointsisaverycommontask.The
manual search procedure in c) can be automated by using a mathematical method
called themethodof least squares.
Exercise4.13:FitSines toStraightLine
A lot of technology, especially most types of digital audio devices for processing
sound, is based on representinga signal of time as a sum of sine functions.Say the
signal is some functionf(t)on the interval [−π,π] (a moregeneral interval [a,b]
can easily be treated, but leads to slightly more complicated formulas). Instead of
workingwithf(t)directly,we approximatef by thesum
SN(t)= N∑
n=1 bnsin(nt), (4.1)
where thecoefficientsbn mustbeadjustedsuch thatSN(t) is agoodapproximation
tof(t). We shall in this exerciseadjustbn bya trial-and-errorprocess.
a) Makeafunctionsinesum(t, b) that returnsSN(t), giventhecoefficientsbn in
anarraybandtimecoordinatesinanarrayt.Note that ift is anarray, thereturn
value is alsoanarray.
b) Write a functiontest_sinesum() that calls sinesum(t, b) in a) and deter-
minesif thefunctioncomputesa testcasecorrectly.Astestcase, lettbeanarray
with values−π/2 andπ/4, chooseN = 2, andb1 = 4 andb2 =−3. Compute
SN(t)byhand toget referencevalues.
c) Makeafunctionplot_compare(f, N, M) thatplots theoriginalfunctionf(t)
together with the sum of sines SN(t), so that the quality of the approximation
SN(t) can be examined visually. The argument f is a Python function imple-
mentingf(t),N is thenumberof termsin thesumSN(t), andM is thenumberof
uniformlydistributed t coordinatesused toplotf andSN.
d) Write a functionerror(b, f, M) that returns a mathematical measure of the
error inSN(t)asanapproximationtof(t):
E= √∑
i (f(ti)−SN(ti))2,
where the ti values are M uniformly distributed coordinates on [−π,π]. The
arraybholds thecoefficients inSN andf is aPythonfunction implementing the
mathematical functionf(t).
e) Make a function trial(f, N) for interactively giving bn values and getting
a plot on the screen where the resulting SN(t) is plotted together with f(t).
The error in the approximationshould also be computedas indicated in d). The
Programming for Computations – Python
A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
- Titel
- Programming for Computations – Python
- Untertitel
- A Gentle Introduction to Numerical Simulations with Python 3.6
- Band
- Second Edition
- Autoren
- Svein Linge
- Hans Petter Langtangen
- Verlag
- Springer Open
- Datum
- 2020
- Sprache
- englisch
- Lizenz
- CC BY 4.0
- ISBN
- 978-3-319-32428-9
- Abmessungen
- 17.8 x 25.4 cm
- Seiten
- 356
- Schlagwörter
- Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
- Kategorie
- Informatik