Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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[1]
f'[1]

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

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

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'[1]

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

1 Answer 1

up vote 3 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}]
f'[1]

(*
InterpolatingFunction[{{0.,2.}},<>]
1.
*)
share|improve this answer

Your Answer

 
discard

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.