Math 450 Laboratory 2

Calculating solutions with python

Friday, 11:15-12:05, 2016-01-22, Osmond 216

Goals of this laboratory...

Start up Canopy, open the editor, and work through examples below to see what you can learn.

Basic math

In Laboratory 1, you saw a little about how to do math calculations in python. But let's go through the basics to make sure we're all on the same page.


The python language itself (like all good modern languages) is simple, using only about 30 words. Python's power comes from the rich collection of well-documented "modules" that are available to perform complex tasks. A module is python script that contains functions (and classes) to perform related tasks, like a library in C. Perhaps the simplest example of this is the "math" module.

You can load a module into your python interpretter's global namespace and shell scripts with the "import" keyword.

>>> import math

You can use help to see the contents of the module you have imported.

>>> help(math)

To use one of the functions from the module, you put the modules name as a prefix, like

>>> math.exp(0.)
>>> math.pi

Function names can also be imported directly into the working namespace, although this can be dangerous and is discouraged.

>>> from math import sin, pi, exp, log
>>> sin(0), sin(pi/2)
>>> exp(0), exp(1), log(exp(1))

All other modules work the same way, and provide a variety of capabilities. Here are some common modules.

Scientific computing modules

For this next part of the lab, it will be useful to start with a clean environment, so go up to the "Run" menu and select "Restart Kernel".

Now, there is standard stack of modules we use for scientific computing called the "scipy stack". Parts of this stack of modules are automatically loaded into Canopy's global namespace. These are numpy (which creates fast arrays), scipy (which supplies all sorts of functions and algorithms) for calculations, and matplotlib (for making plots and pictures). If you know matlab already, there are many similar things to python, but also some differences. If you write a script or use the standard python interprettor, you can load these modules into the global namespace with the following three lines of code.

>>> from numpy import *
>>> from scipy import *
>>> from matplotlib.pyplot import *

Now, let's see how we can use the scipy stack to solve some problems you have seen before.


The scipy stack has a useful set of plotting tools. For a simple example, here is a use of the rational parameteric form of a circle.

>>> t = linspace(-10,10,64)
>>> x = (t*t-1)/(t*t+1)
>>> y = 2*t/(t*t+1)
>>> plot(x, y, 'ro-', cos(t), sin(t), 'k:')

The linspace(-10,10,64) function creates an array of 64 evenly spaced points from -10 to 10, including the endpoints. The extra strings in the plot command specify how to draw a line. Can you guess what each character means?

To clear the figure, you can use the command clf(). Of course, plots without labels are often more confusing than helpful. We can add titles and labels with the functions title(), ylabel(), xlabel(), and text.

>>> xlabel('x-values')
>>> title('A circle', fontsize=25)

To save your figure to a png image file with the "savefig" command.

>>> cd Desktop
>>> savefig('mycircle.png')

Of course, vector formats for images are better, so we usually use portable document format (.pdf), encapsulated postscript (.eps), or scalable vector graphic format (.svg).

For a second example, let's see if we can draw a cycloid.

>>> figure(2)
>>> clf()
>>> r = 1.
>>> theta =  linspace(0, 8 * pi, 257)
>>> x = r*(t -.sin(t))
>>> y = r*(1 - cos(t))
>>> plot(x,y,'r-')

The figure function is used to create a new figure or select an old figure for the plot. `

For more information, see Matplotlib's gallery

Matplotlib will also allow us to construct animations. Check out this animation of a double pendulum and run it as a script in Canopy. How does the motion change as you change the mass of the second pendulum weight? Can you construct your own animation of the cycloid like this one?