# Hot to single out numeric values from NMaximize

I have some problem with NMaximize.

The function NMaximize gives result like this

NMaximize[f[x],x]
{f[xmax] ,{x -> xmax}}


I've read in various answers that I can isolate xmax by writing:

NMaximize[f[x],x][]


And I can get rid of the arrow, in order to obtain the numeric value of xmax, using /.

y /. NMaximize[f[x],x][]


But then, when I type y, Mathematica doesn't give me back the numeric value of xmax, it just returns y again... What's wrong?

• Did you try x /. NMaximize[f[x],x][]? – Michael E2 May 24 '18 at 11:42
• Nope, but I don't want to assign to x any value, I need x to be a variable – Cella May 24 '18 at 11:56
• Use @MichaelE2 to insure I'm pinged about your response. -- I didn't assign any value to x; it's still a variable. Do you want something like x1 = x /. NMaximize[f[x],x][]? Then the value is assigned to a symbol, namely x1, not x. (Did you actually try my previous suggestion? Please do.) – Michael E2 May 24 '18 at 12:03
• Oooh, now I get it. Thank you, it worked – Cella May 24 '18 at 12:06

First example from the docs for NMaximize:

Find the global maximum of an unconstrained problem:

NMaximize[-x^4 - 3 x^2 + x, x]

{0.0825888, {x -> 0.16374}}


Extract the maximizing argument:

x /. Last[NMaximize[-x^4 - 3 x^2 + x, x]]

0.16374


Note that ReplaceAll (/.) replaces the symbol x with the number 0.16374. It does not assign a value to x. If you want to store the value in another variable, use something like this:

x1 = x /. Last[NMaximize[-x^4 - 3 x^2 + x, x]]

• There are also NArgMax and NMaxValue – ilian May 24 '18 at 13:34

you can write y=f[x] /. NMaximize[f[x],x][] or use y=FindMaximum[f[x], x][]