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:

   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);

   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.

  • $\begingroup$ Make sure to Clear all definitions before making assignments. It may be that you have some older rule for mineqnS with more general arguments, still present in the system. $\endgroup$ – Leonid Shifrin Mar 16 '13 at 18:17
  • $\begingroup$ My first operation is always a ClearAll["Global*"]` ;) $\endgroup$ – Clemens Mar 16 '13 at 18:18
  • $\begingroup$ I just tried this in V9, and get the correct result also for the second case, without errors. So, this must have been a bug in V7, which apparently have been fixed since. $\endgroup$ – Leonid Shifrin Mar 16 '13 at 18:21
  • 2
    $\begingroup$ No problem. You've made a great first question, so this was easy. I wish all the new users here would make their questions a quarter as good! $\endgroup$ – Leonid Shifrin Mar 16 '13 at 18:26
  • $\begingroup$ I confirm the presence of the error messages using this code on version 7. $\endgroup$ – Mr.Wizard Mar 16 '13 at 18:33

Your Answer

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

Browse other questions tagged or ask your own question.