I have this table of values, how do I find the maximum value? i.e. find zmax(x,y)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 zz occurs at the most bottom right element in matrix, z=12(−1+√501)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..

=================

1

Remove the // MatrixForm.

– Öskå

Aug 1 ’14 at 10:38

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.

– Öskå

Aug 1 ’14 at 10:39

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.

– bobthechemist

Aug 1 ’14 at 11:14

1

@Öskå That works, but how do I tell what’s the value of (x,y) at that point?

– user44840

Aug 1 ’14 at 11:47

A ref: functions that return rules, which elaborates on Oska’s comment.

– Michael E2

Aug 1 ’14 at 14:32

=================

3 Answers

3

=================

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}

That’s brilliant!

– user44840

Aug 1 ’14 at 12:05

@user44840 I’m glad you like it 🙂 It’s nothing fancy really 🙂

– Öskå

Aug 1 ’14 at 12:07

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])}

*)

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=5x=y=5, and here

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

gives:

1/2 (-1 + Sqrt[501])