# How can I find the roots of the maximizer of a function? [duplicate]

Suppose I define $x(b) = \arg \max (-x^2/2+bx)$. I'd like to find the values of $b$ such that $x(b) = 0$. I tried running the code

x[b_] := x /. FindMaximum[-x^2/2 + b x, x][[2]]
FindRoot[x[b] == 0, {b, 0}]


but I get an error, even though the function x seems to work. I know this is a somewhat trivial example (since $x(b) = b$), but in the application I have in mind the maximizer may not have a closed form expression.

Thanks!

• Use _?NumericQ: x[b_?NumericQ] := x /. FindMaximum[-x^2/2 + b x, x][[2]]; – Michael E2 Oct 11 '16 at 19:41
• Specifically, see this answer – Michael E2 Oct 11 '16 at 19:42

Use NMaximize instead of FindMaximum, with _?NumericQ as pointed out by Michael E2 in the comments (I changed the name of the function from x[b] to max[b] because I simply dislike naming different things with the same symbol):

max[b_?NumericQ] := x /. NMaximize[-x^2/2 + b x, x][[2]]

max[1]


1

Plot[max[b], {b, -1, 1}]


FindRoot[max[b] == 0, {b, 0}]


{b -> 0.}

With a different example:

max[b_?NumericQ] := x /. NMaximize[Sin[b x] + Cos[x] - x^2 + (b - 1)/b x, x][[2]];

max[0.5]


-0.167512

Plot[max[b], {b, 0.1, 1}]


FindRoot[max[b] == 0, {b, 0.5}]


{b -> 0.618034}

• What determines whether Maximize, NMaximize, or FindMaximum work in this context? – David Oct 11 '16 at 19:40