1
$\begingroup$

I have this table of values, how do I find the maximum value? i.e. find $z_{max}(x,y)$

tt1 = Table[Solve[z^2 == x^2 y - z, z, Method -> Reduce], {x, 0, 5, 1}, {y, 0, 
    5, 1}] 

By visual inspection, clearly max $z$ occurs at the most bottom right element in matrix, $z = \frac{1}{2}(-1 + \sqrt{501}) $

How do I get Mathematica to read out the position and value of maximum z?

I tried using:

Max[tt1]

but it didn't work..

$\endgroup$
6
  • 1
    $\begingroup$ Remove the // MatrixForm. $\endgroup$
    – Öskå
    Commented Aug 1, 2014 at 10:38
  • $\begingroup$ tt1 = Table[z /. Solve[z^2 == x^2 y - z, z, Method -> Reduce], {x, 0, 5, 1}, {y, 0, 5, 1}]; Max[tt1]. Screenshot here. $\endgroup$
    – Öskå
    Commented Aug 1, 2014 at 10:39
  • $\begingroup$ For pedagogical purposes, in addition to removing MatrixForm as Oska mentioned, you need to convert your Rules to values, which is what the z /. ... modification is doing. $\endgroup$ Commented Aug 1, 2014 at 11:14
  • 1
    $\begingroup$ @Öskå That works, but how do I tell what's the value of (x,y) at that point? $\endgroup$
    – user44840
    Commented Aug 1, 2014 at 11:47
  • $\begingroup$ A ref: functions that return rules, which elaborates on Oska's comment. $\endgroup$
    – Michael E2
    Commented Aug 1, 2014 at 14:32

3 Answers 3

1
$\begingroup$

Because of this comment the following becomes too long to be just a comment so here you go:

tt1 = Table[
   z /. Solve[z^2 == x^2 y - z, z, Method -> Reduce], {x, 0, 5,1}, {y, 0, 5, 1}];
Max[tt1]
Table[{x, y}, {x, 0, 5, 1}, {y, 0, 5, 1}][[Sequence @@ (First@Position[tt1, Max[tt1]])[[;; 2]]]]
1/2 (-1 + Sqrt[501])
{5, 5}
$\endgroup$
2
  • $\begingroup$ That's brilliant! $\endgroup$
    – user44840
    Commented Aug 1, 2014 at 12:05
  • $\begingroup$ @user44840 I'm glad you like it :) It's nothing fancy really :) $\endgroup$
    – Öskå
    Commented Aug 1, 2014 at 12:07
3
$\begingroup$

We can adapt Sjoerd's solution to the question, Table - find index of the maximum element. Other methods may be found here: List manipulation: position & max value combination.

tt1 = Flatten[
   Table[Thread@{x, y, z /. Solve[z^2 == x^2 y - z, z, Method -> Reduce]},
    {x, 0, 5, 1}, {y, 0, 5, 1}],
   2];

Then this yields {x, y, max}:

tt1 ~Part~ Last @ Ordering @ tt1[[All, 3]]
(*
  {5, 5, 1/2 (-1 + Sqrt[501])}
*)
$\endgroup$
1
$\begingroup$

Too long for a comment but... For your specific setup this can be a way with a v10 function:

tt1 = Table[Solve[z^2 == x^2 y - z, z, Method -> Reduce], {x, 0, 5, 1}, {y, 0, 5, 1}]
With[{v = z /. tt1}, With[{m = Max[v]}, {m, Most@FirstPosition[v, m]}]]

But... Why don't you solve analitically the problem? The command

z /. Solve[z^2 == x^2 y - z, z]

gives

{1/2 (-1 - Sqrt[1 + 4 x^2 y]), 1/2 (-1 + Sqrt[1 + 4 x^2 y])}

so it's not surprising the maximum is located at lower-right corner of the matrix, where $x=y=5$, and here

%[[2]] /. {x -> 5, y -> 5}

gives:

1/2 (-1 + Sqrt[501])
$\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.