# NMaxValue with complex interpolation

I've encountered a very strange problem. I'm trying to find a minimum of a real part (or absolute value, or really any possible real-valued expression) of some complex function, which was previously interpolated from numerical data. Let's see here for the minimal "working" example:

func = Interpolation[Table[{x, Exp[I x]}, {x, -5, 5, 0.01}]];
NMaxValue[Re[func[x]], x]


The "result" of this is:

NMaxValue::cvmit: Failed to converge to the requested accuracy or precision within 100 iterations.
-ExperimentalNumericalFunction[{x}, -Re[
InterpolatingFunction[{{-5., 5.}}, <>][x]],
"-NumericalFunctionData-"][{-6.42031}]


Can someone point me in the right direction and hint on what the problem might be? It works when I do

func = Interpolation[Table[{x, Cos[x]}, {x, -5, 5, 0.01}]];
NMaxValue[func[x], x]


but my functions are usually complex.

Behavior is the same with NMaximize. What am i missing here? What is wrong with Re? What exactly is ExperimentalNumericalFunction? Thanks.

(too long for a comment)

What you saw was NMaximize[] (and thus NMaxValue[]) trying to create a function that is optimized for numerical evaluation, since it is expecting the objective function to be evaluated a lot of times (especially since NMaximize[] uses stochastic methods).

The cure (see this as well), as is usual for thwarting Mathematica's attempts to be smart with numerical function evaluations, is

func = Interpolation[N@Table[{x, Exp[I x]}, {x, -5, 5, 1/100}]];
ff[x_?NumericQ] := Re[func[x]]

NMaxValue[{ff[x], -5 < x < 5}, x]
1.
`

Note that I had also taken the liberty to constrain the independent variable; since you using an interpolating function anyway, the constraint ensures no extrapolation gets done.

• Well, as far as I'm concerned, this is the (or at least 'an') answer! Not sure who will get the rep for it tho, upvoting anyway. I will wait with accepting this as not to discourage anyone in case more solutions would come up. – user16320 Mar 12 '18 at 12:03