1
$\begingroup$

I have a system of three coupled differential equations that can be pretty readily solved by NDSolve:

sol = NDSolve[{x'[t] == (x[t]*1.99*(x[t] + y[t] + z[t]))/(1.99*x[t] +  2.13*y[t] + 2.34*z[t]) - x[t], 
y'[t] == (y[t]*2.13*(x[t] + y[t] + z[t]))/(1.99*x[t] +2.13*y[t] + 2.34*z[t]) - y[t], 
z'[t] == (z[t]*2.34*(x[t] + y[t] + z[t]))/(1.99*x[t] + 2.13*y[t] + 2.34*z[t]) - z[t], x[0] == 1900, y[0] == 90 , 
z[0] == 10}, {x, y, z}, {t, 0, 10}];

Plot[x[t] /. sol, {t, 0, 10}]

and this works fine. But I'd like to change the constant 1.99 to a variable "a" and see how this affects the solution. In principle, I should be able to do this in the manipulate environment, but I'm having issues getting it to work. Initially, I tried a simple approach:

 Manipulate[Plot[z[t] /. NDSolve[{x'[
   t] == (x[t]*a*(x[t] + y[t] + z[t]))/(a*x[t] + 2.13*y[t] + 
      2.34*z[t]) - x[t], 
 y'[t] == (y[t]*2.13*(x[t] + y[t] + z[t]))/(a*x[t] + 2.13*y[t] + 
      2.34*z[t]) - y[t], 
 z'[t] == (z[t]*2.34*(x[t] + y[t] + z[t]))/(a*x[t] + 2.13*y[t] + 
      2.34*z[t]) - z[t], x[0] == 1900, y[0] == 90 , 
 z[0] == 10}, {x, y, z}, {t, 0, 10}], {t, 0, 10}], {a, 0, 5}]

But this yields an error of the form that a constant value cannot be used as a variable. I tried using ParametricNDSolve and got the same error. I've seen a few threads on using NDSolve with manipulate but none of them is directly helping me here. Any ideas on where I'm going wrong, or how I might be able to rewrite this? And should NDSolve or Parametric NDSolve be employed?

$\endgroup$

2 Answers 2

3
$\begingroup$

I would recommend to code intermediate steps as separate functions, e.g.

solve[a_?NumericQ] := 
 z /. First@
   NDSolve[{x'[t] == (x[t]*a*(x[t] + y[t] + z[t]))/(a*x[t] + 2.13*y[t] + 2.34*z[t]) - x[t], 
 y'[t] == (y[t]*2.13*(x[t] + y[t] + z[t]))/(a*x[t] + 2.13*y[t] + 
      2.34*z[t]) - y[t], 
 z'[t] == (z[t]*2.34*(x[t] + y[t] + z[t]))/(a*x[t] + 2.13*y[t] + 
      2.34*z[t]) - z[t], x[0] == 1900, y[0] == 90, 
 z[0] == 10}, {x, y, z}, {t, 0, 10}]

then simply

Manipulate[
 With[{z = solve[a]},
  LogPlot[z[t], {t, 0, 10}, PlotRange -> 10^{-3, 3}]
], {a, 0, 5}]

gives you a desired answer.

$\endgroup$
1
  • $\begingroup$ That's a very cool approach, thanks for that! I found a workaround (posted below) but I think I'll try wrap my head around your idea for future! $\endgroup$
    – DRG
    Dec 19, 2015 at 18:35
2
$\begingroup$

I found a relatively easy solution to my problem, if anyone is interested! The error messages disappear when you use wrap the z[t]/.NDSolve[....] section with Evaluate, by

Manipulate[ Plot[Evaluate[ z[t] /. NDSolve[{x'[
    t] == (x[t]*a*(x[t] + y[t] + z[t]))/(a*x[t] + 2.13*y[t] + 
       2.34*z[t]) - x[t], 
  y'[t] == (y[t]*2.13*(x[t] + y[t] + z[t]))/(a*x[t] + 2.13*y[t] + 
       2.34*z[t]) - y[t], 
  z'[t] == (z[t]*2.34*(x[t] + y[t] + z[t]))/(a*x[t] + 2.13*y[t] + 
       2.34*z[t]) - z[t], x[0] == 1900, y[0] == 90, 
  z[0] == 10}, {x, y, z}, {t, 0, 10}]], {t, 0, 10}], {a, 0, 5}]

and this works perfectly. Hope that helps someone else!

$\endgroup$

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.