If I write this:
sol = NDSolveValue[{f'[t] == (0.4 - 0.01 s[t]) f[t],
s'[t] == (-0.3 + 0.005 f[t]) s[t], f[0] == 40, s[0] == 20}, {f[t],
s[t]}, {t, 0, 80}];
r[t_] = sol;
Then do this:
Manipulate[
Show[ParametricPlot[sol, {t, 0, 80},
PlotRange -> {{0, 140}, {0, 100}}],
Graphics[{Red, Arrow[{{0, 0}, r[tau]}],
Arrow[{r[tau], r[tau] + r'[tau]}]}]
],
{{tau, 0}, 0, 80}]
I get a nice Manipulate I can use for a classroom demonstration.
However, if I later use the sol variable to draw another image (a Plot, a ParametricPlot), the new sol value interferes with the Manipulate demonstration. For example, if the next cell in the notebook has:
sol = NDSolveValue[{x'[t] == y[t], y[t] == -x[t], x[0] == 1,
y[0] == -1}, {x[t], y[t]}, {t, 0, 5}]
Then this version of sol is now used in the Manipulate demonstration above. So, I tried the following:
Manipulate[Module[{sol, r},
sol = NDSolveValue[{f'[t] == (0.4 - 0.01 s[t]) f[t],
s'[t] == (-0.3 + 0.005 f[t]) s[t], f[0] == 40,
s[0] == 20}, {f[t], s[t]}, {t, 0, 80}];
r[t_] = sol;
Show[ParametricPlot[sol, {t, 0, 80},
PlotRange -> {{0, 140}, {0, 100}}],
Graphics[{Red, Arrow[{{0, 0}, r[tau]}],
Arrow[{r[tau], r[tau] + r'[tau]}]}]
]],
{{tau, 0}, 0, 80}]
But it doesn't work. I get an error that I just cannot understand.
Coordinate {InterpolatingFunction[{{0., 80.}}, {5, 7, 1, {353}, {4}, 0, 0, 0, 0, Automatic, {}, {}, False}, {{0., 0.0005236906903058783, 0.0010473813806117565, 0.002094762761223513, 0.0031421441418352696
, 0.004189525522447026, 0.014663339328564591`, 0.02513715313 should be a pair of numbers, or a Scaled or Offset form.
Any thoughts?
Sjoerd C. de Vries suggestion in comments:
Module[{sol, r},
Manipulate[
sol = NDSolveValue[{f'[t] == (0.4 - 0.01 s[t]) f[t],
s'[t] == (-0.3 + 0.005 f[t]) s[t], f[0] == 40,
s[0] == 20}, {f[t], s[t]}, {t, 0, 80}];
r[t_] = sol;
Show[ParametricPlot[sol, {t, 0, 80},
PlotRange -> {{0, 140}, {0, 100}}],
Graphics[{Red, Arrow[{{0, 0}, r[tau]}],
Arrow[{r[tau], r[tau] + r'[tau]}]}]], {{tau, 0}, 0, 80}]]
Still gives the error:
Coordinate {InterpolatingFunction[{{0., 80.}}, {5, 7, 1, {353}, {4}, 0, 0, 0, 0, Automatic, {}, {}, False}, {{0., 0.0005236906903058783, 0.0010473813806117565, 0.002094762761223513, 0.0031421441418352696
, 0.004189525522447026, 0.014663339328564591`, 0.02513715313 should be a pair of numbers, or a Scaled or Offset form.
Michael E2: If I run Michael E2 suggestion it works.
Clear[r];
sol = First@
NDSolve[{r'[
t] == ({0.4, -0.3} + {{0, -0.01}, {0.005, 0}}.r[t]) r[t],
r[0] == {40, 20}}, r, {t, 0, 80}];
With[{sol = sol},
Manipulate[
Show[ParametricPlot[r[t] /. sol, {t, 0, 80},
PlotRange -> {{0, 140}, {0, 100}}],
Graphics[{Red, Arrow[{{0, 0}, r[tau]}],
Arrow[{r[tau], r[tau] + r'[tau]}]} /. sol]], {{tau, 0}, 0, 80}]]
Then if sol gets changed in the global workspace, it doesn't affect Michaels first graph. Note there are very minor changes.
sol = First@
NDSolve[{r'[
t] == ({0.4, -0.3} + {{0, -0.01}, {0.002, 0}}.r[t]) r[t],
r[0] == {40, 20}}, r, {t, 0, 80}];
With[{sol = sol},
Manipulate[
Show[ParametricPlot[r[t] /. sol, {t, 0, 80},
PlotRange -> {{0, 140}, {0, 100}}],
Graphics[{Red, Arrow[{{0, 0}, r[tau]}],
Arrow[{r[tau], r[tau] + r'[tau]}]} /. sol]], {{tau, 0}, 0, 80}]]
Which gives this graph, but doesn't change the first graph.
However, if I now run m_goldberg's script, with slight numerical changes.
Manipulate[
Show[ParametricPlot[r[t], {t, 0, 80},
PlotRange -> {{0, 140}, {0, 100}}],
Graphics[{Red, Arrow[{{0, 0}, r[tau]}],
Arrow[{r[tau], r[tau] + r'[tau]}]}]], {{tau, 0}, 0, 80},
Initialization :> (r =.;
r[t_] = NDSolveValue[{f'[t] == (0.4 - 0.001 s[t]) f[t],
s'[t] == (-0.3 + 0.05 f[t]) s[t], f[0] == 40,
s[0] == 20}, {f[t], s[t]}, {t, 0, 80}])]
Then all three images look like this:
Moreover, if I go back to the first image and try to run the code again, I get this error:
ReplaceAll::reps: {False,True} is neither a list of replacement rules nor a valid dispatch table, and so cannot be used for replacing. >>
ReplaceAll::reps: {False,True} is neither a list of replacement rules nor a valid dispatch table, and so cannot be used for replacing. >>
ReplaceAll::reps: {False,True} is neither a list of replacement rules nor a valid dispatch table, and so cannot be used for replacing. >>
General::stop: Further output of ReplaceAll::reps will be suppressed during this calculation. >>
Which can be fixed if I Clear[r].
So, still having problems with more than one Manipulate in one notebook.