Seite - 99 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
Bild der Seite - 99 -
Text der Seite - 99 -
4.3 Exercises 99
Exercise4.6:FunctionforAreaofaRectangle
Write a program that computes the area A = bc of a rectangle. The values of b
and c should be user input to the program. Also, write the area computation as
a function that takes b and c as input parameters and returns the computed area.
Let the program print the result to screen along with an appropriate text. Run the
programwithb=2andc=3 toconfirmcorrectprogrambehavior.
Filename:function_area_rectangle.py.
Exercise4.7:Averageof Integers
Writeaprogramthatgetsan integerN>1fromtheuserandcomputestheaverage
of all integers i = 1,.. .,N. The computation should be done in a function that
takesN as input parameter. Print the result to the screen with an appropriate text.
Run theprogramwithN =5andconfirmthatyouget thecorrectanswer.
Filename:average_1_to_N.py.
Exercise4.8:When DoesPythonCheckFunctionSyntax?
You are told that, when Python reads a functiondefinition for the first time, it does
notexecute the function,but still checks thesyntax.
Now,youcomeupwith somecode lines toconfirmthat this is thecase.
Hint You may, for example, use a print command and a deliberate syntax error in
a modification ofball_function.py (note that the modified code is one of those
quick “one-time” tests you might make for yourself, meant to be deleted once you
have theanswer).
Filename:when_check_function_syntax.py.
Exercise4.9:Find CrossingPointsofTwo Graphs
Consider two functionsf(x)=x andg(x)=x2 on the interval [−4,4].
Write a program that, by trial and error, finds approximately for which values
of x the two graphs cross, i.e., f(x) = g(x). Do this by considering N equally
distributedpointsontheinterval,ateachpointcheckingwhether |f(x)−g(x)|< ,
where is somesmallnumber.LetN and beuser input to theprogramandlet the
resultbeprinted to screen.RunyourprogramwithN =400and =0.01.Explain
the output from the program. Finally, try also other values ofN, keeping the value
of fixed.Explainyourobservations.
Filename:crossing_2_graphs.py.
Exercise4.10:Linear Interpolation
Some measurements yi, i = 0,1,.. .,N, of a quantity y have been collected
regularly, once every minute, at times ti = i, i = 0,1,.. .,N. We want to find
the valuey in between the measurements, e.g., at t = 3.2 min. Computing suchy
values is called interpolation.
Letyourprogramuse linearinterpolation tocomputeybetweentwoconsecutive
measurements:
1. Find i such that ti ≤ t≤ ti+1.
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