I defined a system of ODEs inside a Manipulate
environment. I would like to draw multiple plots of the same NDSolve
solution, i.e. I want to plot each variable separately. My code is pretty straightforward (see a little working example below), but I have performance issues.
My guess is that NDSolve
is evaluated every time is called inside a Plot
. Is there a way to avoid that? Am I doing something wrong (I am an absolute beginner with Mathematica)?
Here is a minimal working example:
Manipulate[
Module[{sol, t, a, b, kb = 1.0},
sol = NDSolve[
{a'[t] == kb*b[t],
b'[t] == -ka*b[t],
a[0] == 0.1, b[0] == 0.1},
{a, b}, {t, 0.0, 5.0}];
GraphicsRow[{
Plot[a[t] /. sol, {t, 0.0, 5.0}],
Plot[b[t] /. sol, {t, 0.0, 5.0}]}
]
],
{{ka, 1.0, "ka"}, 1.0, 100.0}
]
=
and not:=
NDSolve
shouldn't be recomputing when you plot. There might be a trick or two you could do to make this faster (e.g. prerender the plots for specific values ofka
then just use manipulate to display parts:plts=Table[Module[{...},sol=NDSolve[...];GraphicsRow[...]],{ka,1.0,100.,10.}]; Manipulate[plts[[i]],{i,1,Length@plts,1}]
) but this runs just as fast as I can move the slider on my machine. $\endgroup$