0
$\begingroup$

I am trying to understand how Mathematica finds the local maxima of a function of two variables, but I am a bit lost here.

For example, consider:

m = FindMaximum[{Sin[x] Sin[2 y], {x, y} ∈ Rectangle[{0, 0}, {4, 4}]}, {x, y}]

and what I get is the following:

{1., {x -> 1.5708, y -> 0.785398}}

which is not bad, but when I make a contour plot of the function, I see that there are more local maxima to consider, as the following shows:

Show[
  ContourPlot[Sin[x] Sin[2 y], {x, 0, 4}, {y, 0, 4}],
  Graphics[{Red, PointSize[Large], Point[{x, y} /. Last[m]]}]]

Why doesn't Mathematica provide me with all the local maxima, at least within bounded domain? Do I do something wrong?

I have also tried to find the other local maxima points:

Solve[Sin[x] Sin[2 y] == 1, {x, y}]

but it does not help.

$\endgroup$
  • 1
    $\begingroup$ You need to provide FindMaximum with initial points to search, because it finds local maxima. $\endgroup$ – Alx Dec 21 '19 at 6:18
3
$\begingroup$

FindMaximum searches for a local maximum. If you want to find multiple local maxima you need to specify multiple starting points. At the boundaries, local maxima do not necessarily meet the usual criteria.

Clear["Global`*"]

f[x_, y_] := Sin[x] Sin[2 y]

sol = FindMaximum[{f[x, y], 0 <= x <= 4, 0 <= y <= 4},
    {{x, #[[1]]}, {y, #[[2]]}}] & /@
  {{1.5, 0.75}, {1.5, 4}, {4, 0.1}, {4, 
    2.5}}

(* {{1., {x -> 1.5708, y -> 0.785398}}, {1., {x -> 1.5708, 
   y -> 3.92699}}, {-8.00857*10^-8, {x -> 3.71699, 
   y -> 7.35854*10^-8}}, {0.756802, {x -> 4., y -> 2.35619}}} *)

Show[
 Plot3D[f[x, y], {x, 0, 4}, {y, 0, 4}],
 Graphics3D[{Red, AbsolutePointSize[8],
   Point[{x, y, f[x, y]} /. sol[[All, 2]]]}]]

enter image description here

| improve this answer | |
$\endgroup$
  • $\begingroup$ So first I need to plot the function and then guess the starting points? $\endgroup$ – dmtri Dec 21 '19 at 7:57
  • 3
    $\begingroup$ @dmtri - FindMaximum requires a starting value (even if it is just the default value). Plotting is one method to arrive at good initial values. You could automate the extraction of initial estimates from the plot data by using FindPeaks. Or, in some cases, the context of the problem may indicate the region(s) of interest. Alternatively, one could use a grid search perhaps augmented by use of FindPeaks to get initial starting values. FindMaximum is analogous to FindRoot which also requires a starting value (even if just the default value). $\endgroup$ – Bob Hanlon Dec 21 '19 at 15:10
5
$\begingroup$

You aren't doing anything wrong. However, FindMaximum is a search algorithm, not a solver. In the form you use it stops when it finds one solution.

If you add the region constraint to your Solve expression you would see

Solve[Sin[x] Sin[2 y] == 1 && {x, y} ∈ Rectangle[{0, 0}, {4, 4}], {x, y}]

{{x -> π/2, y -> π/4}, {x -> π/2, y -> (5 π)/4}}

which finds both maxima.

| improve this answer | |
$\endgroup$
1
$\begingroup$

To be learned in school, the maxima and minima of a function are located, where the first derivative is zero.

So one has in two dimensions to task to calculate the gradient and the Hessian Matrix.

Your are lucky that this is a Mathematica help example:

FindMaximum[Sin[x] Sin[2 y], {x, y},Gradient -> {Cos[x] Sin[2 y], 2 Cos[2 y] Sin[x]}, Method -> {"Newton", Hessian -> {{-Sin[x] Sin[2 y], 
  2 Cos[x] Cos[2 y]}, {2 Cos[x] Cos[2 y], -4 Sin[x] Sin[2 y]}}}]

On the page for FindMaximum.

Smallest positive solution is {x -> π/2, y -> π/4}

The example from Mathematica shows, one has to think further on ones own. Both factors of the given function are trigonometric and therefore periodical. One factor has the frequency 1, the other two. So all one has to do after the work You have already done is to add this periodical to the solution. And then restrict them to the region given.

So there is a grid of solutions starting in the positive quadrant with the solution You have calculated. Add 2 Pi or Pi to the component values of the solution and compare to 0 or 4 will to the job.

There is a speciality in the discussion not covered with periodicity.

There is a relative maximum. That You already saw on the border of the given rectangle. So to complete the job restrict the two dimensional function on the border lines.

x==0 is constant, so no maximum there. y==0 is constant, so no maximum there. x==4 is Sin[4]Sin[2y]=-0.756802 Sin[2y] starts with 0 and gets negative for positive y. The maximum is smaller than y=4, so {{4,0},{4,3π/2}} are maxima in the given region. y==4 is Sin[x]Sin[8]=0.989358 Sin[x] starts positive and rises for positive x.

That is the complete job and does contain the mathematical full and accepted procedure for this task.

(i) Find the minima/maxima in the open given region. (ii) Find minima/maxima on the border of the region.

Sorry that Solve is not much better. It is just equal and saves You to think like a Mathematician.

My methodology confirms the steps already done by Bob Halon, but presents the real world maths to be done and requested by staff for example in exams. No searching fo r a good starting point is needed, just straight forward math.

The points with maxima in the region {{0,4},{0,4}} are

{{x -> π/2, y -> π/4}, {x -> π/2, y -> (5 π)/4},{4,0},{4,3π/2}}.

Plot

The methods choosen behind the function FindMaximum are state of the art or really fast methods. Wolfram Inc. does not offer too much information about what the function really are. If You once are in the methodology of calculating maxima and minima in Mathematical Numerics there will be only a few worth to be choosen. A nice start is From Curve Fitting to Machine Learning: An Illustrative Guide to Scientific ... from Achim Zielesny. For further insight You have to study the references therein.

| improve this answer | |
$\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.