I want a slope field for a scalar differential equation. For simplicity I have chosen $dx/dt=rx$, where $r$ is a parameter. I used VectorPlot for the slope field as you can see from the code below. $|r|$ is bounded by 5. All is good when the PlotRange has the same horizontal and vertical ranges. Observe what happens when $a = 5$ and $b=1$. Unless the value of parameter $r$ is close to zero, the direction lines for a given $x$ value are so long that they spill over to the rows above and below.

Because this is a slope field and not a direction field for a 2D system, the length of the vectors should be constant. How can I achieve that? I have tried various rescaling and normalization but to no avail. Previous posts I've seen do not address this matter.


 VectorPlot[{1, r x}, {t, -a, a}, {x, -b, b}, VectorPoints -> 17, 
  VectorScale -> {0.03, Automatic, None}, 
  VectorStyle -> {{Red, Arrowheads[0]}}],
 {a, 1, 5},
 {b, 1, 5},
 {r, -5, 5}]
  • $\begingroup$ I am familiar with Marc McClure's post. He just addresses the removal of the arrowheads which I have done already. McClure's code uses a PlotRange whose horizontal and vertical ranges are identical. $\endgroup$
    – Stephen
    Nov 28 '14 at 22:55
  • $\begingroup$ Not only should the length of the vectors be constant, but there should be a way to define their length $\endgroup$
    – Stephen
    Nov 28 '14 at 23:01
  • $\begingroup$ OK, good point. $\endgroup$
    – C. E.
    Nov 28 '14 at 23:20
  • $\begingroup$ :-) There should be a way to specify the common length of all the vectors in the VectorPlot $\endgroup$
    – Stephen
    Nov 28 '14 at 23:42
  • $\begingroup$ What about varying the first argument of VectorScale? $\endgroup$
    – murray
    Dec 3 '14 at 21:34

If you take the problematic plot and normalize the arrows dimensions by brute force, the issue becomes clear!

plot = (* copy-paste Manipulate's graphic *)

Reap[plot /. Arrow[pts : {pt0 : {__}, {__}}] :> 
     Sow[Line[{pt0, pt0 + .2 Normalize@First@Differences@pts}]]];
Graphics@%[[2, 1]]

The vectors are correct, but then displayed in the plot they get distorted! It must be the plot's AspectRatio messing with their sizes!

And, indeed, if you add AspectRatio -> Automatic, ImageSize -> 300 to VectorPlot, you get nice, equal vectors!

But if you want the plot to be square, I see no easy way other than re-scaling your functions and maintaining the plot range in the {-1, 1} range. But I'd love to be wrong on that one. In your case:

              Evaluate[{1, r x} /. {x -> b*x, t -> a*t}]
              ,{t, -1, 1}, {x, -1, 1}
              ,VectorPoints -> 17, VectorScale -> {0.03, Automatic, None}, VectorStyle -> {{Red, Arrowheads[0]}}]
  , {a, 1, 5}, {b, 1,  5}, {r, -5, 5}]

  • $\begingroup$ Thanks for taking a stab at this problem, but your solution does not work for me. The slope lines get too crowded (and too long) as |r| is increased. I have been trying to develop a knock-off of John Polking's dfield (and pplane) which is implemented in Matlab and as *.jar files. $\endgroup$
    – Stephen
    Dec 4 '14 at 13:06
  • $\begingroup$ That's strange! I've just double-checked it, and the slope line's length is constant for any r, provided you use AspectRatio -> Automatic. V10 here, though. As for the crowding, you have to change VectorPoints to match the range's aspect ratio... But, until someone comes with a proper solution, you can scale the vector field itself and maintain the plot with identical ranges to achieve dfield's appearance! $\endgroup$
    – Aisamu
    Dec 4 '14 at 17:06
  • $\begingroup$ Please edit your code to show the manipulated version with controls for a, b, and r, $\endgroup$
    – Stephen
    Dec 4 '14 at 17:10
  • $\begingroup$ Your code does what I was looking for. Now I want to display the actual ticks values instead of the "nornalized" ones. Many thanks for your simple solution. $\endgroup$
    – Stephen
    Dec 5 '14 at 3:12

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.