22
1 The First Few Steps
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 continuous and apparently smooth, it is generated from a collection of
points only. That is, for the chosen points in time, we have computed the height. 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
consecutive data points will be a good approximation. This is exactly what Python
does, unless otherwise is specified. With “many” data points, as in Fig. 1.1, the curve
appears smooth.
We saw previously, in ball_plot.py, how an array y (heights) could be plotted
against another corresponding array t (points in time) with the statement
plt.plot(t, y)
A plot command like this is very typical and often just what we prefer, for example,
in our case with the ball.
It is also possible, however, to plot an array without involving any second array
at all. With reference to ball_plot.py, this means that y could have been plotted
without any mention of t, and to do that, one could write the plot command rather
like
plt.plot(y)
The curve would then have looked just like the one in Fig. 1.1, except that the x-axis
would span the y array indices from 0 to 1000 instead of the corresponding points
in time (check it and see for yourself).
Quickly testing a (minor) code change
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 alternative code for 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
this case is the modified plot described above.
After running the modified code, there are, generally, two alternatives.
Should the original version be kept or should we make the change permanent?
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
it works as before!).
When the code change to test is more comprehensive, it is much better to
make a separate copy of the whole program, and then do the testing there.
1 The First Few Steps
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 continuous and apparently smooth, it is generated from a collection of
points only. That is, for the chosen points in time, we have computed the height. 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
consecutive data points will be a good approximation. This is exactly what Python
does, unless otherwise is specified. With “many” data points, as in Fig. 1.1, the curve
appears smooth.
We saw previously, in ball_plot.py, how an array y (heights) could be plotted
against another corresponding array t (points in time) with the statement
plt.plot(t, y)
A plot command like this is very typical and often just what we prefer, for example,
in our case with the ball.
It is also possible, however, to plot an array without involving any second array
at all. With reference to ball_plot.py, this means that y could have been plotted
without any mention of t, and to do that, one could write the plot command rather
like
plt.plot(y)
The curve would then have looked just like the one in Fig. 1.1, except that the x-axis
would span the y array indices from 0 to 1000 instead of the corresponding points
in time (check it and see for yourself).
Quickly testing a (minor) code change
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 alternative code for 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
this case is the modified plot described above.
After running the modified code, there are, generally, two alternatives.
Should the original version be kept or should we make the change permanent?
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
it works as before!).
When the code change to test is more comprehensive, it is much better to
make a separate copy of the whole program, and then do the testing there.
