# Extract output from FindMaximum inside a function

I have a function similar to this

 f[a_] := FindMaximum[{3 x, x < 1}, x]


which outputs

 {3., {x -> 1.}}


how do I change it so that the output is just the value that maximizes f (i.e. no lists, no arrows, just the real number)

FindMaximum results in an output in the format of

{fmax, {x -> xmax}}


If you only want the maximum value fmax, you can use FindMaxValue[] and if you only want xmax then you can use FindArgMax[]. In your case, the output of FindArgMax[] is a list that contains only one number. There are several ways to extract it. I used First:

First@FindArgMax[{3 x, x < 1}, x]


But why do you have it as a function of a? You don't have a in your function definition.

• This is probably the best solution for somebody who gets tripped up on extracting stuff from lists. For completeness, I would suggest that you discuss FindMaxValue[] as well. – J. M.'s technical difficulties Nov 7 '17 at 21:17
• @J.M. Good idea. – MathX Nov 7 '17 at 21:38
f[a_] := FindMaximum[{3 x, x < 1}, x][[2, 1, 2]]

• honestly, slicing lists in mathematica is totally unintuitive – andy Nov 7 '17 at 17:36
• @andy, well, it's unintuitive if you don't have sufficient practice, sure. – J. M.'s technical difficulties Nov 7 '17 at 21:16

Perhaps it is more intuitive if we use rules:

FindMaximum[{3 x, x < 1}, x] /. {result_, {x -> _}} -> result