# Minimize a function with singular points ( limit exists)

I need to minimize a function that has several problem points (divergences).

For example, if I consider:

f[xx_] := Limit[Sin[x]/x, x -> xx];
FindMinimum[{f[xx], 5 >= xx && xx >= -5}, {xx, 0}]


The FindMinimum falls due to the divergence that exists at x = 0 and does not consider the limit of the function.

Is there any function to do that?. I have thought about using NMinimize but I don't know if it will be as effective as FindMinimum.

The sin (x) / x function was an example, the function I have to evaluate has multiple repairable divergences whose points I don't know.

I tried the following but it doesn't work either:

ff[x_] := Sin[x]/x;

g[xx_] := Piecewise[{{Limit[ff[x], x -> xx], SameQ[ff[xx], Indeterminate]}, {ff[xx], NumericQ[ff[xx]] == True}}]

FindMinimum[g[xx], {xx, 0}]


It does not yield a value, findfinimum fails

I would recommend using the built-in Sinc[] function as it already knows that Sinc == 1.

Most (I imagine all) minimization routines are sensitive to the starting point. If you give them a bad starting point, you're going to get a bad result. The point x = 0 is pretty much the worst possible starting point for most algorithms because of the shape of the Sinc function. There are a number of possible solutions:

FindMinimum[Sinc[x], x]
FindMinimum[{Sinc[x], -5 <= x <= 5}, {x, 1}]
NMinimize[Sinc[x], x]


All of these return either {-0.217234, {x -> -4.49341}} or {-0.217234, {x -> 4.49341}} which are the two, identical minima.

Some alternatives to Sinc:

ClearAll[f];
f[xx_?NumericQ] := Limit[Sin[x]/x, x -> xx];
FindMinimum[{f[xx], 5 >= xx && xx >= -5}, {xx, 0}]
(*  {1., {xx -> 0.}}  *)

ClearAll[f];
f[xx_] := Piecewise[
{{Sin[xx]/xx, xx != 0}},
Limit[Sin[x]/x, x -> 0]
];
FindMinimum[{f[xx], 5 >= xx && xx >= -5}, {xx, 0}]
(*  {1., {xx -> 0.}}  *)


Reference:

Clear[f]


Only define the function in the limit at the singular points.

f[x_ /; x == 0] = Limit[Sin[x]/x, x -> 0];

f[x_] := Sin[x]/x;

sol1 = FindMinimum[{f[x], -5 <= x <= 5}, x]

(* {-0.217234, {x -> 4.49341}} *)


or

sol2 = NMinimize[{f[x], -5 <= x <= 5}, x]

(* {-0.217234, {x -> -4.49341}} *)


The different results are equivalent since

f[x] == f[-x]

(* True *)


Note that the symbolic evaluation is only possible because the definition of f is not restricted to numeric arguments.