The general question is how can I loop on names and indices?

An example:

I have

pfun = ParametricNDSolveValue[{y'[t] == a y[t], y[0] == b}, y, {t, 0, 10}, {a,b}]

and I want

y11 = pfun[1, 1]
y12 = pfun[1, 2]

How can I generate a list of $yXY$ with $X=a$ and $Y=b$?

  • 3
    $\begingroup$ y = Table[pfun[a, b], {a, 1, 3}, {b, 1, 5}]? $\endgroup$ – Alan Apr 17 '19 at 11:31
  • $\begingroup$ y = pfun @@@ Tuples[{Range[3], Range[5]}]? $\endgroup$ – Henrik Schumacher Apr 17 '19 at 12:38

It is a common mistake of beginners to think they need names to refer to the results of ParametricNDSolveValue. I don't say naming is never needed, but it is very seldom needed.

Suppose you wanted to see a plot of the curves that you suggest labeling y11, y12, ..., y14. This can be done without labeling:

Plot[Evaluate[Table[pfun[1, b][t], {b, 4}]], {t, 0, 10}, PlotLegends -> Automatic]


This approach also has the advantage of allowing you to use parameter values which are not integer index values, which I think will be something you will want to do.

Plot[Evaluate[Table[pfun[.12, b][t], {b, 4}]], {t, 0, 10}, PlotLegends -> Automatic]



Not the answer you're looking for? Browse other questions tagged or ask your own question.