0
$\begingroup$

I am trying to find the maximum value in a list. I can find the maximum value, but how can I find the value of the associated parameter at the maximum? (I don't want the index of the element in the list of the parameter since the actual list (of t in the example) is so long, so I want to get t=4 for the following example instead of its index:3)

 v = Table[
  TR1 + TR2 /. Flatten[{Solve[TR1 - t == 0, TR1], TR2 -> t + 1}], {t, 
   2, 4, 1}]

tstar = Max[v]
$\endgroup$
2
  • 1
    $\begingroup$ Could do v = Table[{t, TR1 + TR2 /. Flatten[{Solve[TR1 - t == 0, TR1], TR2 -> t + 1}]}, {t, 2, 4, 1}]; tstar = MaximalBy[v, Last][[1, 1]] $\endgroup$
    – march
    Commented Sep 14, 2017 at 21:15
  • $\begingroup$ list = RandomReal[1, 100]; Position[list, Max[list]] $\endgroup$
    – anderstood
    Commented Sep 15, 2017 at 0:30

2 Answers 2

1
$\begingroup$
t = Range[2, 4, 1]
v = (TR1 + TR2 /. 
     Flatten[{Solve[TR1 - # == 0, TR1], TR2 -> # + 1}] &) /@ t
t[[Ordering[v, -1]]]
$\endgroup$
1
$\begingroup$

For you example, the following will work.

 Maximize[{1 + 2 t, 1 <= t <= 4 && t ∈ Integers}, t][[2, 1, 2]]

4

It will work for functions that are not monotonic as well.

Maximize[{Sin[π t/2], 0 <= t <= 4 && t ∈ Integers}, t][[2, 1, 2]]

1

In the above two examples the global maximum falls on an integer value, but this does not have be the case. In the following example, the global maximum is at π/2, but the integer maximum is at 2.

Maximize[{Sin[ t], 0 <= t <= 4 && t ∈ Integers}, t][[2, 1, 2]]

2

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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