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 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!

share|improve this question
    
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
3  
Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2)Read the FAQs! 3) When you see good Q&A, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. ALSO, remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign` –  chris Nov 2 '12 at 17:47

4 Answers 4

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!

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

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}]
share|improve this answer
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 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 at 10:56

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

share|improve this answer
    
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.) –  drN 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[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

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.