Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question
    
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}] –  Spawn1701D Apr 9 '13 at 6:04
    
The reason is just, you use p0 = 1 in unforced while actually you use p0 = 0 in the last two lines… –  xzczd Apr 9 '13 at 6:10
2  
@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! –  Spawn1701D Apr 9 '13 at 6:35
    
@Spawn1701D I laughed out loud when I saw your comment, you're exactly right! –  Mithuna Apr 9 '13 at 6:39
    
@Mithuna You had just hit the jack pot! :P –  Spawn1701D Apr 9 '13 at 6:43

2 Answers 2

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.

share|improve this answer

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}]
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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