2
$\begingroup$

I am having issues using Manipulate to plot the (numeric) solution to an ODE for different parameter values.

I have a code that has several stages, which seem to all work when I do them one after another. This code solves a system of ODEs (for particular parameter values), then does a parametric plot of the solution. The problem is I need to put the lines together to wrap them around with Manipulate (for I can do this code again easily for different parameter values), and this is causing me a lot of pain.

My initial code is:

(*this is my ODE*)

unforced[x0_, p0_, α_:α, δ_:δ] := 
{x'[t] == p[t], 
 p'[t] == -α x[t] - δ p[t] + α (x[t])^3, 
 x[0] == x0, 
 p[0] == p0
}

(*Choose some parameter values*)

α = -1, δ = 1

(*Solve my ODE*)

s = NDSolve[unforced[x0 = 1, p0 = 1], {x, p}, {t, 20}]

(*Plot It*)

ParametricPlot[Evaluate[{x[t], p[t]} /. s], {t, 0, 20}]

But now I want to be able to Manipulate the parameter values α and δ. So I start putting the lines of code together... and problems happen.

ParametricPlot[Evaluate[{x[t], p[t]}/.
               NDSolve[{x'[t] == p[t], 
                        p'[t] == -α x[t] - δ p[t] + α (x[t])^3, 
                        x[0] == 1, p[0] == 0}, {x, p}, {t, 20}]], {t, 0, 20}]

This plots an empty graph. This confuses me because it seems like all I did was substitute into my previous code. Because this doesn't work, I can't put a Manipulate around this. If it worked then I would have tried:

Manipulate[ParametricPlot[Evaluate[{x[t], p[t]} /.
                NDSolve[{x'[t] == p[t],   
                         p'[t] == -α x[t] - δ p[t] + α (x[t])^3, 
                         x[0] == 1, p[0] == 0}, {x, p}, {t, 20}]], {t, 0, 20}], 
           {{α, -1, "α"}, -2, 0}, {{δ, 0, "δ"}, 0, 2}]

How do I get around this problem?

$\endgroup$
6
  • $\begingroup$ Your commands looks sound, actually if you put the function you have defined (unforced) instead of the system both last commands work just fine! Manipulate[ ParametricPlot[ Evaluate[{x[t], p[t]} /. NDSolve[unforced[x0 = 1, p0 = 1, \[Alpha], \[Delta]], {x, p}, {t, 20}]], {t, 0, 20}], {{\[Alpha], -1, "\[Alpha]"}, -2, 0}, {{\[Delta], 0, "\[Delta]"}, 0, 2}] $\endgroup$
    – Spawn1701D
    Apr 9, 2013 at 6:04
  • $\begingroup$ The reason is just, you use p0 = 1 in unforced while actually you use p0 = 0 in the last two lines… $\endgroup$
    – xzczd
    Apr 9, 2013 at 6:10
  • 2
    $\begingroup$ @xzczd actually Mithuna's commands are not wrong the only issue is that this particular choice (x=p=0) happens to be one of the fixed points of the system and that results the plot to consists of just one point, the fixed point, and this gave the impression that was wrong! $\endgroup$
    – Spawn1701D
    Apr 9, 2013 at 6:35
  • $\begingroup$ @Spawn1701D I laughed out loud when I saw your comment, you're exactly right! $\endgroup$
    – Mithuna
    Apr 9, 2013 at 6:39
  • $\begingroup$ @Mithuna You had just hit the jack pot! :P $\endgroup$
    – Spawn1701D
    Apr 9, 2013 at 6:43

2 Answers 2

3
$\begingroup$

You can use ParametricNDSolve for this:

pf = ParametricNDSolveValue[{x'[t] == p[t], 
    p'[t] == -a x[t] - d p[t] + a (x[t])^3, x[0] == 1, 
    p[0] == p0}, {x[t], p[t]}, {t, 20}, {a, d, p0}];

Manipulate[
 ParametricPlot[pf[a, d, p0], {t, 0, 20}], {{a, -1, "Alpha"}, -2, 
  0}, {{d, 0, "Delta"}, 0, 2}, {{p0, .01, "p[0]"}, 0, 2}]

This allows you to pre-compute as much as is possible and then evaluate with specific parameters. Also, you could compute the sensitivity of the parameters. This is discussed in the documentation for ParametricNDSolve.

$\endgroup$
0
$\begingroup$

As mentioned by @spawn1701d, use a different initial condition.

This worked:

Manipulate[
 ParametricPlot[
  Evaluate[{x[t], p[t]} /. 
    NDSolve[{x'[t] == p[t], 
      p'[t] == -α x[t] - δ p[t] + α (x[t])^3, 
      x[0] == 1, p[0] == p0}, {x, p}, {t, 20}]], {t, 0, 20}],
 {{α, -1, "Alpha"}, -2, 0}, {{δ, 0, "Delta"}, 0, 
  2}, {{p0, .01, "p[0]"}, 0, 2}]
$\endgroup$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.