Why Matlab is awesome for FEM development. It's fast, too.

I do most of my FEM development with Matlab. This is something that sets me apart from most of my research colleagues. Most of them either use commercial software such as Comsol, or an in-house Fortran software called FCSMEK that we have.

Against this background, I thought it might be a good idea to disclose some of the reasons behind this all. In other words, what makes Matlab so awesome for this purpose, in my opinion.

Please note that most of the stuff I’ll be describing is not really specific to Matlab per se. Instead, it will mostly apply to its OpenSource clones Octave and Scilab. Probably even the Python semi-equivalent Spyder.

Here we go.

Ease

This is huge.

The biggest factor there is, really. Matlab is simply so freaking easy and convenient to use, for more reasons than one. Or even three. It’s so easy it’s getting hard to even describe it. Talk about a paradox.

But, I still made an effort to list down some individual reasons below. The order is somewhat random, and most of them are interlinked with each other. But bear with me.

Bear with me. Haha, got it? Source here.
Hahaha, the bear-joke again! From the ever-awesome Cyanide and Happiness. Buy their stuff.

High-level language made for computing

Matlab is a high-level language specifically made for scientific-style computing. As such, all basic operations on matrices and arrays are included by default. For instance, multiplying two matrices can is as simple as

A*B.

Any other basic-maths operation you can think of is just as easy.

And since it’s a high-level language, it handles things like memory (re)allocation, carbage collecting, and stuff like that for you.

Easy to get started

This ties closely to the previous point. There are of course maths libraries for any programming language you can think of. Most of them have several.

And you have to choose which one to use. And download it. And maybe install it, or at least import. For each program you code.

Not so in Matlab. Just fire it up and start coding. All the basic functionality is already there, behind a quite-intuitive syntax.

No compilation needed

Again, this relates to the previous two points.

Matlab is an interpreted language, so no compilation needed.

This is a huge time-saver especially if you want to re-run your code several times in a row.

Like I do, especially when trying to debug something that’s broken.

Which brings me to my next point.

Advanced debugging functionality

The debugging-slash-IDE functionality kicks ass.

Let’s say something causes an error. You automatically get the type of the error printed on the screen, and the entire call stack leading down to it. With the exact line of code of each call. Absolutely awesome.

Of course, most IDEs for other languages do have similar functionality today. But, I still find Matlab’s very intuitive and easy to understand.

My face with your typical hardline-programming IDE debugging functionality. Also, a literal angry bird, because Finland is awesome.
My face with your typical hardcore-programming IDE debugging functionality. Also, a literal angry bird, because Finland is awesome.

Stupid practical debugging also easy

Yeah, this is about Matlab making my life very easy for me.

Don’t understand why my algorithm doesn’t work? Just print the contents of each variable on the screen. Done in a single line of code.

Or plot this particular vector or matrix in a suitable form? In the middle of a function execution? A line of code.

Or maybe a surface map of the vector potential values, in the middle of a Newton iteration? A line of code.

Makes figuring bugs out a million times easier.

Profiling

I described the code-profiling functionality of Matlab quite extensively a couple of posts ago.

You simply click one button to run your code. (By the way, profiling adds no noticeable delay in your computation times.) After it’s finished, you can see how much time was spent on each line of code, and how many times each of them was visited. See an example below.

Original function implementation.
Example of the Matlab Profiler output.

As I wrote before, the Profiler makes it very easy to identify which lines of code take the majority of time.Simply fixing a few of these these often yields you a very significant drop in execution times. A factor of ten, easily.

Plotting

I already mentioned plotting before, about using it for debugging

But, it’s worth mentioning here again, in the actual context of plotting.

Basically, most types of plots are already there. Lots and lots of them. Just use the correct function.

Even for FEM, you can get quite far with the default plots. For instance, flux density plots can be obtained with the function fill. Mesh plotting is also there.

Unfortunately, drawing flux lines is something you have to implement yourself. But, this is a single time-investment of some hours, tops.

Meshing

This is nothing special.

You can generate tolerable finite element meshed with Matlab. They are not stellar, but they are sufficient for most development purposes.

Of course, you can generate your mesh elsewhere, and import it with any of the built-in functions. (Which are there by default, as I mentioned.)

Speed

Matlab can be surprisingly fast, if know how to vectorize your code. Much, much faster than people expect based on their not-so-good scripts with 12 nested for-loops.

For finite element analysis, there are actually only a few core functionalities that take most of the computation time. (I have a weird feeling I’ve said something like this before…) Implement those properly, and your program will clock some very competitive times.

One is assembling the large matrices: the stiffness and mass matrices, and the Jacobian for nonlinear analysis. Of course, you also have some other matrices related to circuit equations and such, but those three are by far the heaviest to assemble.

But, with a little effort, you can write assembly routines that are vectorized in the direction where it counts. Namely, they can evaluate the contribution of each element in the mesh in a single vector-operation.

And this can be incredibly fast.

Indeed, I did a small test a couple of months ago. I used my own function to assemble the stiffness matrix for a problem with 1.3 million unknowns. Took slightly over 1 second. That’s quite awesome, in my honest opinion.

The other time-expensive task is solving the resulting matrix equation.

This can also be very fast. Indeed, most 2D-FEM problems can nowadays be handled by sparse direct solution algorithms. In my problems, this typically translates to solution times of some milliseconds. Often, it’s even less.

Hard to beat, actually

Talking about speed, Matlab actually has one huge ace in the hole: the Intel MKL library.

The MKL is a proprietary library for many maths operations, “hand-optimized specifically for Intel processors”. As such, it’s quite freaking hard to beat without, well, using it and paying for it.

Thanks to MKL, some basic operations in Matlab are incredibly fast. This includes stuff like matrix and array multiplication, and similar.

You can try to write an actual compiled piece of C-code or similar, but it still may not beat Matlab on such simple operations – despite the comparatively huge overhead Matlab has to spend interpreting your code.


So, here are some basic reasons why I love Matlab for FEM development. Maybe in the future I’ll tackle the fundamental question: why I love FEM development.

-Antti


Need help with electric motor design or design software? Let's get in touch - satisfaction guaranteed!
Why Matlab is Awesome for FEM Development

Leave a Reply

Your email address will not be published. Required fields are marked *