# How do I plot x[t] vs. x'[t] (where x[t] and x'[t] are solutions to NDSolve)?

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!

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

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]


• Reminds me of Strogatz. Did you ever use that book? Commented Nov 2, 2012 at 18:06
• @rcollyer no: is it any good? Commented Nov 2, 2012 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. Commented Nov 2, 2012 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.) Commented Nov 2, 2012 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"}}] Commented Nov 2, 2012 at 19:18

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}]


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}
]


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}]

• 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. Commented Sep 13, 2014 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). Commented Sep 13, 2014 at 10:56

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!

• 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? Commented Mar 26, 2013 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! Commented Mar 27, 2013 at 14:31