Web-Books
im Austria-Forum
Austria-Forum
Web-Books
Informatik
Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python
Seite - 51 -
  • Benutzer
  • Version
    • Vollversion
    • Textversion
  • Sprache
    • Deutsch
    • English - Englisch

Seite - 51 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python

Bild der Seite - 51 -

Bild der Seite - 51 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python

Text der Seite - 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.
zurück zum  Buch Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python"
Programming for Computations – Python A Gentle Introduction to Numerical Simulations with Python
Titel
Programming for Computations – Python
Untertitel
A Gentle Introduction to Numerical Simulations with Python
Autoren
Svein Linge
Hans Petter Langtangen
Verlag
Springer Open
Datum
2016
Sprache
englisch
Lizenz
CC BY-NC 4.0
ISBN
978-3-319-32428-9
Abmessungen
17.8 x 25.4 cm
Seiten
248
Schlagwörter
Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
Kategorie
Informatik
Web-Books
Bibliothek
Datenschutz
Impressum
Austria-Forum
Austria-Forum
Web-Books
Programming for Computations – Python