3

I am trying to plot the vector field of x'=x-x^3 On the real line, and cannot seem to tweak the two dimensional examples I have found.

Bonus points if I can also plot the graph of f(x)=x-x^3 and then input the direction vectors along the x axis underneath the graph, but not necessary.

edit: Here is what I have been trying, found here: Plot a vector field over a line

points = Table[{i, 1}, {i, 0, 1, .1}];
VectorPlot[{x - x^3, 0}, {x, -2, 2}, {y, -1, 1}, 



VectorPoints -> points, VectorScale -> {4, .1}, 

 Epilog -> Point[points]]

but it gives me a pretty lame plot with weird scaling. Changing the values of the parameters doesn't seem to do much, possibly because i copied this from a previous post and dont really know what each command is doing.

Modified Felix's answer to get the following:

f[x_] := x - x^3;

fp[x_] := Evaluate[D[f[x], x]];

arrowLength = 0.1;

xmin = -5;

xmax = 5;

Show[Plot[f[x], {x, xmin, xmax}], 

 Show[Table[



Graphics[Arrow[{{x, 0}, {x + Sign[f[x]], 0}}]], {x, xmin, 
xmax, (xmax - xmin)/10}]]]

Which is exactly what I was looking for! Thanks Felix

  • Does this work or you want the Phase Line StreamPlot[{1, x - x^3}, {y, -5, 5}, {x, -5, 5}]? – Moo Jan 15 '17 at 02:38
  • @Moo Hm im not sure what's going on there, it is also giving vectors in the wrong direction; they should be negative for large x. I am looking for something exactly like this: http://www.scielo.cl/fbpe/img/rchnat/v77n4/FIG2pag715.jpg – operatorerror Jan 15 '17 at 04:36

1 Answers1

4

Like this?

f[x_] := x - x^3;
fp[x_] := Evaluate[D[f[x], x]];
arrowLength = 0.1;
xmin = 0;
xmax = 1;
Show[Plot[f[x], {x, xmin, xmax}], 
 Show[Table[
   Graphics[
    Arrow[{{x, f[x]}, {x + arrowLength Cos[ArcTan[fp[x]]], 
       f[x] + arrowLength Sin[ArcTan[fp[x]]]}}]], {x, xmin, 
    xmax, (xmax - xmin)/10}]]]

enter image description here

The issue with your original approach is that you are not looking for a vector field. A vector field assigns a vector to every point in real space. However, you want to assign vectors only to those points where $y=f(x)$. This is done by plotting arrows, not vector fields.

The code above first produces a stack (list) of arrows at 10 points between xmin and xmax. The arrows start at the function graph points $(x,f(x))$ and point in the direction defined by the derivative. The length is set to 0.1. All arrows are then drawn in one graphics using Show. The outer-most Show then adds a plot of the actual function to that graphics.

Update:

This gives you the arrows only at the positions of the root, indicating if the function has a positive or negative derivative at the root as shown in your example.

f[x_] := x - x^3;
fp[x_] := Evaluate[D[f[x], x]];
roots = x /. Solve[f[x] == 0, x];
arrowLength = 0.1;
xmin = -0.2;
xmax = 1.2;
drawRootArrow[x_, sgn_, al_] := 
  Show[Graphics[
    Arrow[{{x - al (1 - sgn)/2, 0}, {x - al (1 + sgn)/2, 0}}]], 
   Graphics[
    Arrow[{{x + al (1 - sgn)/2, 0}, {x + al (1 + sgn)/2, 0}}]]];
Show[Plot[f[x], {x, xmin, xmax}], 
 Show[Table[drawRootArrow[x, Sign[fp[x]], arrowLength], {x, roots}]]]

enter image description here

Felix
  • 3,871
  • 13
  • 33