0
$\begingroup$

I am working with the following function

Catch[Do[a = 2^(m*(1 - c)) - 2.718/((m^.5)*2*3.14*c^(c*m + .5) *(1 - c)^((1 - c)*m+ .5)); 
If[a <= 0, Throw[m/(m (1 - c) + 1)]], {c, 0.006, 1, 0.001}]]

and would like to find its minimum between m=4 and m=2,500.

However, if I try writing

FindMinimum[ Catch[Do[a = 2^(m*(1 - c)) - 2.718/((m^.5)*2*3.14*c^(c*m + .5) *(1 - c)^((1 - c)*m + .5));
If[a <= 0, Throw[m/(m (1 - c) + 1)]], {c, 0.006, 1, 0.001}]], {m, 4, 2500}]

it returns

"FindMinimum::nrnum: The function value Null is not a real number at \ {m} = {4.}. >>"

I'm not sure why this is, as the function is equal to 1.11235 at m=4, with no imaginary part.

How can I get the FindMinimum function to work?

$\endgroup$
  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the faq! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$ – user9660 Feb 26 '15 at 16:57
  • $\begingroup$ Ok, will do! Thanks Lou $\endgroup$ – marco_gorelli Feb 26 '15 at 16:59
  • $\begingroup$ First, your "function" does not execute properly, when I enter the Catch[ ... ] as above, there are errors, so of course you cannot do anything with it (like find it's minimum). $\endgroup$ – bill s Feb 26 '15 at 17:11
  • $\begingroup$ You're right, sorry. It works now (if you specify a value for m) $\endgroup$ – marco_gorelli Feb 26 '15 at 17:18
  • $\begingroup$ Are you trying to enforce the condition that a is zero with your Do loop? $\endgroup$ – 2012rcampion Feb 27 '15 at 0:56
0
$\begingroup$

Here is your function:

f[m_] := Catch[
  Do[a = 2^(m*(1 - c)) - 
     2.718/((m^.5)*2*3.14*c^(c*m + .5)*(1 - c)^((1 - c)*m + .5));
   If[a <= 0, Throw[m/(m (1 - c) + 1)]], {c, 0.006, 1, 0.001}]]

and here is a plot of this over 4 to 2500

Plot[f[m], {m, 4, 2500}, PlotRange -> All]

enter image description here

Pretty clearly the min is at f[4].

$\endgroup$
  • $\begingroup$ Yes, it is. However, as I'm working with many functions of this type it would be very nice to be able to use a Mathematica function to find their minimum rather than having to plot them all. This is also part of me trying to get to grips with Mathematica. Any idea why FindMinimum[ Catch[Do[a = 2^(m*(1 - c)) - 2.718/((m^.5)*2*3.14*c^(c*m + .5) *(1 - c)^((1 - c)*m + .5)); If[a <= 0, Throw[m/(m (1 - c) + 1)]], {c, 0.006, 1, 0.001}]], {m, 4, 2500}] doesn't work? $\endgroup$ – marco_gorelli Feb 26 '15 at 17:29
  • 1
    $\begingroup$ I believe you can't use FindMinimum on functions created from iterations like Do... the functions need to be differentiable (at least for most of the minimization methods) and so this kind of programming construct is unlikely to work. $\endgroup$ – bill s Feb 26 '15 at 17:48
  • $\begingroup$ Ah ok, I'll have to justify my answers graphically then. Thanks! $\endgroup$ – marco_gorelli Feb 26 '15 at 18:03
  • $\begingroup$ Try plotting from 1 to 4. You'll see that 4 is not the minimum. $\endgroup$ – 2012rcampion Feb 27 '15 at 0:41
  • $\begingroup$ @2012rcampion -- the OP explicitly asked for the min between m=4 and m=2500. m=4 does indeed give the min in this range. $\endgroup$ – bill s Feb 27 '15 at 1:14
3
$\begingroup$

Perhaps better:

NMinimize[{m/(m (1 - c) + 1), 
  2^(m*(1 - c)) - 2.718/((m^.5)*2*3.14*c^(c*m + .5)*(1 - c)^((1 - c)*m + .5)) <= 0 &&    
  0.006 <= c <= 1 && 
   4 <= m <= 2500}, {m, c}, MaxIterations -> 300]

(* {1.11184, {m -> 4., c -> 0.350592}} *)
$\endgroup$
  • $\begingroup$ can't get that to work...how did you run it? $\endgroup$ – marco_gorelli Feb 26 '15 at 18:11
  • $\begingroup$ @Lupacante It works as is here. Try it after starting a fresh Mathematica session $\endgroup$ – Dr. belisarius Feb 26 '15 at 18:16
  • $\begingroup$ @belisarius - very nice! I couldn't see how to make the Catch-Do-Throw into a function... I think you've got it! $\endgroup$ – bill s Feb 26 '15 at 19:53
  • 1
    $\begingroup$ @bills I hate those Do[]s :) $\endgroup$ – Dr. belisarius Feb 26 '15 at 19:58
  • $\begingroup$ Doesn't work for me...do you run the whole thing, including the commented out part? Do I need to add anything at the beginning? $\endgroup$ – marco_gorelli Feb 26 '15 at 21:16

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.