# Time limit NMaximize [closed]

Is it possible to time limit NMaximize? It is taking so long to evaluate my minimum, that I would like to see if it is doing what I expect it to do. So I was thinking of time limiting it in order to have at least a result.

CLOSED

I should use TimeConstrained[] as JM suggested in this case.

• Have you seen TimeConstrained[]? – J. M.'s technical difficulties May 8 '16 at 0:40
• No I haven't will do now! – Mirko Aveta May 8 '16 at 8:44
• OK. J.M. Answered my question. Thank you once again sir!! – Mirko Aveta May 8 '16 at 8:46
• Why not answer your own question (with a usage example) instead of putting it in as an edit? – J. M.'s technical difficulties May 8 '16 at 9:05

I'm not sure if you can specify a time limit, but you can use the StepMonitor option to watch the maximization as it progresses. Here is an example:

f[x_] = -x^4 - 3 x^2 + x;
NMaximize[f[x], x, StepMonitor :> (Print["step x = ", x, ", f[x]=", f[x]]; Pause[.1])]


I'm only using Pause[] in the StepMonitor to slow down the process, so we can watch it happen for this simple function.

If the goal is to "time out" when the value is good enough, you may be better off using AccuracyGoal and/or PrecisionGoal. You could also lower MaxIterations below the default value of 100, but I would try watching things first with StepMonitor.

• Thank you for your comment. I have tried all your options yet I still don't received even one evaluation. I used even accuracyGoals and PrecisionGoals set to 1, which I guess is the minimum. I really don't understand why. Unfortunately I can't post my function because it needs a package to run. – Mirko Aveta May 8 '16 at 8:44

I should use TimeConstrained[] as JM suggested in this case.

• It is an answer! It was exactly what I was using for. As soon as I can I'll tick it and edit my question. – Mirko Aveta May 8 '16 at 9:44