I have a problem with NMaximize which is best depicted by the following figure. The result indicates, that the solution mathematica finds seems to be smooth except a few outliers. How can I get rid of them? I tried to change the method NMaximizes uses, e.g. SimulatedAnnealing etc. but it did not improve. Moreover, changing WorkingPrecision was also not a viable option, too (increase up to 400). Any kind of help is appreciated!

Solution of NMaximze

The code is as follows:

RS[a_, p_, v_, t1_, T1_, x0_, i1_, i2_, t2_]:=Module[{z, T0, dT0, b, n1, d, i},  
b := 1/2 - (a/v^2) + Sqrt[(1/2 - a/v^2)^2 + (2*p)/v^2];   
n1[w_] := PDF[NormalDistribution[0, 1], w];  
z[a1_] := a1/(p-a);  
i[a4_] := i1 + a4*i2;  
T0[a2_, a3_] := T1 + (a2)^t1*(a3)^t2;  
dT0[a2_, a3_] := t1*(a2)^(t1-1)*(a3)^t2;  
d[a2_, a1_, a3_, a5_] := (Log[T0[a2, a3] - Log[a5]] + (a - 1/2*(v)^2)*a3)*(v*Sqrt[a3])^(-1);  
NMaximize[{((T0[a2, a3]-1)*z[a1]-a2-i[a4])*(x0/a1)^b, 
a4*Exp[-p*a3]*n1[d[a2, a1, a3, a5]]*1/(v*Sqrt[a3])*dT0[a2, a3]==1 
&& a1 >= 0 && a2 >= 0 && a3 >= 0 && a4 >= 0 && a5 >= 1}, {a1, a2, a3, a4, a5}] 

while the graph is generated using, e.g.:

Plot[RS[0.03, 0.05, j, 0.1, 1.1, 1, 30, 0.1, 0.025][[2, 1, 2]], {j, 0.05, 0.25}]
  • 2
    $\begingroup$ It will be easier if You provide input data instead of plot image. $\endgroup$
    – Wojciech
    Jan 28, 2014 at 15:16
  • $\begingroup$ Luke, without code to reproduce the problem this question will most likely be closed - so please add that. $\endgroup$
    – Yves Klett
    Jan 28, 2014 at 17:26

1 Answer 1


Apparently, for certain values of v, (the automatic method/options of) NMaximize fail(s) because the objective function becomes too complicated. In addition, your constraints are not set properly. For example, you allow $a_1$ and $a_3$ to be equal to $0$ even though they appear as part of denominators. (The outliers seem to be generated with NMaximize getting trapped at $a_3=0$ - the actual value of $a_1$ is irrelevant in that case.)

I tried changing the method and (one of) its parameters (Method -> {"DifferentialEvolution", "ScalingFactor" -> 0.1}), as well as the inequality constraints of $a_1$ and $a_3$ to a1 > 0 and a3 > 0.001, respectively (a3>0 still gave me solutions with $a_3=0$).

Replacing the NMaximize function in your module with

NMaximize[{((T0[a2, a3] - 1)*z[a1] - a2 - i[a4])*(x0/a1)^b, 
  a4*Exp[-p*a3]*n1[d[a2, a1, a3, a5]]*1/(v*Sqrt[a3])*dT0[a2, a3] == 
    1 && a1 > 0 && a2 >= 0 && a3 > 0.001 && a4 >= 0 && a5 >= 1}, {a1, 
  a2, a3, a4, a5}, 
 Method -> {"DifferentialEvolution", "ScalingFactor" -> 0.1}]

and performing the optimization for a limited set of values of v (Range[0.05, 0.3, 0.01]) gives a smooth solution

c = Monitor[
  Table[RS[0.03, 0.05, j, 0.1, 1.1, 1, 30, 0.1, 0.025][[2, 1, 2]], {j,
     Range[0.05, 0.3, 0.01]}], j];
ListLinePlot[Transpose[{Range[0.05, 0.3, 0.01], c}]]

enter image description here

It is very much possible to get outliers for a denser set of values of v (e.g., those evaluated by Plot). In that case, I would try different methods/parameters for these specific values of v. However, if plotting is all you care about, the above graph looks fine to me.

P.S. Detailed description of methods and parameters used by NMaximize can be found here

  • $\begingroup$ The Mathematica documentation on NMaximize is lengthy. I don't really want to invest time in the theory of maximization; I just want to use it. Unfortunately, the Mathematica documentation doesn't seem to answer the most basic question: I just want to know if a larger ScalingFactor means that Mathematic will work harder and longer to get my results. I'm assuming that the more time Mathematica spends on a maximization, the more accurate the result will be. Maybe I'm oversimplifying the subject, but nonetheless I'd be thankful if you could help me with an answer. $\endgroup$
    – Chris
    Oct 22, 2020 at 17:26

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.