# How to find the minimum at each iteration?

I would like to iterate the table for {Q,0,3,1}, {R0,1,5,1}, the values of m=[3.83171, 7.01559, 10.1735, 13.3237, 16.4706, 19.6159, 22.7601, 25.9037] and calculate the corresponding k and Ra. Also, I want to indicate the minimum Ra for each value of R0.
This is the code that I have written, but I don’t seem to get what I’m looking for.

ClearAll["Global*"]
p = 2;
k[R0_, m_] := R0/m;
Ra[Q_, R0_, m_] := (Q*R0*p)/m;
data = Table[{Q, R0, m, k[R0, m], Ra[Q, R0, m]}, {Q, 1, 3, 1}, {R0, 1, 5, 1}, {m, 3.83171, 7.01559, 1}];
data // Flatten[#, 1] & // Grid;
TableForm[data,TableHeadings -> {{}, {},{}, {"Q", "R0", "m", "k", "Ra"}}]


I’m expecting the output to be something similar to this. Here I have shown the table for Q=1 as an example.

• There is a missing comma: {Q, 1, 3, 1} {R0, 1, 5, 1}? Mar 11, 2023 at 9:30
• Oh that was an error. I've edited it now. @DanielHuber Mar 11, 2023 at 14:12
• How can I run the code for m=[3.83171, 7.01559, 10.1735, 13.3237, 16.4706, 19.6159, 22.7601, 25.9037] and indicate the minimum value of Ra for each R0? Any help would be appreciated. Mar 11, 2023 at 14:14
• Ra depends on Q, R0, and m. You want the minimum relative to m. But what about Q? Mar 11, 2023 at 18:02

How to iterate over specific values of m is well documented for Table: http://reference.wolfram.com/language/ref/Table.html

But there are issues of data manipulation and display that might be worth explicating. So, here is our data:

data =
Table[
{Q, R0, m, k[R0, m], Ra[Q, R0, m]},
{Q, 1, 3, 1},
{R0, 1, 5, 1},
{m, {3.83171, 7.01559, 10.1735, 13.3237, 16.4706, 19.6159, 22.7601, 25.9037}}]


It looks like you want each indidual "cell" to be shown with table headers, so let's build a special function to do that (and we'll highlight the minimum cell while we're at it):

FormatCells[table_] :=
With[
{minPosition = OrderingBy[table, Last, 1]},
MapAt[
Item[#, Background -> Yellow] &,
TableForm[{#}, TableDirections -> Row, TableHeadings -> {None, {"Q", "R0", "m", "k", "Ra"}}, TableSpacing -> {.8, .8}] & /@ table,
minPosition]]


The Item won't mean anything outside of a Grid expression, but we'll use Grid to format the larger structure (with yet another special function).

FormatTable[table_] := Grid[FormatCells /@ table, Dividers -> All]


Now, we can get one "chunk" of data and display it:

FormatTable[data[[1]]]


UPDATE

For versions of Mathematica prior to 12, use this definition of FormatCells instead:

FormatCells[table_] :=
With[
{minPosition = Ordering[table, 1, Last[#1] < Last[#2] &]},
MapAt[
Item[#, Background -> Yellow] &,
TableForm[{#}, TableDirections -> Row, TableHeadings -> {None, {"Q", "R0", "m", "k", "Ra"}}, TableSpacing -> {.8, .8}] & /@ table,
minPosition]]

• The first three code snippets work just fine in Mathematica (I am getting the output for the first and no output for the other two). But, the fourth code snippet gives me the following error: ________ MapAt : Position specification OrderingBy[{{1,1,3.83171,k[1,3.83171],Ra[1,1,3.83171]}, ,<<4>>,1] in MapAt[#1&,{{{Q, 1}, {R0, 3},{m, 3.83171},{k, k[3,3.83171]},{Ra, Ra[1,3,3.83171]}},<<1>>] is not a machine-sized integer or a list of machine-sized integers. _____Note that I have not included the complete data in between the error due to character exceed. @lericr Mar 11, 2023 at 17:42
• Yes, sorry, that was a refactoring error as I was experimenting with the code. Try the updated version. Mar 11, 2023 at 18:00
• Oh, wait. You may have an additional problem. You still need to define p, k, and Ra. I just took them from your original post, and I didn't replicate them in my answer. Mar 11, 2023 at 18:02
• Oh got you!. But I still have the same error :( ____MapAt::psl: Position specification OrderingBy[<<1>>] in MapAt[<<1>>] is not a machine-sized integer or a list of machine-sized integers. @lericr Mar 11, 2023 at 18:08
• The "top" level in your data iterates over the Q values. I.e., data[[1]] gives us the sub-table for the first value of Q. data[[2]] would give the second, and so forth. So, to see the same sort of display for the second value of Q you'd execute FormatTable[data[[2]]]. Mar 11, 2023 at 20:07