Web-Books
in the Austria-Forum
Austria-Forum
Web-Books
Informatik
Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Page - 101 -
  • User
  • Version
    • full version
    • text only version
  • Language
    • Deutsch - German
    • English

Page - 101 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition

Image of the Page - 101 -

Image of the Page - 101 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition

Text of the Page - 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
back to the  book Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition"
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
Web-Books
Library
Privacy
Imprint
Austria-Forum
Austria-Forum
Web-Books
Programming for Computations – Python