Page - 52 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python
Image of the Page - 52 -
Text of the Page - 52 -
52 2 BasicConstructions
f) Choosef.t/ to be a straight line f.t/ D 1 t on Œ ; . Call trial(f, 3)
and try tofind throughexperimentationsomevaluesb1,b2, andb3 such that the
sumofsinesSN.t/ is agoodapproximation to the straight line.
g) Nowwe shall try to automate the procedure in f). Write a function that has
threenested loopsovervaluesofb1,b2, andb3. Leteach loopcover the interval
Œ 1;1 in steps of 0.1. For each combinationofb1, b2, andb3, the error in the
approximationSN shouldbecomputed.Use this tofind, andprint, the smallest
error and the correspondingvaluesofb1,b2, andb3. Let the programalso plot
f and theapproximationSN corresponding to thesmallest error.
Filename:fit_sines.py.
Remarks
1. The function SN.x/ is a special case of what is called a Fourier series. At
thebeginningof the19th century, JosephFourier (1768-1830)showed that any
function can be approximated analytically by a sumof cosines and sines. The
approximationimprovesas thenumberof terms(N) is increased. Fourier series
arevery important throughoutscienceandengineering today.
(a)Finding thecoefficientsbn is solvedmuchmoreaccurately inExercise3.12,
byaprocedure that also requiresmuch lesshumanandcomputerwork!
(b)In real applications, f.t/ is not known as a continuous function, but func-
tionvaluesoff.t/areprovided. Forexample, indigital soundapplications,
music in aCD-qualityWAVfile is a signalwith 44,100 samples of the cor-
respondinganalogsignalf.t/per second.
Exercise2.19:Countoccurrencesofa string inastring
In theanalysisofgenesoneencountersmanyproblemsettings involvingsearching
for certain combinations of letters in a long string. For example, we may have
astring like
gene = ’AGTCAATGGAATAGGCCAAGCGAATATTTGGGCTACCA’
Wemaytraverse this string, letterby letter, by the for loopfor letter in gene.
The lengthof the string is givenbylen(gene), so an alternative traversal over an
index i is for i in range(len(gene)). Letter number i is reached through
gene[i], and a substring from index i up to, but not including j, is created by
gene[i:j].
a) Write a functionfreq(letter, text) that returns the frequencyof the letter
letter in the string text, i.e., the number of occurrences ofletterdivided
by the lengthoftext. Call the function todetermine the frequencyofC andG
in thegenestringabove.Compute the frequencybyhand too.
b) Write a function pairs(letter, text) that counts howmany times a pair
of the letterletter (e.g.,GG) occurswithin the stringtext. Use the function
to determine howmany times the pair AA appears in the string gene above.
Performamanualcounting too tocheck theanswer.
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