Page - 22 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Image of the Page - 22 -
Text of the Page - 22 -
22 1 TheFirstFewSteps
A Single Curve In Fig.1.1, we saw a nice and smooth curve, showing how the
height of a ball developed with time. The reader should realize that, even though
the curve is continuousand apparently smooth, it is generated from a collection of
pointsonly.That is, for thechosenpoints in time,wehavecomputedtheheight.For
times in between, we have computed nothing! So, in principle, we actually do not
know what the height is there. However, if only the time step between consecutive
height computations is “small enough”, the ball can not experience any significant
change in its state of motion. Thus, inserting straight lines between two and two
consecutivedata points will be a good approximation.This is exactly what Python
does,unlessotherwiseisspecified.With“many”datapoints,as inFig.1.1, thecurve
appearssmooth.
Wesawpreviously, inball_plot.py, howanarrayy (heights)couldbeplotted
againstanothercorrespondingarrayt (points in time)with thestatement
plt.plot(t, y)
Aplotcommandlike this isvery typicalandoften justwhatweprefer, forexample,
inourcase with the ball.
It is also possible, however, to plot an array without involving any second array
at all. With reference toball_plot.py, this means thaty could have been plotted
without any mention oft, and to do that, one could write the plot command rather
like
plt.plot(y)
Thecurvewouldthenhavelookedjust like theoneinFig.1.1,except that thex-axis
would span they array indices from 0 to 1000 instead of the correspondingpoints
in time(check it andsee foryourself).
Quickly testinga (minor)codechange
Let us take the opportunity here, to mention how many programmers
would go about to check the alternative plot command just mentioned. In
ball_plot.py, one would typically just comment out the original lines and
insert alternativecodefor these, i.e., as
#plt.plot(t, y)
#plt.xlabel(’t (s)’)
plt.plot(y)
plt.xlabel(’Array indices’)
One would then run the code and observe the impact of the change,which in
thiscase is themodifiedplotdescribedabove.
After running the modified code, there are, generally, two alternatives.
Shouldtheoriginalversionbekeptorshouldwemakethechangepermanent?
With the present ball example, most of us would prefer the original plot, so
we would change the code back to its original form (remember to check that
itworksasbefore!).
When the code change to test is more comprehensive, it is much better to
makea separatecopyof thewholeprogram,and thendo the testing there.
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