# Plotting the direction field of a differential equation

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: • I think this is duplicate mathematica.stackexchange.com/questions/8841/… "How can I plot the direction field for a differential equation?" – Nasser Sep 19 '13 at 0:20
• @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! – Mr.Wizard Sep 19 '13 at 0:22
• 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'. – user9444 Sep 19 '13 at 3:31

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[{}]] • 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! – rm -rf Sep 19 '13 at 0:57
• @rm-rf Oh, well I guess we can delete this in a little while then. – Mr.Wizard Sep 19 '13 at 0:58
• 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 – Dr. belisarius Sep 19 '13 at 1:04
• By the way, how to get arrows with the equal lengths? Because of the large aspect ratio (3100/150), they always have different lengths... – ybeltukov Sep 19 '13 at 1:05
• @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! – Mr.Wizard Sep 19 '13 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] &}] This solution gives the equal lengths of the arrows with taking into account the aspect ratio.

• 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/… – Michael E2 Jan 1 '16 at 23:22
• @MichaelE2 You are right, thanks. – ybeltukov Jan 2 '16 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:

StreamPlot[
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] 