Here are two alternatives:
Localize solution symbol
In the OP's code, both Manipulate
demo use the same global, solution function symbol s
. (See Domen's comment.)
Manipulate[Plot[y[t] /. s[a], {t, -2, 2}],
{a, -1, 1, 0.1},
{{s, s}, ControlType -> None},
Initialization :> (
s[a_] = DSolve[{y'[t] == a*y[t], y[0] == 1}, y[t], t])]
Manipulate[Plot[y[t] /. s[c], {t, -2, 2}],
{c, -1, 1, 0.1},
{{s, s}, ControlType -> None},
Initialization :> (
s[c_] = DSolve[{y'[t] == 2*t*(y[t])^2, y[0] == c}, y[t], t])]
Inject solution
showSol // ClearAll;
showSol // Attributes = {HoldRest};
showSol[sol_, params___] :=
Manipulate[
Plot[y[t] /. sol, {t, -2, 2}],
params]
Examples:
showSol[DSolve[{y'[t] == a*y[t], y[0] == 1}, y, t], {a, -1, 1, 0.1}]
sol2 = DSolve[{y'[t] == 2*t*(y[t])^2, y[0] == c}, y[t], t];
showSol[sol2, {c, -1, 1, 0.1}]
Over generalization of showSol[]
User gets to specify variable t
, expression(s) to plot, plot domain & options; and choose an appropriate plotter,
basically because I had the plotter-parser hanging around from other DE work I've done. Goes a little beyond the OP's request, but what the heck.
I removed some code blocking the variables as Plot
would do, simply because the use-case here is a DSolve
solution. DSolve
does not protect the variables from external values, so they probably do not have any. ReImPlot
has a lameness -- call it a bug, if you like: Unlike Plot
, Evaluated -> True
has no effect in ReImPlot
. To get different colors for multiple functions, you have to use Evaluate
, yet another reason not to worry about protecting the variables from external values.
parsePlotters // ClearAll;
parsePlotters[sol_, expr_, {t_, a_, b_}] :=
DeleteDuplicates@Apply[Join]@Replace[
Dimensions[expr /. sol, AllowedHeads -> List], {
{
{___, 1} | {} -> {Plot, ReImPlot},
{___, 2} -> {ParametricPlot},
{___, 3} -> {ParametricPlot3D},
_ -> {Graphics[
Text[Grid[{
{"Do not know how to plot" },
{expr /. sol // HoldForm[#] & // StandardForm}}]]
] & -> "No plotter"}
}, {
{_} -> {Plot, ReImPlot},
_ -> Nothing
}
}];
plotSol // ClearAll;
plotSol[sol_, expr_, {t_, a_, b_}, params___,
Optional[popts : HoldPattern["PlotOptions" -> _], "PlotOptions" -> {}]] :=
With[{plotters = parsePlotters[sol, expr, {t, a, b}]},
Manipulate[
plotter[expr /. sol // Evaluate, {t, a, b},
"PlotOptions" /. popts // Evaluate],
params,
{{plotter,
Replace[plotters, {{p_ -> l_, ___} :> p, {p_, ___} :> p}]},
plotters, SetterBar}]
];
Examples:
plotSol[DSolve[{y'[t] == a*y[t], y[0] == 1}, y, t],
y[t], {t, -2, 2}, {a, -1, 1, 0.1}]
plotSol[
First@DSolve[{x'[t] == b y[t], y'[t] == a*x[t], x[0] == 1,
y[0] == 0}, {x[t], y[t]}, t],
{x[t], y[t]}, {t, 0, c},
{{a, 1}, -1, 1, 0.1}, {b, -1, 1, 0.1}, {{c, 2 Pi}, 1, 20},
"PlotOptions" -> AspectRatio -> 0.6]
s1[c_]
ands2[a_]
. Otherwise, bothManipulate
point to the same symbols
. Note that the names of patterns (a_
orc_
in your case) aren't making the twos
different! $\endgroup$