I have a large system of ODEs, and I find a large number of steady-state solutions. But many of them do not satisfy the condition that all the variables must have positive or zero values at the steady-state.

I like to:

  1. retrieve only the solutions that satisfy the condition above;
  2. sort the variables in a solution with respect to the variable (integer) subscript; and
  3. put all the retrieved and sorted solutions in a table column-wise (each column should have a solution)

I have already done what I request above by using a mixture of If, AppendTo and Table commands but since the list is too long and it takes much time to achieve what I want, plus it looks ugly.

A minimal example of the desired output is:

TableForm[{{0.5, 2}, {1, 3}, {2, 1}}, 
 TableHeadings -> {{"\!\(\*SubscriptBox[\(x\), \(1\)]\)", 
    "\!\(\*SubscriptBox[\(x\), \(2\)]\)", 
    "\!\(\*SubscriptBox[\(x\), \(3\)]\)"}, {"solution 1", 
    "solution 2"}}]


Below is a subset of solutions that directly come out of the model.

{Subscript[x, 1][t] -> -331, Subscript[x, 3][t] -> -1.8, 
  Subscript[x, 4][t] -> -32, Subscript[x, 2][t] -> -1.37, 
  Subscript[x, 6][t] -> -49.2, 
  Subscript[x, 5][t] -> -98},
{Subscript[x, 3][t] -> -1.84, 
  Subscript[x, 2][t] -> 1.6, Subscript[x, 4][t] -> 141.9, 
  Subscript[x, 1][t] -> 334.1, Subscript[x, 5][t] -> 0, 
  Subscript[x, 6][t] -> 10},
{Subscript[x, 6][t] -> 0, Subscript[x, 1][t] -> 35.13, 
  Subscript[x, 2][t] -> 0, Subscript[x, 4][t] -> 0, Subscript[x, 
  3][t] -> 2.92, Subscript[x, 5][t] -> 10.41},
{Subscript[x, 1][t] -> 11.17, 
  Subscript[x, 3][t] -> 14.7, Subscript[x, 4][t] -> 2.3, 
  Subscript[x, 2][t] -> -6.6, Subscript[x, 5][t] -> 3.3, 
  Subscript[x, 6][t] -> -7.13},
{Subscript[x, 1][t] -> 1, 
  Subscript[x, 2][t] -> 4, Subscript[x, 6][t] -> 0, 
  Subscript[x, 3][t] -> 3, Subscript[x, 5][t] -> 6, 
  Subscript[x, 4][t] -> 0}

The above set has 5 solutions, shown with {...} for each solution. As one may easily see, the variables in each solution are in somewhat mixed order with respect to variable subscripts. Here is a brief explanation for preparing the table.

  1. The solutions should be sorted with respect to the variable subscripts, such as x1, x2, x3, etc.
  2. Identify the solutions in which all variables have non-negative values, and place them in the early columns of the table according to the number of non-negative values of the variables. The more the non-negative values in a solution, the earlier the column position in the table.
  3. Priority with respect to the column position should be given to those non-zero solutions with the highest number of non-negative values.
  4. In case of multiple non-positive solutions, the ordering and column position of the solution set is not important.


Here is the table of solutions I refer to in my comment.

enter image description here

The solution 2 should be in the first column of the table.

  • $\begingroup$ @Bill: I will put some input in the edited version of the question and explain it in more detail what I request from this forum. Thanks. $\endgroup$ Oct 29, 2018 at 20:50

1 Answer 1

list = SortBy[#, #[[1, -1]] &] & /@ 
  SortBy[Select[solutions /. a_[t] :> a, And @@ NonNegative[#[[All, 2]]] &], 
   {Count[#[[All, -1]], _?Negative] &, -Count[#[[All, -1]], _?Positive] &}];
rlabels = ToString[#, StandardForm] & /@ list[[1, All, 1]];
clabels = "solution" <> ToString[#] & /@ Range[Length@list];
values = list[[All, All, 2]];
TableForm[Transpose @ values, TableHeadings -> {rlabels, clabels} , 
 TableAlignments -> Center]

enter image description here

If you want to keep all solutions: change list above to

list = SortBy[#, #[[1, -1]] &] & /@ 
   SortBy[solutions /. a_[t] :> a, 
    {Count[#[[All, -1]], _?Negative] &, -Count[#[[All, -1]], _?Positive] &}];

enter image description here

  • $\begingroup$ Thanks very much for this neat code. It is a useful piece for many of us for making smooth presentations of the data. Thanks again. $\endgroup$ Oct 29, 2018 at 23:00
  • $\begingroup$ @Tugrul, my pleasure. Thank you for the accept. $\endgroup$
    – kglr
    Oct 29, 2018 at 23:07
  • $\begingroup$ Maybe one slight revision is needed in your code. In the set of solutions, one of the solutions has all variables zero, and your code places this solution in the very first column of the table, although there are other solutions with non-zero and all positive values which are supposed to be before the zero solution. I will put the image of the table in the edited question for you to see what I mean. Thank you. $\endgroup$ Oct 29, 2018 at 23:15
  • $\begingroup$ Maybe I am confusing you. If so, I apologize. The table I inserted in EDIT 2 is made up from my solution set, not the set posted in the question, which does not have a zero solution. $\endgroup$ Oct 29, 2018 at 23:25
  • $\begingroup$ @Tugrul, hope it is fixed now. $\endgroup$
    – kglr
    Oct 29, 2018 at 23:30

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.