# Results of ArgMax as a function of a parameter with InterpolatingFunctions

I would like to obtain a function and its derivatives, where the function is defined as the solution to a maximization problem. The obvious approach

Clear[f]
f[a_?NumericQ] := NArgMax[-(x - a)^2, x]
f
f'


fails to give the numeric f'. So I used the following:

Clear[f]
f = FunctionInterpolation[ArgMax[-(x - a)^2, x], {a, 0, 2}]
f'


The actual function I would like to maximize has no closed form, thus an InterpolatingFunction object must be used:

Clear[f, g]
g = FunctionInterpolation[-(x - a)^2, {x, 0, 2}, {a, 0, 2}];
f = FunctionInterpolation[ArgMax[g[x, a], x], {a, 0, 2}]
f'


This gives the error

FunctionInterpolation::nreal: Near {a} = {0}, the function did not evaluate to a real number.

How to avoid this error and get the correct f'[a] for any a?

## 1 Answer

Clear[f, g, gg]
g = FunctionInterpolation[-(x - a)^2, {x, 0, 2}, {a, 0, 2}];
gg[a_?NumericQ, b_?NumericQ] := g[a, b];
f = FunctionInterpolation[ArgMax[{gg[x, a], 0 <= x <= 2}, x], {a, 0, 2}]
f'

(*
InterpolatingFunction[{{0.,2.}},<>]
1.
*)