# Find all points that have the same minimum? [duplicate]

Possible Duplicate:
How to find all the local minima/maxima in a range

Sin[x] have two points ($3\pi/2,7\pi/2$) where the same minimum ($-1$) is achieved at the range from $0$ to 4 $\pi$. I don't know how to get two points at once, if using Minimize, only only one point can be got.

Minimize[{Sin[x], 0 <= x <= 4 Pi}, x]


the output:

{-1, {x -> (7 \[Pi])/2}}

And the last sentence of the information about Minimize gives a definitely declaration, which almost cut off this road:

Even if the same minimum is achieved at several points, only one is returned.

Other similar function like FindMinimum seems to be helpless either,so are there any methods to help achieve that goal?

-

## marked as duplicate by J. M.♦Sep 16 '12 at 9:09

Have you tried using Solve? For example: Solve[Sin[x] == -1 && 0 <= x <= 4 π, x]? If you don't know that -1 is the minimum, use the value from First@Minimize[...] – R. M. Sep 16 '12 at 4:44
@R.M Thanks for your comment, which provide a good approach.Would you like to post your answer and then I can accept it. – withparadox2 Sep 16 '12 at 5:35
I would, but I think this is a duplicate (seen a few like this and I might have answered it) and I'm too sleepy now to search for it. Feel free to write it down as your own answer and add a couple of words of explanation :) – R. M. Sep 16 '12 at 6:07
Have a look at the method by Daniel in the dupe question linked to. – J. M. Sep 16 '12 at 9:13

Minimize always looks for global minimum. One way to find all minimums is to use random application of FindMinimum which looks for local minimums. I will intentionally use more complicated function:

fun[x_] :=Cos[x] + 2 Sin[5 x]


The following code produces 100 random points to use as seeds for FindMinimum:

sol = {#, fun[#]} & /@ Union[Round[x /. (FindMinimum[{fun[x], .5 < x < 8 Pi},
{x, #}] & /@ RandomReal[{.5, 8 Pi}, 100])[[All, 2]], .00001]]


{{0.95887, -1.41884}, {2.21512, -2.59426}, {3.44969, -2.95199}, {4.69236, -2.01001}, {5.96272, -1.04992}, {7.24205, -1.41884}, {8.49831, -2.59426}, {9.73287, -2.95199}, {10.9755, -2.01001}, {12.2459, -1.04992}, {13.5252, -1.41884}, {14.7815, -2.59426}, {16.0161, -2.95199}, {17.2587, -2.01001}, {18.5291, -1.04992}, {19.8084, -1.41884}, {21.0647, -2.59426}, {22.2992, -2.95199}, {23.5419, -2.01001}, {24.8123, -1.04992}}

where above are the pairs of minimums and their function values. Separate those minimums that have the same function value:

pts = GatherBy[Round[sol, .00001], #[[2]] &];
Framed /@ pts // Column


Now, you can see all minimums were found and same ones are distinct with the same color:

Plot[fun[x], {x, 0, 8 Pi},
Epilog -> ({Hue[RandomReal[]], PointSize[.02], Point[#]} & /@ pts)]


The more minimums you have the more seeds you need to use.

-

Another possibility is to use RootSearch; using the same example as Vitaliy Kaurov for comparison.

This finds all roots of the derivative (candidate minima):

all = RootSearch[fun'[x] == 0 , {x, 0, 8 π}]


then we select the true minima:

good = Select[all, fun''[#[[1, 2]]] > 0 &]


A quick comparison :

Length[good] == Length[sol]
(* True *)

Max[Abs[#] & /@ (good[[All, 1, 2]] - sol[[All, 1]])]
(* 0.0000448352 *)

-