Scientific Computing Software: Matlab, Python, & Octave

Since 2005 the majority of my experience lies in developing and testing algorithms used in space systems applications such as satellite and orbit trajectory simulations. Matlab and Python are my go-to languages that I use whenever I have to solve problems in my work. I thought I would write a post to discuss the merits and limitations of using both languages. In addition, I wanted to introduce a free Matlab alternative, GNU Octave.

Note: I know that Matlab is supposed to be written in all caps. Whenever I write it that way, it feels like I’m shouting. I’ve purposely chosen to capitalize it incorrectly…sue me.

Why Matlab and Python?

My career field is very focused on scientific computing. To solve everyday problems I often find myself using techniques from various mathematical fields including linear algebra, geometry, calculus, statistics, etc. As a research scientist, I’m often toying with ideas and concepts which may or may not lead anywhere. What I need is a tool which allows me to quickly prototype a solution and evaluate the results. Oftentimes, “evaluating the results” means visualizing the outputs in some useful way. Out of the box, Matlab provides nearly all of the tools needed to meet these goals. Certain distributions of Python, namely Anaconda Python, do the same thing, though it isn’t quite as seamless as Matlab. More on that later. GNU Octave provides a free “Matlab-like” alternative for those not willing to go down the Python route.

There are libraries that help traditional programming languages (like C++ and Java) implement mathematical solutions. However, they aren’t nearly as easy to use as Matlab and Python. Forget about it if you want to visualize your results using graphs in C++ or Java! While these languages can provide blazing fast results, they are best left for final product implementation.

I’m aware of other languages, such as R and Julia, which provide similar capabilities to Matlab and Python and in some cases perform better than the languages I’ve chosen. However, I’ve never really encountered these languages in actual use by anyone in my field. It’s hard to justify the time and effort needed to learn a new language when that language is not used by my colleagues. I’m sure that they are perfectly fine languages, I just don’t know enough about them to write about them here.

Software Options

In my opinion, there are only three easy to use software distributions for a Matlab or Python scientific computing environment. These distributions are: Matlab, Anaconda Python, or GNU Octave.

Matlab

Matlab, provided by MathWorks, is a solution which provides an IDE, runtime environment, graphics capabilities, debugger, and profiler all within a single software package. The Matlab language itself is a scripting language that is interpreted by the Matlab software program. There is a compiler, which allows a developer to create compiled Matlab that can run on a machine without Matlab. However, compiled Matlab code is rarely used in practice. Generally, most users simply pass around their raw Matlab scripts if they want to share code.

The biggest complaint that I (and most people) have with Matlab is the cost. A single-seat license typically costs ~$2,000 with a yearly maintenance fee of ~$1,000. In addition to this, there are several toolboxes which make solving problems with the software easier. Each of these toolboxes typically cost ~$1,000 with a yearly maintenance fee of ~$500. The ones that I use on a daily basis and can’t live without are the Parallel Computing Toolbox and the Statistics and Machine Learning Toolbox. If I had my druthers, I would also have the Optimization and Database Toolboxes. However, as you can see, the costs start piling up. It would cost my company ~$6,000 upfront and ~$3,000 every year just for a single-seat license with my optimal setup. They are currently paying ~$2,000 for yearly maintenance fees for my less than ideal configuration.

However, there are some great benefits that come with the hefty price tag. In particular, the Parallel Computing Toolbox is worth its weight in gold. In order to change something from serial to parallel computing, all you need to do is change a “for” loop into a “parfor” loop. With that simple code change you’ve unlocked the power of all the processors in your machine. Granted, there are some gotchas with implementing parfor loops, however they are generally overcome with a few easy workarounds. The debugger is the best I’ve encountered across any programming language and/or IDE. It simply just works without any fuss or hassles.

Anaconda Python

There are many ways to install and use Python on a modern computer. In fact, most modern Linux distributions come with Python preinstalled. However, I would caution any user from using a vanilla Python distribution for scientific computing. Python requires the installation of a number of libraries to provide scientific computing functionality. Some of the libraries include: numpy, scipy, matplotlib, and pandas. While it is relatively easy to install these libraries manually, I would not recommend using Python in this way, especially for scientific computing. One major reason is that installing these libraries could impose dependencies on the OS version of Python which, in turn, could break that version of Python for the OS.

Of course, a user could download a Python installer from python.org and then install the software as a “local” install. Then they could install all the needed libraries to create their own homebrew scientific computing environment. But why go through all of that hassle when Anaconda Python does all of this for you? In addition, Anaconda Python includes the Spyder IDE which provides similar capabilities to the Matlab IDE.

Oh yeah, did I forget to mention that Anaconda Python is free? However, before Matlab is sworn off forever, there are some caveats with using Python. You definitely get what you pay for. While the Spyder IDE provides similar capabilities to the Matlab IDE (it even has a “Matlab layout” option) it is nowhere near as clean as the Matlab experience. As a very experienced Matlab user, everything in Spyder is usable but is just a little “off”. This is mostly caused by the fact that Python is a general purpose language which can also be used for scientific computing. This isn’t Python’s fault, per se, but can be a little frustrating when such a seamless user experience exists with Matlab.

There is certainly a learning curve associated with using Python for scientific computing. A user needs to learn to use various open-source libraries, some of which aren’t consistently documented. In addition, simple constructs in Matlab can be quite a bit more arduous in Python. Declaring a 2D matrix in Python requires a bunch more commas and brackets than Matlab. Oh, and don’t forget to put a “.” after one of the numbers if all the values happen to be integers, otherwise Python will interpret the entire matrix as integers. This has bitten me a number of times when I’ve expected floating point outputs from my computations.

Remember how I mentioned “parfor” in the Matlab section for parallel computing? Yes, you can implement parallel processing in Python, but it is a bit more complicated than Matlab. Instead of going into the details, I’ll direct you to the following article on the Anaconda website.

However, despite its shortcomings, free is free. Oftentimes Python is chosen simply because the licensing costs of Matlab are untenable.

GNU Octave

The final player in my scientific computing roundup is GNU Octave. For the uninitiated, GNU Octave (or Octave, for short) is a free scientific Matlab-like computing language distributed under the GNU General Public License. Many scripts written in Octave are drop-in compatible with Matlab, and vice-versa. The Octave IDE closely mimics the Matlab IDE, providing similar functionality. All you have to do is ignore the all caps warning that “There is ABSOLUTELY NO WARRANTY” with using the software!

In my experience, Octave is a pretty good stand-in for Matlab when cost is an issue. Since it shares many of the programming constructs as Matlab, I also find Octave much easier to use than Python for scientific computing. I even used it to create the nifty animation relating mean, eccentric, and true anomalies in one of my OrbitNerd videos.

OrbitNerd Video Screenshot
OrbitNerd Video Screenshot

However, all isn’t rosy when using Octave. It is freeware, so some of the implementations are not as good as Matlab. The debugger is pretty good, but the fact that I can’t traverse the call stack is something that I miss from Matlab. Also, once you move past basic functionality, you are dependent on free and open-source packages to implement more complicated mathematical routines. Thus, falling prey to a similar conundrum encountered with Python libraries. Finally, Octave can be pretty slow. Of the three software packages presented in this post, Octave is definitely the slowest.

Again, free is free. If you are looking for a scientific computing package that is free and you are intimidated by Python, then Octave is definitely the way to go.

My Preferences

If money is no option, then Matlab will always be my #1 preference. It’s easy to use, provides some robust toolsets, and enables me to do my work with the greatest ease. One of my colleagues and I frequently talk about this topic. Our consensus is that we prefer Matlab because we “don’t have to think” in order to do our work. Ok, that may seem a little weird to say, considering that the work we do is closely related to rocket science. Let me explain.

As I alluded to earlier, we deal with a lot of math and equations on a daily basis. Matlab’s syntax most closely resembles the equations that we translate into software algorithms. When we say we “don’t have to think” when working in Matlab, we mean that we don’t have to expend much brain power when performing this translation. In Python, we have to worry about more “computer sciency” type stuff like data types and syntax rules, which frankly get in the way of our thought processes. These may seem like minor quibbles, but it makes enough of a difference that I’ll reach for Matlab over Python every time.

However, money is often very important, especially for an organization that might not be able to pay those extravagant license fees for Matlab. The choice becomes much harder then, since the benefits of using Octave don’t necessarily outweigh its shortcomings. I think it’s much harder to justify “it uses Matlab syntax” for a relatively unknown piece of software when stacked against Python. It’s an even tougher pill to swallow with that “ABSOLUTELY NO WARRANTY” disclaimer that shouts at you every time you start the program.

That said, for home projects, I end up using Octave as my scientific computing platform. In a professional setting, I would opt for Anaconda Python over Octave.

Epilogue

It feels like I wrote a book here. I originally wanted to go into details of implementation and syntax between the different languages, but then I realized that this post would be way too long! I’ll probably go into those details in other posts; honing in on implementations as they relate to specific scientific computing problems. Feel free to post particular questions you may have about Matlab or Python. I’m definitely much more of an expert in Matlab (as if you couldn’t tell from the context of this post), but I’m happy to take a swing at whatever comes my way.