Skip to main content

numerics - Strategies to avoid LessEqual::nord in NMinimize?


When using NMinimize on functions with complex intermediate expressions (but a real end result), quite often one gets the error LessEqual::nord. Example:


NMinimize[Abs[(a+I b)^(3/2)],{a,b}]
(*
LessEqual::nord: Invalid comparison with 0.306069 + 0. I attempted.

LessEqual::nord: Invalid comparison with 0.306069 + 0. I attempted.

LessEqual::nord: Invalid comparison with 0.306069 + 0. I attempted.


General::stop: Further output of LessEqual::nord
will be suppressed during this calculation.

Less::nord: Invalid comparison with -0.0745302 + 0. I attempted.

NMinimize::cvmit:
Failed to converge to the requested accuracy or precision within 100
iterations.

{1.0635969220476164*^-12 + 0.*I, {a -> -8.71759358950322*^-9, b -> 5.707170335837908*^-9}}

*)

In some cases (not the one above; I didn't find a simple one where it happens) this also results in a clearly wrong result. Therefore it's desirable to remove the error messages.


Now the only way to get rid of this error is to change the expression in a way that it doesn't trigger the error. However the expressions are generally complicated enough that it's not feasible by hand. I've found that a combination of the following strategies works sometimes:



  • Use ComplexExpand with the option TargetFunctions->{Re,Im}.

  • Put the entire expression into an Abs or Re (despite it being known to be real from construction) and use Simplify or FullSimplify with appropriate constraints (and hope it finishes in reasonable time). (Abs of course only works if the result is also nonnegative)


However those strategies are not always sufficient. Therefore my question:


What are other good strategies to get the expression into a form suitable to NMinimize?




Answer



I think you have to do the same as in many such cases: protect your arguments to be strictly numerical:


f[a_?NumericQ, b_?NumericQ] := Abs[(a + I b)^(3/2)];

And then no problems:


NMinimize[f[a,b],{a,b}]

(*
==> {1.11868*10^-26,{a->3.9489*10^-18,b->3.07007*10^-18}}
*)


Edit:


The following function automatically packs the expression into a function with _?NumericQ pattern arguments:


NOptimize[optfunc_,expr_,vars_,options___]:=
Module[{f,
varlist=If[ListQ[vars],vars,{vars}],
expression=If[ListQ[expr],First@expr,expr],
conditions=If[ListQ[expr],Rest@expr,{}]},
Evaluate[f@@(Pattern[#,_?NumericQ]&/@varlist)]=expression;
optfunc[{f@@varlist}~Join~conditions,vars,options]]


It can be used as follows:


NOptimize[NMinimize, a^2, a, AccuracyGoal->0.01]
(*
--> {2.39829*10^-33,{a->4.89724*10^-17}}
*)

or with constraints:


NOptimize[NMinimize, {a^2, a>3}, a, AccuracyGoal->0.01]
(*

--> {9.,{a->3.}}
*)

The following shows that it indeed solves the problem with LessEqual::Nord:


NOptimize[NMinimize,Abs[(a+I b)^(3/2)],{a,b}]
(*
--> {9.06219*10^-27,{a->4.31982*10^-18,b->4.8223*10^-19}}
*)

Comments

Popular posts from this blog

front end - keyboard shortcut to invoke Insert new matrix

I frequently need to type in some matrices, and the menu command Insert > Table/Matrix > New... allows matrices with lines drawn between columns and rows, which is very helpful. I would like to make a keyboard shortcut for it, but cannot find the relevant frontend token command (4209405) for it. Since the FullForm[] and InputForm[] of matrices with lines drawn between rows and columns is the same as those without lines, it's hard to do this via 3rd party system-wide text expanders (e.g. autohotkey or atext on mac). How does one assign a keyboard shortcut for the menu item Insert > Table/Matrix > New... , preferably using only mathematica? Thanks! Answer In the MenuSetup.tr (for linux located in the $InstallationDirectory/SystemFiles/FrontEnd/TextResources/X/ directory), I changed the line MenuItem["&New...", "CreateGridBoxDialog"] to read MenuItem["&New...", "CreateGridBoxDialog", MenuKey["m", Modifiers-...

How to thread a list

I have data in format data = {{a1, a2}, {b1, b2}, {c1, c2}, {d1, d2}} Tableform: I want to thread it to : tdata = {{{a1, b1}, {a2, b2}}, {{a1, c1}, {a2, c2}}, {{a1, d1}, {a2, d2}}} Tableform: And I would like to do better then pseudofunction[n_] := Transpose[{data2[[1]], data2[[n]]}]; SetAttributes[pseudofunction, Listable]; Range[2, 4] // pseudofunction Here is my benchmark data, where data3 is normal sample of real data. data3 = Drop[ExcelWorkBook[[Column1 ;; Column4]], None, 1]; data2 = {a #, b #, c #, d #} & /@ Range[1, 10^5]; data = RandomReal[{0, 1}, {10^6, 4}]; Here is my benchmark code kptnw[list_] := Transpose[{Table[First@#, {Length@# - 1}], Rest@#}, {3, 1, 2}] &@list kptnw2[list_] := Transpose[{ConstantArray[First@#, Length@# - 1], Rest@#}, {3, 1, 2}] &@list OleksandrR[list_] := Flatten[Outer[List, List@First[list], Rest[list], 1], {{2}, {1, 4}}] paradox2[list_] := Partition[Riffle[list[[1]], #], 2] & /@ Drop[list, 1] RM[list_] := FoldList[Transpose[{First@li...

plotting - How to draw lines between specified dots on ListPlot?

I would like to create a plot where I have unconnected dots and some connected. So far, I have figured out how to draw the dots. My code is the following: ListPlot[{{1, 1}, {2, 2}, {3, 3}, {4, 4}, {1, 4}, {2, 5}, {3, 6}, {4, 7}, {1, 7}, {2, 8}, {3, 9}, {4, 10}, {1, 10}, {2, 11}, {3, 12}, {4,13}, {2.5, 7}}, Ticks -> {{1, 2, 3, 4}, None}, AxesStyle -> Thin, TicksStyle -> Directive[Black, Bold, 12], Mesh -> Full] I have thought using ListLinePlot command, but I don't know how to specify to the command to draw only selected lines between the dots. Do have any suggestions/hints on how to do that? Thank you. Answer One possibility would be to use Epilog with Line : ListPlot[ {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {1, 4}, {2, 5}, {3, 6}, {4, 7}, {1, 7}, {2, 8}, {3, 9}, {4, 10}, {1, 10}, {2, 11}, {3, 12}, {4, 13}, {2.5, 7}}, Ticks -> {{1, 2, 3, 4}, None}, AxesStyle -> Thin, TicksStyle -> Directive[Black, Bold, 12], Mesh -> Full, Epilog -> { Line[ ...