1
$\begingroup$

I have a differential equation which I solved using NDSolve. I can easily plot x[t] vs. t, x'[t] vs. t, but....

how do I plot x[t] vs. x'[t]?

I tried using the Evaluate function to simplify things, but I still have no luck. Here's what I mean:

x1 = Evaluate[x'[t] /. sol];
x2 = Evaluate[x[t] /. sol];

Plot[x2, {x1, 0, 50}, PlotRange -> Automatic, AxesLabel -> {x[t], x'[t]}]

How can I plot these? This approach also did not work:

Plot[x[t]/.sol, {x'[t]/.sol, 0, 50}, PlotRange -> Automatic, 
AxesLabel -> {x[t], x'[t]}]

Help!

$\endgroup$
1
  • $\begingroup$ Evaluate only makes sense when used inside Plot to distinguish the plotted functions, not during assignment. $\endgroup$ Nov 2, 2012 at 17:40

4 Answers 4

10
$\begingroup$

or from a minor modification of the documentation

splot = StreamPlot[{y, -Sin[x]}, {x, -4, 4}, {y, -3, 3},StreamColorFunction -> "Rainbow"]; 
Manipulate[Show[splot,
ParametricPlot[
Evaluate[
First[{x[t], y[t]} /. 
  NDSolve[{x'[t] == y[t], y'[t] == -Sin[x[t]], 
    Thread[{x[0], y[0]} == point]}, {x, y}, {t, 0, T}]]], {t, 0, 
T}, PlotStyle -> Red]],
{{T, 20}, 1, 100}, {{point, {3, 0}}, Locator}, 
SaveDefinitions -> True]

Mathematica graphics

$\endgroup$
10
  • $\begingroup$ Reminds me of Strogatz. Did you ever use that book? $\endgroup$
    – rcollyer
    Nov 2, 2012 at 18:06
  • $\begingroup$ @rcollyer no: is it any good? $\endgroup$
    – chris
    Nov 2, 2012 at 18:08
  • $\begingroup$ I think it is an excellent primer for undergraduates, a bit thin for graduate level work. But, it provides a nice overview so can act as a jumping off point. $\endgroup$
    – rcollyer
    Nov 2, 2012 at 18:11
  • $\begingroup$ @rcollyer Thanks for the reference! I'll look into getting a copy. I had bifurcation theory as a grad level course but somehow nothing in that made sense to me (partly I suppose because of the terrible text book we had... cant remember the name now.) $\endgroup$
    – dearN
    Nov 2, 2012 at 19:12
  • $\begingroup$ @rcollyer,@ drN this one from the doc also is particularly neat Manipulate[Row[{Text["m"] == MatrixForm[m], StreamPlot[Evaluate[m . {x, y}], {x, -1, 1}, {y, -1, 1}, StreamScale -> Large, StreamColorFunction -> "Rainbow"]}], {{m, {{1, 0}, {0, 2}}}, {{{1, 0}, {0, 2}} -> "Nodal source", {{1, 1}, {0, 1}} -> "Degenerate source", {{0, 1}, {-1, 1}} -> "Spiral source", {{-1, 0}, {0, -2}} -> "Nodal sink", {{-1, 1}, {0, -1}} -> "Degenerate sink", {{0, 1}, {-1, -1}} -> "Spiral sink", {{0, 1}, {-1, 0}} -> "Center", {{1, 0}, {0, -2}} -> "Saddle"}}] $\endgroup$
    – chris
    Nov 2, 2012 at 19:18
7
$\begingroup$

For instance, solving this

sol = First@NDSolve[
   {x''[t] == Sin[x[t]],
    x[0] == 1, x'[0] \[Equal] 0},
   x,
   {t, 0, 10}]

and then

ParametricPlot[{x[t], x'[t]} /. sol, {t, 0, 10}]

Mathematica graphics

Of course you can elaborate this so as to set the initial condition by clicking:

Manipulate[
 sol = First@NDSolve[
    {x''[t] == Sin[x[t]],
     x[0] == p[[1]], x'[0] == p[[2]]},
    x,
    {t, 0, 10}];
 ParametricPlot[
  {x[t], x'[t]} /. sol, {t, 0, 10},
  AxesLabel -> {"x[t]", "x'[t]"},
  PlotRange -> {{0, 2*Pi}, {-2, 2}}],
 {{p, {2, 1}}, Locator}
 ]

Mathematica graphics

$\endgroup$
1
$\begingroup$

Use ParametricPlot:

NDSolve[{x''[t] + x[t] == 0, x[0] == 1, x'[0] == 0}, x, {t, 0, 2 Pi}]

{X[u_], XD[u_]} = {x[u], x'[u]} /. First[%]

ParametricPlot[{X[t], XD[t]}, {t, 0, 2 Pi}, AxesLabel -> {x[t], x'[t]}, PlotStyle -> {Blue, Thick}]
$\endgroup$
2
  • 1
    $\begingroup$ I just have edited (just) the formatting of your answer, because to me it looks like a very good solution to the question. One thing you might want to change is the naming of the variables which hold the solution, it usually is recommended to not use variable names which start with uppercase letters to stay out of conflicts with internal symbols. $\endgroup$ Sep 13, 2014 at 10:42
  • $\begingroup$ @Albert Retey Thanks,Albert. Shall avoid starting names with capital case.(Just put in capitals so the student would relate to his own old comfy nomenclatures). $\endgroup$
    – Narasimham
    Sep 13, 2014 at 10:56
0
$\begingroup$

All the above are right and helped me too, but I want to give you a much more brief and simple answer. Given a non linear equation in terms of x[t], you use:

solution=NDSolve[{equation,initial_cond},x[t],{t,0,10}]

to solve it. Then to plot x[t] vs x'[t] (phase space) you can use the following:

ParametricPlot[{x[t],x'[t]}/.solution,{t,0,10},AxesLabel->{x[t],x'[t]}]

That's it. I hope I helped you!

$\endgroup$
2
  • 2
    $\begingroup$ Welcome to our site! I was wondering what your answer adds to the one by acl which seems to give the identical solution. In what way(s), precisely, is yours simpler? $\endgroup$
    – whuber
    Mar 26, 2013 at 21:09
  • $\begingroup$ It is the same you are right, seems like I had problems with my browser. I had some problems reading the use of NDSolve above. Sorry! $\endgroup$
    – 2island
    Mar 27, 2013 at 14:31

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.