# FindMaximum is not working properly?

I'm trying to find maximum of the functions that I created by interpolating experimental data by using FindMaximum:

FindMaximum[{ii[x]}, {x, 41, 40, 57}, Method -> InteriorPoint]


but it works not very stable way. Here is the results of application of this function to 9 different curves. As you can see in the last curve it failed to find maximum, while the curve itself looks similar to others:

Any ideas why it happens and how I can fix it?

• It looks like you're looking for a point ii'[x]==0? If yes try NSolve[ii'[x]==0&&ii''[x]<0,40<x<60,x] Commented May 26, 2023 at 12:16
• If you provide no complete code and no data, we can provide no help... Commented May 26, 2023 at 16:21

FindMaximum searches for a local maximum. FindMaximum is working properly in your last example, it can find the global max in the bound value of your function. So, FindMaximum is working ok.

However, it looks like you're looking for points where your function has a local max.

For example, if your function is $$\dfrac{\sin(10\pi x^{3})}{2x} + (x-1/2)^{4}$$ and your range to search for the optimal values are from -1.3 to 1.3:

f = Sin[10 Pi x^3]/(2 x) + (x - 1/2)^4;
xLimits = {-1.3, 1.3};


You can use WhenEvent and NDSolve to find the points where f'[x] == 0:

{sol, points} =
Reap@NDSolve[{y'[x] == D[f, x],
y[xLimits[[1]]] == (f /. x -> xLimits[[1]]),
WhenEvent[y'[x] == 0, Sow[{x, y[x]}]]},
y[x], {x, xLimits[[1]], xLimits[[2]]}];


Plot the results:

Plot[f, {x, xLimits[[1]], xLimits[[2]]},
Epilog -> {PointSize[Medium], Red, Point[Flatten[points, 1]]},
PlotPoints -> 20]


You can do something similar if you want only the max local values.

points = Select[Flatten[points, 1], (D[f, {x, 2}] /. x -> #[[1]]) < 0 &];
Plot[f, {x, xLimits[[1]], xLimits[[2]]},
Epilog -> {PointSize[Medium], Red, Point[points]}, PlotPoints -> 20]