# 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. – István Zachar Nov 2 '12 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, y} == 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? – rcollyer Nov 2 '12 at 18:06
• @rcollyer no: is it any good? – chris Nov 2 '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 2 '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 2 '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 2 '12 at 19:18

For instance, solving this

sol = First@NDSolve[
{x''[t] == Sin[x[t]],
x == 1, x' \[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 == p[], x' == p[]},
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 == 1, x' == 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. – 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

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? – 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