# Finding exact local extrema of simple functions

I want to exactly find all the local maxima of

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


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

The correct answer is that we have global maxima at x = -Sqrt 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) && (x <= 1 + Pi/10)}, x]


This finds the point x=0.

Iteration 2:

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


This finds the point x = -Sqrt.

Iteration 3:

Maximize[{x^4/4 - x^2/2, (x >= -Sqrt) && (x <= 1 + Pi/10) && (x != 0) && (x != -Sqrt)}, 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.

• If your function going to be twice differentiable?
– Kuba
Jan 29, 2014 at 8:33
• 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. Jan 29, 2014 at 8:34
• @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). Jan 29, 2014 at 19:25
• Ahhh... you should definitely add this info to the question. Jan 29, 2014 at 20:15

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], #[] <= x && x <= #[]}, x] & /@
Partition[
Flatten@{#[],
Solve[D[#[], x] == 0 && #[] <= x <= #[], x][[All, 1,
2]], #[]}, 2, 1]] &[{-Sqrt, 1 + Pi/10, (x^4)/4 - (x^2)/2}] • 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! Jan 31, 2014 at 19:40