NMinimize in Mathematica could drive you insane!

For the past one week, I had been breaking my head over a piece of Mathematica code. I wanted to use NMinimize in Mathematica to minimize a function which was written as a module. In this module, I was solving a linear system of equations (sparse and overdetermined, but that’s not the point). The problem was that even with moderate dimensions, say 20*17, Mathematica ran out of memory and crashed on me.

I realized that the first invocation to my objective function from NMinimize was being done symbolically rather than numerically. Why the hell was it doing that? I read and re-read the documentation (whatever little of it there is) until I knew it by heart. I was just about to punch my computer, when the good guys over at Stack Overflow told me what the problem was.

It turns out that NMinimize does not hold its arguments. This means that as the list of arguments is read from left to right, each argument is evaluated and replaced by the result of the evaluation. So for example, something like the following:


would cause the evaluation of dummy with “x” as an input, which is exactly what was happening in this case.

I changed the call to


and lo and behold! everything was well in my world again.

So, what are the lessons for me in this totally forgettable experience?

#1. If a piece of software (which is not free) is driving you insane, the fault is most probably yours.

#2.  You may have worked through all the advanced notebooks your prof gave you while learning Mathematica, but it won’t hit you until it has hit you.

#3. The www makes the world a better place to live in.  Strive to contribute sense to this sea of sense and nonsense.

This entry was posted in Software and tagged . Bookmark the permalink.

6 Responses to NMinimize in Mathematica could drive you insane!

  1. Matt says:

    I have been banging my head against the wall all morning with the same exact problem. Im trying to use NMinimize on a module which is essentially a black box which spits out a single value. I was having the same problem as you when it tried to evaluate my module before substituting the parameter I am optimizing. I tried holding the argument but it seems to never evaluate it. I get “The function value – Hold[dummy[5.3]] is not a number at x=5.3”. Is that all you did? If you can help I would greatly appreciate it.

    • Well,I did an additional thing: I imposed a constraint that my module should only accept numeric values, and that seemed to work for me, even if I did not hold the first argument.something like dummy[arg/;VerctorQ[arg,NumericQ] in my case since the argument to my module was a vector.
      hope this helps!

  2. Thank you very much. The second solution, imposing constraints on the parameters, is indeed the solution.


  3. sana says:

    Whats going seriously.I have been trying this since week.Can you help me how to generate simulated data under given condition

  4. Sam says:

    NMinimize[] filling the memory is still stupid, and having to “Hold[]” doesn’t justify this. I can understand that a single call could fill up memory, but I can’t understand why that filled memory can’t be released after the minimization is done…

  5. Mariano Pierantozzi says:

    You save me from! Thanks a lot!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s