I am having a bit of trouble when it comes to plotting the solution of a system of non-linear ODE's.

I write

pfun = ParametricNDSolveValue[
  system, {x[t], y[t], z[t], px[t], py[t], pz[t]}, {t, 0, 1}, {tau, 
   omega, xi, yi, zi, pxi, pyi, pzi}]
  • xi, yi, zi, pxi, pyi, pzi are the initial conditions

  • tau and omega are just two other parameters

This gives me a single parametric function as the output.

I then write

 Plot[pfun[tau0, omega0, xi0, yi0, zi0, pxi0, pyi0, pzi0], {t, 0, 
   1}], {{tau0, 0.5, "strength"}, 0, 1}, {{omega0, 2 Pi}, 0, 
  2 Pi}, {{xi0, 1, "initial theta"}, 0, Pi}, {{yi0, 1, "initial phi"},
   0, 2 Pi}, {{zi0, 1 , "initial phase"}, 0, 2 Pi}, {{pxi0, 1}, -Pi , 
  Pi}, {{pyi0, 1}, -Pi, Pi}, {{pzi0, 1}, -Pi, Pi}]

Which does correctly plot a bunch of solutions, with parameters that I can vary, however, I cannot then make the different plots show up in different colours! enter image description here

Could anyone explain what I need to do?

I have tried to use things like "Plot Style" in the third argument of the "Plot" function, but this does not work.


I will copy and paste a full simple example that causes me the same issue as described above.

$Assumptions = {x, y, tau, xi, yi} ∈ Reals

system = {D[x[t], t] == (Cos[x[t]] y[t])/tau, 
  D[y[t], t] == (Sin[y[t]] x[t])/tau, x[0] == xi, y[0] == yi}

pfun = ParametricNDSolveValue[
  system, {x[t], y[t]}, {t, 0, 1}, {tau, xi, yi}]

 Plot[pfun[tau0, xi0, yi0], {t, 0, 1}], {{tau0, 0.5, "strength"}, 0, 
  1}, {{xi0, 1, "initial theta"}, 0, 
  Pi}, {{yi0, 1, "initial phi"}, 0, 2 Pi}]
  • 1
    $\begingroup$ Can you make a simpler case where you could provide a complete example? Hard to duplicate your problem otherwise. $\endgroup$ – MikeY Mar 15 '20 at 15:36
  • $\begingroup$ Yes; I will include a simpler case by editing the question. $\endgroup$ – Blueberry Mar 15 '20 at 15:49

Try using the Evaluate command before pfun

Manipulate[Plot[Evaluate@pfun[tau0, xi0, yi0], {t, 0, 1}], 
           {{tau0, 0.5,  "strength"}, 0, 1}, 
           {{xi0, 1, "initial theta"}, 0, Pi}, 
           {{yi0, 1, "initial phi"}, 0, 2 Pi}]

enter image description here

  • $\begingroup$ Thank you. This worked for the main problem also! Could you please explain why we should use the Evaluate command here? What is Mathematica thinking when we include it vs when we do not. Apologies; I am very new to this. $\endgroup$ – Blueberry Mar 15 '20 at 16:18
  • 1
    $\begingroup$ It is quirky. With the Evaluate command, the values of (for example) pfun[1,1,1] are determined immediately and you get your two functions to plot, then t is set. Otherwise a Plot command holds the function to be plotted in an unevaluated form until the independent variable is set, so t is set first then the pfun[1,1,1] is evaluated. That is enough to puzzle the line coloring algorithm. $\endgroup$ – MikeY Mar 15 '20 at 16:58

This works:

    Manipulate[Plot[{pfun[tau0, xi0, yi0][[1]], pfun[tau0, xi0, yi0] 
              [[2]]}, {t, 0,  1}, PlotStyle -> {Red, Blue}, 
              ImageSize -> Medium], {{tau0, 0.5, "strength"}, 0, 
              1}, {{xi0, 1, "initial theta"}, 0, Pi}, {{yi0, 1, "initialphi"}, 0, 2 Pi}]
  • 1
    $\begingroup$ Thanks. This answer seems the simplest to understand: We use the first set of square brackets on pfun to turn the single parametric function into a list of interpolating functions, then use the second double square brackets to pick out each one of the interpolating functions from the list?! $\endgroup$ – Blueberry Mar 15 '20 at 16:26
  • $\begingroup$ @monday I think it is more-so just the use of the double-bracketed version of Part that helps in this case. The "first set of square brackets" is merely accessing the function using those arguments. Although with the way you defined pfun, it is easy to see where the confusion comes into being. $\endgroup$ – CA Trevillian Mar 15 '20 at 18:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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