I have to sketch the direction field for the following differential equation:

$$\frac{dy}{dx}=\frac{-0.02 y +0.00002 xy}{0.08 x-0.001xy}$$

This is the code I used, which gives an incorrect plot:

StreamPlot[Normalize[{1, (y (-0.016 + 0.00008 x))/(x (0.12 - 0.006 y))}], 
    {x, -200, 200}, {y, -200, 200}, Axes -> True]

The following picture shows what I need to get:

enter image description here

  • $\begingroup$ I think this is duplicate mathematica.stackexchange.com/questions/8841/… "How can I plot the direction field for a differential equation?" $\endgroup$
    – Nasser
    Commented Sep 19, 2013 at 0:20
  • $\begingroup$ @Nasser The user has already tried StreamPlot as shown there and says the result is incorrect, so I don't think it is a duplicate outright. Thanks for looking for duplicates however! $\endgroup$
    – Mr.Wizard
    Commented Sep 19, 2013 at 0:22
  • $\begingroup$ data = Table[{1, dyx}, {x, 1, 3000, 10}, {y, 1, 150}]; ListStreamPlot[data] comes close. I'm not saying it's an answer, just 'closer'. $\endgroup$
    – user9444
    Commented Sep 19, 2013 at 3:31

3 Answers 3


I believe this is getting close to what you want:

f[x_, y_] := (-0.02 y + 0.00002 x y)/(0.08 x - 0.001 x y)

VectorPlot[{1, f[x, y]}, {x, 0, 3100}, {y, 0, 150}, VectorStyle -> Arrowheads[{}]]

enter image description here

  • $\begingroup$ VectorPlot is addressed in the post that Nasser linked to... besides, OP's main fault is plotting a different equation than the one that they actually want! $\endgroup$
    – rm -rf
    Commented Sep 19, 2013 at 0:57
  • $\begingroup$ @rm-rf Oh, well I guess we can delete this in a little while then. $\endgroup$
    – Mr.Wizard
    Commented Sep 19, 2013 at 0:58
  • $\begingroup$ VectorPlot[{5 , f[x, y]}, {x, 0, 3100}, {y, 0.1, 150}, PlotRange -> {{0, 3100}, {0, 150}}, VectorScale -> .03] is a little more aligned with the OP's plot $\endgroup$ Commented Sep 19, 2013 at 1:04
  • $\begingroup$ By the way, how to get arrows with the equal lengths? Because of the large aspect ratio (3100/150), they always have different lengths... $\endgroup$
    – ybeltukov
    Commented Sep 19, 2013 at 1:05
  • $\begingroup$ @ybeltukov I'll admit I tried for a few minutes to get that (I was using VectorScale) but I failed. I intend to return to it later. Let me know if you figure it out before I do! $\endgroup$
    – Mr.Wizard
    Commented Sep 19, 2013 at 1:11

I think the following is a bit closer

f[x_, y_] := (-0.02 y + 0.00002 x y)/(0.08 x - 0.001 x y)
{X1, X2} = {0, 3100};
{Y1, Y2} = {0, 150};
AR = 0.5;
length = 0.04;
VectorPlot[{1, f[x, y]}, {x, X1, X2}, {y, Y1, Y2}, AspectRatio -> AR, 
 PlotRange -> {{X1, X2}, {Y1, Y2}}, VectorStyle -> Arrowheads[{}], 
 VectorScale -> {length, Automatic, If[#5 > 0, #5/Sqrt[#3^2 + (AR(X1-X2)#4/(Y1-Y2))^2],0] &}]

enter image description here

This solution gives the equal lengths of the arrows with taking into account the aspect ratio.

  • $\begingroup$ The lines above/below (250, 80) look shorter to me. I think you want #5/Sqrt[..] inside the If in VectorScale. See mathematica.stackexchange.com/questions/103179/… $\endgroup$
    – Michael E2
    Commented Jan 1, 2016 at 23:22
  • $\begingroup$ @MichaelE2 You are right, thanks. $\endgroup$
    – ybeltukov
    Commented Jan 2, 2016 at 13:47

I'll assume that what you really want is a StreamPlot and not a vector plot, because that's in your code.

The equation you're plotting in the question isn't the one in the first equation. But even if we correct this, the StreamPlot looks bad because it cuts off the automatically generated streamlines before they are long enough to outline the shape of the slope field.

To remedy this, you can try specifying a minimum length for the streamlines, and also choose them to go through the interesting points in the plot. I've taken your (corrected) StreamPlot and added the necessary StreamPoints option:

 Normalize[{1, (-0.02 y + 0.00002 x y)/(0.08 x - 
      0.001 x y)}], {x, 0, 3000}, {y, 0, 150}, Axes -> True, 
 StreamPoints -> {Table[{1040, i}, {i, 13, 150, 5}], Automatic, 3000},
  PlotRange -> All]



Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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