Python as alternative to Matlab for engineering calculations
Posted December 30, 2013 at 03:20 PM | categories: python | tags:
Updated December 30, 2013 at 09:15 PM
Table of Contents
For the past year I have been seriously exploring whether Python could be used as a practical alternative to Matlab in engineering calculations, particularly in chemical engineering undergraduate and graduate courses. Matlab is very well suited for these calculations, and I have used it extensively in teaching in the past. For example, there is my Matlab blog (http://matlab.cheme.cmu.edu ), and my cmu Matlab package that contains a very nice units package (https://github.com/jkitchin/matlab-cmu ). Matlab is widely used and recognized as a standard software package in engineering. My university has a site license for Matlab, so it doesn't cost me or my students anything to use. Matlab is easy to install, and has almost everything we need out of the box. So why try using something else then? Here are the main reasons:
- Not everyone has access to a "free" Matlab license, and Matlab may not be available to my students when they leave the University. Python offers a free, always available option to them.
- There are several recent Python distributions that are easy to install, and contain almost everything we need for engineering calculations.
- I have been increasingly integrating code into my lecture notes, and this is not easy with Matlab, but it is easy with Python.
- I use Python exclusively in my research, and although Matlab and Python are similar, they are different enough that switching between them is bothersome to me. I do not like teaching students to use tools I do not regularly use, and I believe I can provide them with more value by teaching with tools I have a high level of proficiency in.
1 The significance of an open-source alternative
Many people will be able to use Matlab or some other proprietary software that someone has paid for the license to use. Some people, however, will not have that option for a variety of reasons. Maybe the company they work for will not pay for the license, maybe they are unemployed, or entrepeneurs in a small startup that cannot afford it, maybe they are students at a University without a site license,… For these people Python is a viable option that is always available. That makes me happy.
2 Easy to install Python distributions
An important development in using Python as an alternative to Matlab is the development of many "one-click" installers. Ten years ago it took me about 2 weeks to download and build a Python environment suitable for scientific and engineering calculations. That has kept me from trying to use Python in teaching in the past. Today, I can download a package and install one in about 10 minutes! More importantly, so can my students.
My favorite distribution is the Enthought Canopy distribution (https://www.enthought.com/products/canopy/ ). This distribution comes with all the essential python modules, and an integrated editor with IPython. It is available for Windows, Macs and Linux. They offer free academic licenses.
Another good alternative is the Anaconda distribution (https://store.continuum.io/cshop/anaconda/ ) by Continuum Analytics. It is also available for Windows, Macs and Linux. I have not used this one, but it looks like it would be very good. Anaconda comes with the Spyder editor. They offer free academic licenses.
Python(x,y) is available for Windows (http://code.google.com/p/pythonxy/ ) and comes with the Spyder editor.
WinPython (http://winpython.sourceforge.net/ ) is also available for Windows, and comes with the Spyder editor.
The point here is that there are many options available now that make installing a Python distribution as easy as installing packages like Matlab. Enthought Canopy also provides a "desktop environment" similar to Matlab with an editor, documentation browser, package manager and console that is pretty easy to use.
3 Python + numpy/scipy/matplotlib does almost everything you need
Python by itself is not suitable for typical engineering calculations. You need the numerical, scientific and plotting libraries that provide that functionality. These are provided in numpy, scipy and matplotlib, which are included in the distributions described above.
Typical chemical engineering calculations involve one or more of the following kinds of math problems:
- Linear algebra
- Root finding (nonlinear algebra)
- linear regression
- Nonlinear regression
- Integration and ordinary differential equations
- statistics
- plotting
All of these are doable out of the box with the Python distributions discussed above. You can find many examples of using these, and more on my PYCSE blog (http://kitchingroup.cheme.cmu.edu ) and http://kitchingroup.cheme.cmu.edu/pycse/ . In short, almost every example I put in the Matlab blog has been done in Python. The only ones I did not do yet are some of the interactive graphics with the steam tables. I have not had time to work those out in detail.
I have found it convenient to augment theses with a package I wrote called pycse (https://github.com/jkitchin/pycse ).
- an ode integrator with events similar to the one in Matlab
- some numerical differentiation functions
- linear and nonlinear regression with confidence intervals
- some boundary value problem solvers
- a publish function to convert python scripts to PDF via LaTeX
This package is still a work in progress. Notably, there is not a really good units package in Python that works as well as my Matlab units package does. Two that come close are quantities and pint . Both have some nuances that make them tricky for regular use, and both have some challenges in covering all the functions you might want to use them for.
4 Python from the educator perspective
Make that my perspective. I have developed an approach to using code in my lectures where I use the code to reinforce the structure of the problems, and to analyze the solutions that result. Doing that means I need to have code to show students, and the output, and sometimes to run the code to illustrate something. I also like these examples integrated into my lecture notes, so they have the right context around them.
I have found that Emacs+org-mode+python allows me to easily integrate notes, equations, images, code and output in one place, and then export it to a PDF which I can annotate in class. This ensures that the code and output stay synchronized, that the code is always right where it needs to be, in the right context, and that I can annotate actual code in class, and not pseudocode. This heavily influenced my decision to use Python because it leverages what I already know and want to do. In fact, using it makes me even better at what I already know and helps me learn more about it. That makes me happy!
Not everyone will be a content developer like this, but that is what I like to do. Python makes that process fun, and worth doing for me.
5 Final thoughts
In my opinion Python is and is becoming a more viable alternative to other packages like Matlab for scientific and engineering calculations. I have used it exclusively for about a year solving all kinds of engineering problems that I used to solve in Matlab.
Python is different, for sure. The main differences in my opinion are:
- Python is less consistent in syntax than Matlab. For example, there are two ODE solvers in scipy with incompatible syntax. That is a result of the fact that you install a Python distribution made of packages written by many different people with different needs.
- There is duplicated functionality between numpy and scipy.
- Some functionality in scipy is provided by external "scikits" (http://scikits.appspot.com/ ).
- Support for boundary value problems and partial differential equations is not as good in Python as it is in Matlab 1. At the undergraduate level, this is not a big deal. It is not like the Matlab functions are that easy to use!
- Data regression in Python is not as complete as in Matlab.
- indexing in Python starts at 0, and uses [], whereas in Matlab it starts at 1 and uses ()
- You have to import most functions into Python. In contrast, Matlab has them all in one big namespace.
It is certainly doable to use Python for many scientific and engineering calculations. This past Fall I took the plunge, and taught a whole core course in chemical reaction engineering using Python! It was a Master's level course with 59 graduate students in it. I have also taught a graduate elective course in Molecular Simulation using Python. I still have some polishing to do before I would teach this to undergraduates, but I think it is definitely worth trying!
Footnotes:
It is true there are packages like FiPy (nil) for PDEs, and scikits for BVPS, (nil, nil). But these are not easily installed on all platforms, and typically require some developer experience in compiling.
Copyright (C) 2013 by John Kitchin. See the License for information about copying.