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 the following Mathematica code (Mathematica version 9):

DH[x_] := (0.399582 Exp[-0.501606 (-3.57699 + Log[x])^2])/x   
F[n1_, n2_] := Integrate[DH[x], {x, n1, n2}];            
G[n_] := Integrate[DH[x], {x, 0, n - 1}];      
Cp[n1_, n2_] := Piecewise[{{F[n1, n2]*Log2[n1], n1 < n2},
                           {0, n1 >= n2}}];       
Et[n1_, n2_] := Piecewise[{{-F[n1, n2]*Log2[F[n1, n2]], n1 < n2 }, 
                           {0, n1 >= n2}}];  
H[n1_, n2_] := Piecewise[{{Cp[n1, n2] + Cp[n2, 233]- Et[n1, n2]-Et[n2, 233]-G[n1], n1 < n2},
                           {0, n1 >= n2}}];

I want to maximize value of H[n1,n2] and I use the Maximize function:

Maximize[{H[n1, n2],2<=n1<=231 && 3<=n2<=232 && n1 < n2}, {n1, n2}, Reals]

After I run the program, I obtained the solution: {2.64505, {n1 -> 11.6199, n2 -> 232.}}

and many error messages. How obtain the maximum?

share|improve this question
a general comment: try to avoid capitalized function names, especially if single letter (I see no conflicts in your case above (as you go for DH, Cp, Et in the critical cases), but you risk conflicting with built-in symbols, such as D, C etc.) – Pinguin Dirk Aug 23 '13 at 6:59
Your code contains a syntax error (bracket mismatch) in the DH definition. Please ensure your code runs properly as copied and pasted from the site. As DH is using reals, you might as well switch over to NMaximize directly (Maximize does it for you already). – Yves Klett Aug 23 '13 at 7:49
I modified the message.Thanks for the observations. – user599395 Aug 23 '13 at 8:23
Please remember to upvote good answers (and eventually accept your favorite one), see and – Yves Klett Aug 23 '13 at 12:06

I would suggest defining F and G using Set instead of SetDelayed, so that the integral is done just once:

F[n1_, n2_] = Integrate[DH[x], {x, n1, n2}, Assumptions -> {n1 > 0, n2 > 0}];
G[n_] = Integrate[DH[x], {x, 0, n - 1}];

For the maximization , you could try changing the Method option if the default is giving bad results. For example:

NMaximize[{H[n1, n2], 2 <= n1 <= 231 && 3 <= n2 <= 232 && n1 < n2}, {n1, n2}, 
 Method -> "DifferentialEvolution"]
(* {2.73157, {n1 -> 6.92863, n2 -> 18.3921}} *)

There is a tutorial on Numerical Nonlinear Global Optimization, with details about the various method options.

share|improve this answer
Thank you very much for the response! – user599395 Aug 23 '13 at 9:24

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.