# Why does FindMinimum return 'The function value Null is not a real number'?

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?

• 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! – user9660 Feb 26 '15 at 16:57
• Ok, will do! Thanks Lou – marco_gorelli Feb 26 '15 at 16:59
• 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). – bill s Feb 26 '15 at 17:11
• You're right, sorry. It works now (if you specify a value for m) – marco_gorelli Feb 26 '15 at 17:18
• Are you trying to enforce the condition that a is zero with your Do loop? – 2012rcampion Feb 27 '15 at 0:56

## 2 Answers

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]


Pretty clearly the min is at f[4].

• 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? – marco_gorelli Feb 26 '15 at 17:29
• 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. – bill s Feb 26 '15 at 17:48
• Ah ok, I'll have to justify my answers graphically then. Thanks! – marco_gorelli Feb 26 '15 at 18:03
• Try plotting from 1 to 4. You'll see that 4 is not the minimum. – 2012rcampion Feb 27 '15 at 0:41
• @2012rcampion -- the OP explicitly asked for the min between m=4 and m=2500. m=4 does indeed give the min in this range. – bill s Feb 27 '15 at 1:14

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}} *)

• can't get that to work...how did you run it? – marco_gorelli Feb 26 '15 at 18:11
• @Lupacante It works as is here. Try it after starting a fresh Mathematica session – Dr. belisarius Feb 26 '15 at 18:16
• @belisarius - very nice! I couldn't see how to make the Catch-Do-Throw into a function... I think you've got it! – bill s Feb 26 '15 at 19:53
• @bills I hate those Do[]s :) – Dr. belisarius Feb 26 '15 at 19:58
• 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? – marco_gorelli Feb 26 '15 at 21:16