0

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!

David
  • 101
  • 1

1 Answers1

0

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

enter image description here

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

enter image description here

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

{b -> 0.618034}

corey979
  • 23,947
  • 7
  • 58
  • 101