I'm trying to find the place on a highway that's furthest from it's nearest gas station. Here are the functions I'm using.
dist[x_?NumericQ, stations_] := Abs[x - Nearest[stations, x][[1]]];
maxdist[lowerbound_, upperbound_, stations_] :=
NMaximize[{dist[x, stations], lowerbound < x < upperbound}, x];
Here are the mile markers of my gas stations:
stations={22, 25, 27, 31, 34, 42, 47, 52, 54, 62, 63, 70, 71, 74, 78, 80,
84, 101, 106, 110, 115, 136, 137, 143, 149, 151, 154, 169, 174, 175, 176,
182, 184, 196, 206, 215, 220, 221, 226, 231, 245, 254, 257, 264, 270,
272};
maxdist[20,272,stations]
yields {8.5,{x->92.5}}
But that's obviously not the largest, as dist[125.5,stations]
gives 10.5
. Why is NMaximize
not finding that global maximum?
A side note: There is an obvious workaround that avoids NMaximize
(see below), but I'm curious to know why NMaximize
isn't working.
maxdist2[lowerbound_, upperbound_, stations_] := Max[Table[dist[x,
stations], {x, lowerbound, upperbound, 0.5}]]
NMaximise
is not guaranteed to find a global maximum.Plot[dist[x, stations], {x, 20, 272}]
$\endgroup$ – george2079 Jun 23 '17 at 17:06Nearest[]
is making it hard for it. $\endgroup$ – Shane Jun 23 '17 at 17:11if linear
..otherwise
bullet items under details that bite you here. $\endgroup$ – george2079 Jun 23 '17 at 17:13NMaximize
than the one I got! Thanks for the explanation! $\endgroup$ – Shane Jun 23 '17 at 17:16