Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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

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

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

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}]

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?

share|improve this question
up vote 4 down vote accepted
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}]

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.