1
$\begingroup$

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?

$\endgroup$
  • 1
    $\begingroup$ 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.) $\endgroup$ – Pinguin Dirk Aug 23 '13 at 6:59
  • $\begingroup$ 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). $\endgroup$ – Yves Klett Aug 23 '13 at 7:49
  • $\begingroup$ I modified the message.Thanks for the observations. $\endgroup$ – user599395 Aug 23 '13 at 8:23
  • $\begingroup$ Please remember to upvote good answers (and eventually accept your favorite one), see mathematica.stackexchange.com/help/why-vote and mathematica.stackexchange.com/help/accepted-answer $\endgroup$ – Yves Klett Aug 23 '13 at 12:06
2
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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