3
$\begingroup$

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

$\endgroup$
  • $\begingroup$ Does this work or you want the Phase Line StreamPlot[{1, x - x^3}, {y, -5, 5}, {x, -5, 5}]? $\endgroup$ – Moo Jan 15 '17 at 2:38
  • $\begingroup$ @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: scielo.cl/fbpe/img/rchnat/v77n4/FIG2pag715.jpg $\endgroup$ – qbert Jan 15 '17 at 4:36
4
$\begingroup$

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

$\endgroup$
  • $\begingroup$ hmmm not quite, although I may be able to tweak it a little. I was hoping to have the arrows you have lying on the x axis, pointing forward whenever $f$ is positive above them, and backwards whenever $f$ is negative (which would be below them). $\endgroup$ – qbert Jan 15 '17 at 2:03
  • $\begingroup$ ah! I found a picture: scielo.cl/fbpe/img/rchnat/v77n4/FIG2pag715.jpg $\endgroup$ – qbert Jan 15 '17 at 2:03
  • $\begingroup$ So you want to draw the arrows only at the roots of f? $\endgroup$ – Felix Jan 15 '17 at 2:25
  • $\begingroup$ Yes I guess i only need vectors in between the roots of $f$ $\endgroup$ – qbert Jan 15 '17 at 4:32
  • $\begingroup$ I modified your code to get exactly what I wanted (put into question). Thanks! $\endgroup$ – qbert Jan 15 '17 at 4:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.