Let us keep working on the ball launching problem but let's ask the opposite question: at what time does the ball pass through some vertical position y? The analytic solution is:
If we try to compute the square root in ipython, we get an error message. Ipython cannot compute square roots. The square root function in python is included in a ``module'', a collection of functions that is not part of the standard python but that can be imported to inrease the functionality. In our case the module we want to import is the math module. There are several ways in which you can import modules in python. One can import the whole module by typing:
>> import math
In this case the square root of 5 can be computed as:
>> import math >> result = math.sqrt(5) >> print result
Since the function sqrt is part of the math module, we have to call it with the name of the module attached. Alternatively, if one does not want to carry the name of the module, the program can be changed to:
>> from math import * >> result = sqrt(5) >> print result
This yields a lighter notation. However, when importing more than one module, you run in the possibility of importing two different functions with the same name but different effects. A good solution is to rename the module with a lighter notation:
>> import math as m >> result = m.sqrt(5) >> print result
EXERCISE: Write a routine that asks you for the initial velocity and vertical position and computes the time according to the equation above. Make it pretty.
Going back to our ball thrown in the air, we may want to compute the position of the ball at many times and make a plot of the position y versus time t. Looking ahead to plotting, we want to generate a series of positions at discrete times: y(tn), where tn represents a series of times, t0, t1, t2.... You can generate such a list in Python with the command
>> t = [0,1,2,3,4]but this is merely a list of numbers in Python. Instead, we would like Python to treat it in a mathematical sense as a vector. For example, if we sum two lists we get
>> t+t = [0,1,2,3,4,0,1,2,3,4]but if we sum two vectors we expect
>> t+t = [0,2,4,6,8]In order to do that we need to import the numpy module. Here is a NumPy Tutorial. There are several ways of creating an array of time:
>> import numpy as n >> t1 = n.array([1.,2,3,4,5,6]) >> t2 = n.arange(1.,7,1) >> t3 = n.linspace(1,6,num=6)
EXERCISE: Try each of these methods and verify that they are indeed identical.
If you do not know exactly how to use a function inside a module, help can be obtained. For example, to obtain help on the n.arange function, you type:
>> help(n.arange)
An advantage of operating with arrays rather than lists is that Python can do mathematical operations with arrays, without the need of cycling over the elements of the array one by one. For example, to compute the position of the ball at times t=1,2,3,4,5,6,7,8,9 seconds, one simply writes:
# this routine computes the vertical position of an object at several times import numpy as n t = n.linspace(0.,10.,11) g = 9.8 # the acceleration of gravity [m/s**2] v0 = raw_input('Initial velocity: ') # input the initial speed of the ball [m/s] v0 = float(v0) y = v0*t - g/2.*t**2 print 'The position of the ball at time t = ',t,'s is y = ',y,'m'
Note that nothing has changed in the line where we compute y. However, since t is an array, Python interprets the assignment statement y = v0 ... as implying that y is an array as well. The resulting print statement is then writing out both arrays of numbers, although the output is not well formatted. We will learn in the next lesson how to output the results of array calculations (to a file).
As a final task, we want to plot the position of the ball as a function of time. To do that, we first need to compute the position y in a fairly high-resolution time array. Once we have the position as a function of time t for many times, we use the matplotlib module to make the plot. Here is a matplotlib tutorial.
Pylab is a mode of the IPython interpreter that incorporates the matplotlib package. If you are using the IPython shell, you only need to include
from matplotlib.pylab import *to include the plotting support and numpy (which includes math) that you need for this class. The additional lines needed to generate a plot of y(t) are
plot(t,y) title('Position of the ball') xlabel('Time (seconds)') ylabel('Position (meters)') show()
ASSIGNMENT: Write a routine that prompts for the initial velocity and angle of a projectile and then plots the trajectory (on an X-Y plot).