3
$\begingroup$

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)

$\endgroup$
4
$\begingroup$

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.

|improve this answer|||||
$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – J. M.'s technical difficulties Nov 7 '17 at 21:17
  • $\begingroup$ @J.M. Good idea. $\endgroup$ – MathX Nov 7 '17 at 21:38
3
$\begingroup$
f[a_] := FindMaximum[{3 x, x < 1}, x][[2, 1, 2]]
|improve this answer|||||
$\endgroup$
  • $\begingroup$ honestly, slicing lists in mathematica is totally unintuitive $\endgroup$ – andy Nov 7 '17 at 17:36
  • 1
    $\begingroup$ @andy, well, it's unintuitive if you don't have sufficient practice, sure. $\endgroup$ – J. M.'s technical difficulties Nov 7 '17 at 21:16
2
$\begingroup$

Perhaps it is more intuitive if we use rules:

FindMaximum[{3 x, x < 1}, x] /. {result_, {x -> _}} -> result 
|improve this answer|||||
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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