2
$\begingroup$

I want to exactly find all the local maxima of

f[x_] := x^4/4 - x^2/2

over the interval [-Sqrt[2], 1 + Pi/10].

The correct answer is that we have global maxima at x = -Sqrt[2] and x=0, and a local maximum at x= 1 + Pi/10.

Right now my approach is to iterate the Maximize command, which finds global maximums symbolically, and each time it finds a maximum I tell it to not find the same one again. Of course after it finds the two global maxima, it doesn't give me anything useful on the third application, as the one I am trying to find is merely local.

Now I could use FindMaximum, but that only yields a numerical approximation, and I want the exact value. Also note that in this case my algorithm fails to find the local maximum at an endpoint, but it would be easy to use a different function where the method would fail to find a local maximum properly inside the interval.

Iteration 1:

Maximize[{x^4/4 - x^2/2, (x >= -Sqrt[2]) && (x <= 1 + Pi/10)}, x]

This finds the point x=0.

Iteration 2:

Maximize[{x^4/4 - x^2/2, (x >= -Sqrt[2]) && (x <= 1 + Pi/10) && (x != 0)}, x]

This finds the point x = -Sqrt[2].

Iteration 3:

Maximize[{x^4/4 - x^2/2, (x >= -Sqrt[2]) && (x <= 1 + Pi/10) && (x != 0) && (x != -Sqrt[2])}, x]

Of course this throws a "no maximum in the region" error, when I would like it to find x = 1 + Pi/10.

I am open to patches for this approach or completely different algorithms as well.

$\endgroup$
4
  • $\begingroup$ If your function going to be twice differentiable? $\endgroup$
    – Kuba
    Jan 29, 2014 at 8:33
  • 1
    $\begingroup$ You could do it old-school: solve for f'[x]==0 and check the second derivatives plus look at the edges of your interval for local extrema. $\endgroup$
    – Yves Klett
    Jan 29, 2014 at 8:34
  • $\begingroup$ @Yves Klett and Kuba, good suggestions. Unfortunately the function isn't guaranteed to be twice-differentiable, and even when it is, the second-derivative test isn't guaranteed to give all extrema (e.g. in y = x^4). $\endgroup$
    – Twiffy
    Jan 29, 2014 at 19:25
  • 1
    $\begingroup$ Ahhh... you should definitely add this info to the question. $\endgroup$
    – Yves Klett
    Jan 29, 2014 at 20:15

1 Answer 1

2
$\begingroup$

Since you used "algorithms", I offer this probably fragile kludge. It uses a simple first derivative test bounded by your interval to find possibilities, then divides your interval using those, finally using Maximize over those to find results. You'd probably want to use more sophisticated (e.g. second derivative testing) checks, but perhaps it's a starting point for other ideas. Takes the lower & upper bounds, and the function, as arguments.

DeleteDuplicates[
   Maximize[{f[x], #[[1]] <= x && x <= #[[2]]}, x] & /@ 
    Partition[
     Flatten@{#[[1]], 
       Solve[D[#[[3]], x] == 0 && #[[1]] <= x <= #[[2]], x][[All, 1, 
         2]], #[[2]]}, 2, 1]] &[{-Sqrt[2], 1 + Pi/10, (x^4)/4 - (x^2)/2}]

enter image description here

$\endgroup$
1
  • $\begingroup$ Thanks Rasher, I think that's a good approach. I only added one refinement. My algorithm was 1) Find all critical pts (where first deriv = 0) 2) Cut the function along each of those points 3) Use Maximize / Minimize on the function restricted to each interval. There will be exactly one max and one min. 4) Any true local max, say, will only be a max in any of the intervals. If a point is a max in one interval and a min in another, it's a critical point but non-extremal, so remove it from your answer list. Thanks again! $\endgroup$
    – Twiffy
    Jan 31, 2014 at 19:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.