1
$\begingroup$

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. enter image description here

$\endgroup$
4
  • $\begingroup$ There is a missing comma: `{Q, 1, 3, 1} {R0, 1, 5, 1}? $\endgroup$ Mar 11, 2023 at 9:30
  • $\begingroup$ Oh that was an error. I've edited it now. @DanielHuber $\endgroup$ Mar 11, 2023 at 14:12
  • $\begingroup$ 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. $\endgroup$ Mar 11, 2023 at 14:14
  • $\begingroup$ Ra depends on Q, R0, and m. You want the minimum relative to m. But what about Q? $\endgroup$ Mar 11, 2023 at 18:02

1 Answer 1

3
$\begingroup$

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

enter image description here

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]]
$\endgroup$
16
  • $\begingroup$ 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 $\endgroup$ Mar 11, 2023 at 17:42
  • $\begingroup$ Yes, sorry, that was a refactoring error as I was experimenting with the code. Try the updated version. $\endgroup$
    – lericr
    Mar 11, 2023 at 18:00
  • $\begingroup$ 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. $\endgroup$
    – lericr
    Mar 11, 2023 at 18:02
  • $\begingroup$ 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 $\endgroup$ Mar 11, 2023 at 18:08
  • 1
    $\begingroup$ 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]]]. $\endgroup$
    – lericr
    Mar 11, 2023 at 20:07

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.