THE UNIVERSITY OF BRITISH COLUMBIA
 
Physics 210 Assignment # 5:
 
WRESTLE WITH PYTHON: SOLUTIONS
 
Tue. 30 Sep. 2008 - finish by Tue. 07 Oct.

Note that there is no hardcopy of PDF file for this Assignment, because almost everything you need to know is in the form of links to various URLs -- for which dead trees are almost completely useless. So this is it!

This is also [meant to be] a rather short assignment, in recognition of your need to get to work on your Project proposal, to be presented  next week. (More on that later.)   So please don't make this take longer than it needs to.   :-)

Your Mission is as follows (this message will not self-destruct, sorry!):


Do what you did in Assignment 4, only this time use the full-featured programming language Python.

Then get right to work on your Project Proposal Presentation!


OK, that sounds simple, but you are going to need some guidance, even if you have already done some Python programming, because saying, "I know some Python, this should be easy!" is like saying, "Oh, you're from Toronto, do you know my friend George there?" Python is huge, diverse, mysterious and powerful; no one really "knows Python" -- but you can get used to a small subset of its idiosyncracies in certain contexts. After that, how far you take it is up to you. You should know, however, that it it probably the most popular language for writing system software, and that armies of Python programmers have devoted enormous efforts to building emulators in Python for complex and esoteric programming environments like MatLab.

There are usually several differences between established programming environments and their emulators:

  1. The emulator is almost always free and Open Source.

  2. The emulator is usually slower than the "real thing". However, in the case of Python's MatLab emulator, "PyLab", this may not be the case; first because both are scripted languages, and second because both have extensive pre-compiled (fast) components which the scripts call. You may want to try a few "benchmarks" to test this claim.

  3. The established programming environment almost always has better (i.e. more convenient, better organized and comprehensible to novices) documentation. Although there is some decent documentation for PyLab and PyX (a fancy PostScript graphics package with LaTeX support for fancy text), it is not bundled with the basic Python help files and must be hunted down with Google in most cases. The main problem here is that there is too much documentation, duplicated at many levels from the utter novice to the advanced expert, and much of it highly idiosyncratic, reflecting the level of understanding of the programmer who wrote it and can't understand why the syntax isn't obvious to everyone.

To save you some time and effort, I have done a bit of hunting myself, looking for well-written documentation that seems at an appropriate and useful level for most PHYS 210 students; here are the results:

Python: Object-Oriented Programming (PDF)
Verbose Tutorial for Beginners
Alphabetical Glossary of Terms
NumPy:
(You won't need NumPy
for this Assignment.)
Tentative NumPy Tutorial
NumPy for MatLab Users [Mathesaurus]
NumPy for MatLab Users [SciPy.org]
NumPy Functions by Category
matplotlib: matplotlib.pyplot API
errorbar method in pyplot [scienceoss]
errorbar method in pyplot [official]
PyX: PyX Examples
Titus' PyX Tutorial for Gnuplot Users

I expect that, by browsing these links, thinking about what you read, "borrowing" examples, trying them out and modifying them*, you will be able to get a nice plot like the one below, except using either pyplot or PyX instead of µView.

* Remember the 11th Commandment: "Thou shalt not change more than one independent parameter at a time before testing to see the effect." Even though Python (unlike PHP on a public Website) does provide lots (and lots!) of diagnostic messages, it helps to know which ones come from which changes! Be prepared to make many, many trials.

As usual, when your script(s) work perfectly and you have nice plot files to show, put them all in your /home2/phys210/$USER/a05/ directory. If you have any comments or suggestions (in a README.txt file) those are welcome too, but not required.


ANSWERS:

The script dbpylab.py generates the desired figure pretty well using the pylab method of the matplotlib module in Python. To run it, make it executable and then enter "./dbpylab.py" in its directory. The resultant plot is interactive, as in MatLab.


The script dbpyplot.py generates the desired figure pretty well using the pyplot method of the matplotlib module in Python. To run it, make it executable and then enter "./dbpyplot.py" in its directory. The resultant plot is interactive, as in MatLab. Note that there is virtually no difference between dbpyplot.py and dbpylab.py!


The script dbpyx.py generates the desired figure pretty well using the graph method of the pyx module in Python. To run it, make it executable, copy db.dat into the same directory and then enter "./dbpyx.py db.dat" to create the plot files data-pyx.eps and data-pyx.pdf there. (This one is not interactive like MatLab.)



Jess H. Brewer
Last modified: Sun Oct 3 17:01:22 PDT 2010