1
$\begingroup$

For the function fun[a_, b_, x_, y_] = Sin[a x] Cos[b y] (a x + b y);corresponding to $x$ and $y$, I need a RegionPlot showing the maximum of function $fun[a, b, x, y]>0.5$ such that the maximization is carried over $a,b\in [0,1000]$.

A failed attempt: RegionPlot[{NMaximize[ fun[a, b, x, y] > 0.5, {a, 0, 1000}, {b, 0, 1000}]}, {x, 0, 2 \[Pi]}, {y, 0, 2 \[Pi]}]

$\endgroup$
2
  • $\begingroup$ Is this really that difficult? $\endgroup$ – Mike Jan 30 at 13:13
  • $\begingroup$ it looks like the region will be the whole x-y region (Rectangle[{0,0},{2Pi, 2Pi}]) if threshold is less than MaxValue[{fun[a,b,x,y], 0<=a<=1000,0<=b<=1000,0<=x<=2Pi,0<=y<=2Pi}, {a,b,x,y}] and empty region otherwise. $\endgroup$ – kglr Jan 31 at 12:25
0
$\begingroup$

Inequalities are boolean expressions; they do not have maximums and minimums.

$Version

(* "12.2.0 for Mac OS X x86 (64-bit) (December 12, 2020)" *)

Clear["Global`*"]

fun[a_, b_, x_, y_] = Sin[a x] Cos[b y] (a x + b y);

NMaximize[{fun[a, b, x, y], 0 <= a <= 1000, 0 <= b <= 1000, 0 <= x <= 2 Pi, 
   0 <= y <= 2 Pi}, {a, b, x, y}, WorkingPrecision -> 20] // N

(* {2797.59, {a -> 404.339, b -> 595.551, x -> 3.68672, y -> 2.19444}} *)

For nonlinear expressions, NMaximize generally only finds a local maximum.

Using a non-default Method provides a better result:

(sol = {#, NMaximize[{
          fun[a, b, x, y], 0 <= a <= 1000, 0 <= b <= 1000, 0 <= x <= 2 Pi, 
          0 <= y <= 2 Pi},
         {a, b, x, y},
         WorkingPrecision -> 20,
         Method -> #]} & /@
      {Automatic, "NelderMead", 
       "DifferentialEvolution", "SimulatedAnnealing"} //
     SortBy[#, #[[2, 1]] &] &) // N //
 Grid[#, Frame -> All] &

enter image description here

(param = sol[[4, 2, 2, 1 ;; 2]]) // N

(* {a -> 973.25, b -> 1000.} *)

Due to the complicated nature of the region's structure, this is quite slow.

RegionPlot[(fun[x, y, a, b] /. param) > 1/2,
 {x, 0, 2 Pi}, {y, 0, 2 Pi},
 PlotPoints -> 75,
 WorkingPrecision -> 20]

enter image description here

Using a much higher threshold.

RegionPlot[(fun[x, y, a, b] /. param) > 2500,
 {x, 0, 2 Pi}, {y, 0, 2 Pi},
 PlotPoints -> 75,
 WorkingPrecision -> 20]

enter image description here

$\endgroup$

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.