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.
StreamPlot[{x[0], x'[0]} /. 
  NDSolve[{x''[t] ==  1, x'[0] == v0, x[0] == x0}, {x, x'}, {t, 0, 1}],
 {x0, -2, 2}, {v0, -2, 2}]

The above code gives me the errors

NDSolve::ndinnt: Initial condition x0 is not a number or a rectangular array of numbers. >>

ReplaceAll::reps: {NDSolve[{(x^[Prime][Prime])[t]==1,(x^[Prime])[0]==v0,x[0]==x0},{x,x^[Prime]},{t,0,1}]} is neither a list of replacement rules nor a valid dispatch table, and so cannot be used for replacing. >>

NDSolve::ndinnt: Initial condition x0 is not a number or a rectangular array of numbers. >>

ReplaceAll::reps: {NDSolve[{(x^[Prime][Prime])[t]==1,(x^[Prime])[0]==v0,x[0]==x0},{x,x^[Prime]},{t,0,1}]} is neither a list of replacement rules nor a valid dispatch table, and so cannot be used for replacing. >>

While the code still produces the plot that I want, is it possible to get rid of the error messages?

share|improve this question

3 Answers 3

up vote 3 down vote accepted

Same idea a belisarius but moving the entire first argument into a separate function, while also localizing x:

fn[x0_?NumericQ, v0_?NumericQ] :=
 Module[{x},
  {x[0], x'[0]} /. NDSolve[{x''[t] == 1, x'[0] == v0, x[0] == x0}, {x, x'}, {t, 0, 1}]
 ]

StreamPlot[fn[x0, v0], {x0, -2, 2}, {v0, -2, 2}]

No errors.

share|improve this answer
f[v0_?NumericQ, x0_?NumericQ] := 
                 NDSolve[{x''[t] == 1, x'[0] == v0, x[0] == x0}, {x, x'}, {t, 0, 1}]
StreamPlot[{x[0], x'[0]} /. f[v0, x0], {x0, -2, 2}, {v0, -2, 2}]

Mathematica graphics

share|improve this answer
    
Got rid of the NDSolve errors but still gives me ReplaceAll::reps: {f[v0,x0]} is neither a list of replacement rules nor a valid dispatch table, and so cannot be used for replacing. >> ReplaceAll::reps: {f[v0,x0]} is neither a list of replacement rules nor a valid dispatch table, and so cannot be used for replacing. >> –  Felix Sep 21 '13 at 23:26

I think you use StreamPlot incorrectly. Let you have a differential equation

x''[t] == f[x[t],x'[t]]

In your particular case

f[x_,v_] := 1;

StreamPlot plots streams based on the vector field. In our case

StreamPlot[{dx,dv},{x,-2,2},{-2,2}]

Here dx and dv is the time derivatives of x and v respectively. By definition dx=v and dv=f[x,v]. Therefore, the command must be

StreamPlot[{v, f[x,v]}, {x, -2, 2}, {v, -2, 2}]

enter image description here

Another example: phase diagram of a pendulum

f[x_, v_] := -Sin[x];
StreamPlot[{v, f[x, v]}, {x, -5, 5}, {v, -3, 3}]

enter image description here

share|improve this answer
    
The point is I want to StreamPlot an arbitrary differential equation and not get the error messages above. –  Felix Sep 21 '13 at 23:30
    
@Felix If you have differential equation like x''[t]=f[x,x'] then you can use StreamPlot[{v,f[x,v]},...]. The first argument of StreamPlot is the vector field, not coordinates. –  ybeltukov Sep 21 '13 at 23:33
    
Regardless of how StreamPlot is normally used, I want to have it plot {x[t], x'[t]}, where x and x' are the solutions to some differential equation. –  Felix Sep 21 '13 at 23:38
    
@Felix I see your problem. I update my answer. I hope now it is more helpful. –  ybeltukov Sep 21 '13 at 23:50

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.