How do I separate the solutions to these coupled DEs in the plot?

I want to plot the solutions to these equations at time t=7 as a function of parameter a while manipulating parameter b.


 sol = ParametricNDSolveValue[
   {s'[t] == -a*b*d[t], d'[t] == +b*s[t], s[0] == 100, d[0] == 0},
   {s[7], d[7]},
   {t, 0, 20},

   Plot[sol[a], {a, 0.1, 10}, ImageSize -> 400, 
    PlotStyle -> {Black, Blue}, PlotLabel -> populations]

 {{b, 1, "b"}, 0.1, 10, Appearance -> "Open"}

I tried




and various other lines but Mathematica is giving me errors like "is neither a list of replacement rules nor a valid dispatch table, and so cannot be used for replacing"


Two changes in your code: (1) made sol a two-parameter function and evaluate it outside Manipulate, (2) used {sol[a,b][[1]],sol[a,b][[2]]} as the first argument in Plot:

sol = ParametricNDSolveValue[{s'[t] == -a*c*d[t], d'[t] == c*s[t], 
    s[0] == 100, d[0] == 0}, {s[7], d[7]}, {t, 0, 20}, {a, c}];

Manipulate[Plot[{sol[a, b][[1]], sol[a, b][[2]]}, {a, 1/10, 10}, 
  PerformanceGoal -> "Speed", ImageSize -> 400, 
  PlotStyle -> {Black, Blue}], 
 {{b, 1, "b"}, .1, 10, Appearance -> "Open"}]

enter image description here

Note: If we use sol[a,b] or Evaluate@sol[a,b] instead of {sol[a, b][[1]], sol[a, b][[2]]} we get two curves with the same color.

  • $\begingroup$ Thanks so much! I have a lot to learn. PerformanceGoal is a nice touch. $\endgroup$ – Takeitfromme Aug 30 '18 at 8:11
  • $\begingroup$ @Takeitfromme, my pleasure. Welcome to mma.se. $\endgroup$ – kglr Aug 30 '18 at 19:20

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.