Page - 51 - in Programming for Computations β Python - A Gentle Introduction to Numerical Simulations with Python
Image of the Page - 51 -
Text of the Page - 51 -
2.7 Exercises 51
Exercise2.18: Fit sines tostraight line
A lot of technology, especiallymost types of digital audio devices for processing
sound, is basedonrepresentinga signalof timeasa sumofsine functions. Say the
signal is some functionf.t/on the interval Ε ; (amoregeneral interval Εa;b
can easily be treated, but leads to slightlymore complicated formulas). Instead of
workingwithf.t/directly,weapproximatef by the sum
SN.t/D NX
nD1 bnsin.nt/; (2.1)
where thecoefficientsbnmustbeadjustedsuch thatSN.t/ is agoodapproximation
tof.t/.Weshall in this exerciseadjustbnbya trial-and-errorprocess.
a) Make a functionsinesum(t, b) that returnsSN.t/, given the coefficientsbn
in an arrayb and time coordinates in an arrayt. Note that ift is an array, the
returnvalue is alsoanarray.
b) Write a functiontest_sinesum() that callssinesum(t, b) in a) anddeter-
mines if the function computes a test case correctly. As test case, let t be an
arraywith values =2 and =4, chooseN D 2, and b1 D 4 and b2 D 3.
ComputeSN.t/byhand toget referencevalues.
c) Makeafunctionplot_compare(f, N, M) thatplotstheoriginalfunctionf.t/
togetherwith the sumof sinesSN.t/, so that the quality of the approximation
SN.t/ can be examined visually. The argumentf is a Python function imple-
mentingf.t/, N is the numberof terms in the sumSN.t/, andM is thenumber
ofuniformlydistributed t coordinatesused toplotf andSN .
d) Write a functionerror(b, f, M) that returns amathematicalmeasure of the
error inSN.t/asanapproximation tof.t/:
E D sX
i .f.ti/ SN.ti//2;
where the ti values areM uniformly distributed coordinates on Ε ; . The
arraybholds thecoefficients inSN andf isaPythonfunctionimplementingthe
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/.
Theerror in theapproximationshouldalsobecomputedas indicated ind). The
argumentf is aPythonfunction forf.t/ andN is thenumberof termsN in the
sumSN.t/. Thetrialfunctioncanrunaloopwheretheuser isaskedforthebn
values in eachpass of the loop and the correspondingplot is shown. Youmust
find away to terminate the loopwhen the experiments are over. UseM=500 in
thecalls toplot_compareanderror.
Hint To make this part of your program work, you may have to insert from
matplotlib.pylab import * at the top and also add show() after the plot
command in the loop.
back to the
book Programming for Computations β Python - A Gentle Introduction to Numerical Simulations with Python"
Programming for Computations β Python
A Gentle Introduction to Numerical Simulations with Python
- Title
- Programming for Computations β Python
- Subtitle
- A Gentle Introduction to Numerical Simulations with Python
- Authors
- Svein Linge
- Hans Petter Langtangen
- Publisher
- Springer Open
- Date
- 2016
- Language
- English
- License
- CC BY-NC 4.0
- ISBN
- 978-3-319-32428-9
- Size
- 17.8 x 25.4 cm
- Pages
- 248
- Keywords
- Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
- Category
- Informatik