Message Boards Message Boards

How do I use NDSolve in Manipulate?

Posted 8 years ago

I'm trying to use NDSolve with Manipulate but have difficulty doing so. I think the difficulty is because when using Manipulate, non-numerical boundary conditions are called in NDSolve, which I'm then trying to assign numerical value to in Manipulate. I tried using a delay tag with NDSolve, but that did not seem to solve the issue. Does anyone know a way to use symbolic boundary conditions in NDSolve, using a delay tag, which will work when called in Manipulate?

The diff. eq. works with numerical boundary conditions:

Eqn1 = D[f1[t], t, t] + f1[t] == (1 + f1[t]/100)^-1;
IC11 = f1[0] == 1;
IC12 = (D[f1[t], {t, 1}] == 0) /. {  t -> 0  };

Sol1 := Simplify[
  NDSolve[
   {Eqn1, IC11, IC12},
   f1[t],
   {t, 0, 100}]
  ]

x1[t_] = Simplify[
    Replace[
     f1[t],
     Sol1
     ]
    ][[1]];

Plot[
 x1[t],
 {t, 0, 100}
 ]

However, this method does not work:

Eqn2 = D[f2[t], t, t] + f2[t] == (1 + f2[t]/100)^-1;
IC21 = f2[0] == \[Alpha];
IC22 = (D[f2[t], {t, 1}] == \[Beta]) /. {  t -> 0  };

Sol2 := Simplify[
  NDSolve[
   {Eqn2, IC21, IC22},
   f2[t],
   {t, 0, 100}]
  ]

x2[\[Alpha]_, \[Beta]_, t_] := Replace[
   f2[t],
   Sol2
   ][[1]]

Manipulate[
 Plot[
  {
   x2[\[Alpha], \[Beta], t]
   },
  {t, tmin, tmax},
 PlotRange -> {  {tmin, tmax}, Automatic  },
 {{\[Alpha], 1, ""}, -0.5, 1, .001, ImageSize -> Medium, Appearance -> "Labeled"},
 {{\[Beta], 0, ""}, -0.5, 1, .001, ImageSize -> Medium, Appearance -> "Labeled"},
 {{tmin, 0, ""}, 0, 0, .001, ImageSize -> Medium, Appearance -> "Labeled"},
 {{tmax, 100, ""}, 10, 1000, .001, ImageSize -> Medium, Appearance -> "Labeled"},
 TrackedSymbols :> {\[Alpha], \[Beta], tmin, tmax},
 ]

Any thoughts?

POSTED BY: Tim Kirkpatrick
2 Replies

I went through the script you provided; thank you! When solving numerically, I did need to assign alpha and beta to be variable for IC11 and IC12. That is what was causing the program to not operate properly in Manipulate. Thanks again!

POSTED BY: Tim Kirkpatrick

Your variables IC21, IC22, Sol2 depend on alpha and beta too. This should be made explicit if you plan to change alpha and beta in real time. Morover, there seems to be a conflict with the variable t in Plot and the variable t in NDSolve. An Evaluate inside Plot solves the problem:

Clear[IC21, IC22, x2, Sol2, t];
Eqn2 = D[f2[t], t, t] + f2[t] == (1 + f2[t]/100)^-1; 
IC21[\[Alpha]_, \[Beta]_] = f2[0] == \[Alpha]; 
IC22[\[Alpha]_, \[Beta]_] = (D[f2[t], {t, 1}] == \[Beta]) /. {t -> 
    0};
Sol2[\[Alpha]_, \[Beta]_] := 
 NDSolveValue[{Eqn2, IC21[\[Alpha], \[Beta]], 
   IC22[\[Alpha], \[Beta]]}, f2, {t, 0, 100}]
Manipulate[
 Plot[Evaluate[Sol2[\[Alpha], \[Beta]][t]], {t, 0, 1}], {{\[Alpha], 1,
    ""}, -0.5, 1, .001}, {{\[Beta], 0, ""}, -0.5, 
  1, .001}, {{tmin, 0, ""}, 0, 1, .001}, {{tmax, 100, ""}, 10, 
  1000, .001}]
POSTED BY: Gianluca Gorni
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract