1
$\begingroup$

I am trying to manipulate two parameters from DSolve but something is happening that I don't understand. I type my first code, whith which I am trying to manipulate the initial conditions of an ODE.

Clear["Global`*"]

s[c_] = DSolve[{y'[t] == 2*t*(y[t])^2, y[0] == c}, y[t], t]
Manipulate[Plot[y[t] /. s[c], {t, -2, 2}], {c, -1, 1, 0.1}]

After that,in another cell I type the following code, so I can manipulate the coefficient of another ODE.

Clear["Global`*"]

s[a_] = DSolve[{y'[t] == a*y[t], y[0] == 1}, y[t], t]
Manipulate[Plot[y[t] /. s[a], {t, -2, 2}], {a, -1, 1, 0.1}]

However, something weird happens, when I run the first code, it all works fine, when I run the second code, although it gives me the desired output, it also changes the output of the first code (making it identical to the output of the second code). If I go back to the first code and run it again, it changes the output of the second code now (again making it identical to the output of the first code).

Why is this happening and how can I fix it?

$\endgroup$
1
  • 2
    $\begingroup$ Use different symbols for the two cases, like s1[c_] and s2[a_]. Otherwise, both Manipulate point to the same symbol s. Note that the names of patterns (a_ or c_ in your case) aren't making the two s different! $\endgroup$
    – Domen
    Jan 30, 2022 at 16:55

2 Answers 2

1
$\begingroup$

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]

Mathematica graphics

$\endgroup$
2
$\begingroup$

To avoid this, just add Tracking to each Manipulate. So it knows what to track. This is something that should always be done. Now the second Manipulate does not affect the first

Clear["Global`*"]
s[c_] = DSolve[{y'[t] == 2*t*(y[t])^2, y[0] == c}, y[t], t]
Manipulate[Plot[y[t] /. s[c], {t, -2, 2}], {c, -1, 1, 0.1}, 
 TrackedSymbols :> {c}]

And

Clear["Global`*"]

s[a_] = DSolve[{y'[t] == a*y[t], y[0] == 1}, y[t], t]
Manipulate[Plot[y[t] /. s[a], {t, -2, 2}], {a, -1, 1, 0.1}, 
 TrackedSymbols :> {a}]

Reply to comment

The original problem was

when I run the second code, although it gives me the desired output, it also changes the output of the first code (making it identical to

This no longer happens. When starting second the Manipulate or while running it, the other one do not change.

But when switching from one to the other, the other will start from where the other stopped since that is the global solution now.

But there is no interaction when running one.

Here is a movie. V 13

enter image description here

the output of the second code).

$\endgroup$
3
  • $\begingroup$ What happens when you move the sliders on both your manipulates after evaluating both of them? I think I must get something different than you, because I still see the OP's problem. $\endgroup$
    – Michael E2
    Jan 31, 2022 at 1:31
  • $\begingroup$ @MichaelE2 answered comment in post. Ofcourse the best solution is not to share global solution and to put everything inside Manipulate, but that is not the setup the OP had. $\endgroup$
    – Nasser
    Jan 31, 2022 at 1:47
  • $\begingroup$ Thanks, that's clearer now. I misunderstood what your code was supposed to do. $\endgroup$
    – Michael E2
    Jan 31, 2022 at 1:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.