0
$\begingroup$

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!

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

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}

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

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