Seite - 50 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python
Bild der Seite - 50 -
Text der Seite - 50 -
50 2 BasicConstructions
b) Write another functionwith a loopwhere the user is asked for a time on the
interval Œ0;N and the corresponding (interpolated) y value is written to the
screen. The loop is terminatedwhen theusergivesanegative time.
c) Use the following measurements: 4:4;2:0;11:0;21:5;7:5, corresponding to
times0;1;:: :;4 (min),andcompute interpolatedvaluesat t D2:5and t D3:1
min. Perform separate hand calculations to check that the output from the
programis correct.
Filename:linear_interpolation.py.
Exercise2.16:Test straight linerequirement
Assume the straight line function f.x/ D 4xC 1. Write a script that tests the
“point-slope” form for this line as follows. Within a chosen interval on thex-axis
(for example, forx between 0 and 10), randomlypick 100 points on the line and
check if the followingrequirement is fulfilled foreachpoint:
f.xi/ f.c/
xi c Da; i D1;2;:: :;100;
wherea is theslopeof the lineandcdefinesafixedpoint .c;f.c//onthe line. Let
cD2here.
Filename:test_straight_line.py.
Exercise2.17:Fit straight line todata
Assume somemeasurements yi;i D 1;2;:: :;5 have been collected, once every
second.Your task is towrite aprogramthatfits a straight line to thosedata.
a) Makeafunctionthatcomputestheerrorbetweenthestraight linef.x/DaxCb
and themeasurements:
eD 5X
iD1 .axi Cb yi/2 :
b) Makeafunctionwitha loopwhereyougiveaandb, thecorrespondingvalueof
e iswritten to thescreen,andaplotof thestraight linef.x/DaxCb together
with thediscretemeasurements is shown.
Hint Tomake the plotting from the loop to work, youmay have to insert from
matplotlib.pylab import * at the topof the script and also addshow()after
theplot command in the loop.
c) Giventhemeasurements0:5;2:0;1:0;1:5;7:5,at times0;1;2;3;4,use thefunc-
tion inb) to interactivelysearch fora andb such thate isminimized.
Filename:fit_straight_line.py.
Remarks Fittingastraight line tomeasureddatapoints isaverycommontask. The
manual search procedure in c) can be automated by using amathematicalmethod
called themethod of least squares.
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