Bug introduced in 7.0 or earlier and fixed in 9.0
I encounter a problem (Mathematica 7) similar to Strategies to avoid LessEqual::nord in NMinimize? but the advised strategies don't work for me. Also, I get different results with different approaches, though a standard evaluation of the different approaches results into the same number. I think this will become clear when looking at the function. My first approach is to define the function first and then minimize the sum of the squared real and imaginary part. To save a second evaluation, my second approach was to define a Module which directly returns the sum of the squared parts. Here are both the definitions:
eqnS[R_?MachineNumberQ,
k0_?MachineNumberQ, eps_?MachineNumberQ, mu_?MachineNumberQ] :=
2/(3 k0^2) (Exp[-I k0 R] (1 + I k0 R) - 1) -
1/(3 k0^2) (2 + eps mu)/(eps mu - 1);
mineqnS[R_?NumericQ,
k0_?NumericQ, eps_?NumericQ, mu_?NumericQ] := Module[{t},
t = 2/(3 k0^2) (Exp[-I k0 R] (1 + I k0 R) - 1) -
1/(3 k0^2) (2 + eps mu)/(eps mu - 1);
Re[t]^2 + Im[t]^2];
Now, using
k0 = 2 Pi/(510 10^-9);
eps = .7062 + I 2.0307;
mu = 1;
and then comparing
NMinimize[{Re[eqnS[R, k0, eps, mu]]^2 +
Im[eqnS[R, k0, eps, mu]]^2,
R >= 10^-9 && R <= 300 10^-9}, R, MaxIterations -> 200,
Method -> "NelderMead"]
NMinimize[{mineqnS[R, k0, eps, mu],
R >= 10^-9 && R <= 300 10^-9}, R, MaxIterations -> 200,
Method -> "NelderMead"]
the first approach without the "Module" and a doubled evaluation results to
{4.16756*10^-31, {R -> 1.10178*10^-7}}
which gives the correct result without errors. But the second, "Module", one, results (without the displaying the errors here) to
{3.76855*10^-29 + 0. I, {R -> 1.78688*10^-7}}.
And this, even though
mineqnS[10^-8, k0, eps, mu] ==
Re[eqnS[10^-8, k0, eps, mu]]^2 +
Im[eqnS[10^-8, k0, eps, mu]]^2 // N
gives me a "True"!
The message for the second attempt reads
LessEqual::nord: Invalid comparison with 3.768547875531289`*^-29+0.` I attempted.
PS: In V8, the problem is still there.
Clear
all definitions before making assignments. It may be that you have some older rule formineqnS
with more general arguments, still present in the system. $\endgroup$ClearAll["Global
*"]` ;) $\endgroup$