Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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}]
share|improve this question
It will be easier if You provide input data instead of plot image. – Wojciech Jan 28 '14 at 15:16
Luke, without code to reproduce the problem this question will most likely be closed - so please add that. – Yves Klett Jan 28 '14 at 17:26

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

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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