2
$\begingroup$

First some background, I am a mathematician by training with very little formal training in programming. I am most comfortable in MATLAB and have been working with it for a long time. My second choice would be C++. But in either language the longest most complicated code I have written is maybe between a hundred and two hundred lines.

I will soon start writing some code for tracking charged particles in various electromagnetic field configurations, at first in 2D and then in 3D. This sparked a debate with my friend if I should write this in MATLAB or C++.

My questions to this community it, what exactly (if any) are the benefits of doing computations like this in C++ over MATLAB? Considering all of the functions/toolboxes available in MATLAB

1.I can do in MATLAB in one line what might take dozens in C++ (old classic argument I know)

2.I don't have to implement any ODE solvers for example (like RK45). All such things I will need are already efficiently tested and implemented so one less thing for me to code/debug/worry about.

3.Same thing for interpolation routines, interpolation in 2D-3D gets very messy, super super easy in MATLAB

4.Same thing for visualization. Once I have the data, super easy to just look at it.

5.A big chunk of this can be vectorized so MATLAB code would be very efficient I think. Not too many loops or anything.

6.I only need small scale shared memory parallelization (in C++ I would use OpenMP) and MATLAB already has a nice easy to use parallel toolbox.

7.MATLAB scripts can be run from command line and our local super computer has it installed (with more than enough licenses) so I can actually run my MATLAB scripts on our super computer. Just invoke multiple instances, each with 6-8 threads if necessary.

So is there any real reason why I should use C++ instead of MATLAB? I will be the only one developing and then the only one using it. I'll be using it for the next year or two for sure and after that I don't know yet. All "real" programmers tell me that all "real" programming should be done in "real" programming languages (i.e. FORTRAN or C). But today it just seems very archaic and outdated opinion at least for mathematical/scientific computation problems. Is there a real reason which I just didn't consider or is it just historical irrational bias?

$\endgroup$
2
  • $\begingroup$ Fortran 95 (and 03/08) code is very MATLAB like if that helps. Also OpenMP gives you more flexibility than MATLABs parallel toolbox. Personally I have always found Fortran 95 + Gnuplot for viz (or Paraview for 3D viz) more convenient to use than MATLAB. A lot of libraries that you get through MATLAB (BLAS/LAPACK/Cholmod/FFTW etc.) are free and can be used by Fortran and C/C++. $\endgroup$
    – stali
    Commented Dec 24, 2013 at 15:57
  • $\begingroup$ I would also consider looking into Cython cython.org $\endgroup$ Commented Dec 25, 2013 at 21:51

1 Answer 1

3
$\begingroup$

Your question teeters on the edge of topicality for this site but here's my ha'pennyworth

Personally (see, this is an opinion and that's where your question is close to off-topic), writing as a real programmer (Fortran, Matlab, Mathematica, C when I have to, C++ when a gun is held to my head) I can't fault the reasons you advance for sticking with Matlab.

And if any real programmers scoff at your choice of Matlab, why, just respond Well, of course, I can code the performance-critical sections in C++ and compile them into MEX-files, if that becomes desirable.

Another opinion of mine is that it is actually very difficult to write performance-critical computational kernels in C++ which are (a) faster and (b) more reliable/robust/accurate than the Matlab intrinsic functions. The Mathworks have put a lot of effort into optimising the execution of much of their basic functionality; oft-times Matlab functions call a tailored version of BLAS (or similar) which is the product of a compilation of C++ (or some other language).

Finally, add another item to your list of points in Matlab's favour:

8 Since R2013a Matlab's core toolbox now includes a unit-testing framework.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.