1

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!

Anna
  • 11
  • 1
  • 2

4 Answers4

10

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

chris
  • 22,860
  • 5
  • 60
  • 149
  • Reminds me of Strogatz. Did you ever use that book? – rcollyer Nov 02 '12 at 18:06
  • @rcollyer no: is it any good? – chris Nov 02 '12 at 18:08
  • 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. – rcollyer Nov 02 '12 at 18:11
  • @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.) – dearN Nov 02 '12 at 19:12
  • @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"}}] – chris Nov 02 '12 at 19:18
  • @drN it provides a nice foundation, primarily in the language used, so you're welcome. – rcollyer Nov 02 '12 at 19:24
  • @rcollyer but you can't have chaos in continuous systems with two degrees of freedom :) – acl Nov 02 '12 at 21:09
  • @acl that is true, but the graphical techniques introduced in that book are very nice for non-chaotic systems, too. – rcollyer Nov 02 '12 at 21:11
  • @rcollyer no of course, it was a lame joke. I've never read the book although I know the author's name somewhere – acl Nov 02 '12 at 21:39
7

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

acl
  • 19,834
  • 3
  • 66
  • 91
1

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}]
Albert Retey
  • 23,585
  • 60
  • 104
Narasimham
  • 3,160
  • 13
  • 26
  • 1
    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. – Albert Retey Sep 13 '14 at 10:42
  • @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). – Narasimham Sep 13 '14 at 10:56
0

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!

Albert Retey
  • 23,585
  • 60
  • 104
2island
  • 85
  • 2
  • 9
  • 2
    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? – whuber Mar 26 '13 at 21:09
  • 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! – 2island Mar 27 '13 at 14:31