I expect to write computer programs to find and graph solutions to some homework problems. So, which programming language should I use? What are the selection criteria?
For starters, the language should:
- be inexpensive ( free is always good for students and retirees )
- be easy to install
- be easy to learn
- have a productive development environment
- have a large and growing community of users and supporters
- be mature
- have rich scientific, math and plotting libraries
- have cross-platform support ( Linux, Windows, iOS, etc. )
I chose the Python language because it satisfies all of the above.
According to Wikipedia, Python was conceived in the late 1980s and its implementation was started in December 1989. Python 2.0 was released on 16 October 2000, with many major new features. With this release the development process was changed and became more transparent and community-backed. Python 3.0 a major, backwards-incompatible release, was released on 3 December 2008 after a long period of testing. Latest version is Python 3.4.2. Since Python 2.x is incompatible with Python 3.x, there is a passionate on-going debate about which version one should use. I opted for version 3.x because I don’t have an investment in 2.x code. In a sense, version 2.x is the Windows XP of the Python world.
Python is totally free to use. Need I say any more about the price?
Python itself is simple to install. The problem is to install its packages for scientific programming. The esiest way to install Python and its scientific packages (i.e. libraries) is via a Python distribution. Anaconda from Continuum Analytics and Canopy from Enthought are distributions that include the most commonly used Python scientific packages. Both vendors offer Linux, Mac and Windows versions. I chose Anaconda because its free version appeared to include more packages than Canopy’s free Express version. The whole installation process took about 15 minutes. This is infinitely easier than first installing Python and then installing the scientific packages separately.
Since Python uses constructs which are common to many programming languages, I learned the basics of Python very quickly. I signed up for an on-line class from Udacity titled “Intro to Computer Science” by Prof David Evans. Python was taught and used to program the homework problems. It was a great class and it was free. Udacity recently upgraded their offerings and started charging for them. Fortunately, the instructor lectures and homework problems are still free for this class. I also purchased an excellent beginning to intermediate level book titled “The Quick Python Book” by Naomi Ceder.
When you have questions, your best friend is Google. It will often take you to StackOverflow.com where Python developers have probably already answered your question. This is a lot easier than digging through the documentation.
My preferred Python development environment is the IPython Notebook. It is a quantum leap forward from the primitive character based Python command line interpreter, Python shell and IPython shell that were used before. The notebook is a web-based interactive computational environment where you can combine code execution, text, mathematics, plots and rich media, even YouTube videos, into a single document. Notebooks can be shared and executed by anyone with IPython. Notebooks can also be downloaded as html or pdf files. IPython Notebook makes Python a way more compelling language for this project.
The real power of Python lies in its large collection of add-on packages ( aka libraries ). There are literally tens of thousands of domain specific packages in the Python ecosystem. For a scientist or engineer, the most important packages are probably Matplotlib for plotting, NumPy for arrays and linear algebra and SciPy for numerical methods. The Pandas package has routines for data structures and data analysis.
The following snippet shows how the trajectory (x-y coordinates) of a projectile fired at an angle of 45-degrees to the horizontal with a muzzle velocity of 100 m/sec can be plotted using just a few lines of code. The instructions to import the matplotlib and numpy libraries are on lines 1 and 2.
import matplotlib.pyplot as plt import numpy as np velocity = 100 # velocity in m/sec angle = 45 # elevation in degrees rad = 3.14159*angle/180 # convert angle to radians tmax = 2*velocity*np.sin(rad)/9.8 # flight time times = np.linspace(0, tmax, 100) # 100 time steps from 0 to tmax x = [velocity * np.cos(rad)*t for t in times] # horizontal range y = [-0.5*9.8*t*t + velocity * np.sin(rad)*t for t in times] # vertical distance plt.xlabel('Horizontal range [meters]') plt.ylabel('Vertical distance [meters]') plt.title('Projectile Trajectory') plt.grid(True) plt.plot(x, y) plt.show()
The chart produced by the above code is:
A few more enhancements can turn this basic chart into a journal-quality chart.