views:

789

answers:

3

I am doing a research project and need to use Monte Carlo simulation. I want to know, what is better to use: MATLAB or Mathematica? I have used MATLAB before but have heard good things about Mathematica.

+3  A: 

If you are running Monte Carlo simulations, you probably need high speed more than any other time. You should write this in C if you need to. Otherwise, MATLAB is perfectly adequate for fast numerical calculations - it uses BLAS and LAPACK to do the heavy lifting.

EDIT: I might add: be very careful about the kind of code you write. Use vector or matrix operations wherever at all possible in MATLAB: even if they look silly, they'll be much faster.

Incredibly trivial example to illustrate:

    % Extremely slow:
    for i = 1:length(x)
       x(i) = 2*x(i);
    end

    % Extremely fast:
    x = 2*x;
Peter
@Peter, you are quite right! C or composite applications that bind to C for time critical portions are the eventual destination of many MC sims. However Matlab (and probably Mathematica as well) are routinely used to provide ad-hoc / one-off runs, or to serve as prototypes (which eventually get ported to C-based apps).
mjv
While vectorisation is generally quicker (and can take advantage of multi-threading in some operations), MATLAB's JIT engine has reduced the relative importance of vectorisation in recent years. Also, the for loop can be converted more easily to a parallel for loop (PARFOR) which is ideal for MC simulations.
Edric
Bad example, the "slow" version is faster. You should avoid vectorisation unless it makes the program more clear: http://willwont.blogspot.com/2009/10/matlab-vectorisation.html
Will Robertson
@mvj: I usually prototype MC (*) with Python. This gives me the chance to keep the interface (mainly reading the inputs) and only substitute the actual computations when needed. Plus, you get all the fancy histograms of matplotlib :-)(*) In the last year I have needed some MC prototypes mainly as teaching examples..
Francesco
@Will, your example is completely busted. You should use rand(1000,1), and everything will become clear: in summary, your method does 1000 multiplies for the loop version, and 1e6 multiplies for the `x = 2*x` version.
Peter
Sorry Peter, you're totally right. Too long away from Matlab, I'm afraid.
Will Robertson
+2  A: 

If you've the luxury of time you should try both and see which fits your mental model of how to write the simulations better.

If you're passing around and iterating ideas then you may find Mathematica lets you express abstract concepts a little better; if you're writing lots of "straight code" then MATLAB will probably more suitable.

Will Robertson
+3  A: 

One advantage that Mathematica has over MATLAB is that, unless you buy the Parallel Computing Toolbox (PCT) for the latter, the former has better 'out-of-the-box' tools for parallelising your simulations across multiple cores. I'm not saying that this makes Mathematica better, I think that that decision is down to which of the systems fits your computations better or which fits your thinking better.

I've used both (including PCT with MATLAB) and, as ever with languages, it's horses for courses and Mathematica is better for those jobs which Mathematica is better for, so is MATLAB.

High Performance Mark

related questions