# How to Plot the Direction Field and Integral Curves Correctly with Plots

Hi all,

I have found a code in this discourse and modify it to solve the separable equation of:

\frac{dy}{dx} = \frac{x^{2}}{1 - y^{2}}

with separable equations, we obtain the equation for the integral curve is:

-x^{3} + 3y - y^{3} = c

I use this code then:

using Plots
gr()
#gr(size=(600,400))

function example()
X = range(-2, stop=2, length=50)
Y = range(-2, stop=2, length=50)
f(x, y) = -x^3 + 3y - y^3
contour(X, Y, f)

x = range(-2, stop=2, length=15)
y = range(-2, stop=2, length=15)
# Calculate the partial derivative for each variable manually:
df(x, y) = [-3x^2; 3 - 3y^2] / 25
quiver!(repeat(x,11), vec(repeat(y',11)), quiver=df, c=:blue)

xlims!(-2, 2)
ylims!(-2, 2)

# to save as png uncomment the code below and gr(size=(600,400))
#png("example")
end

example()


but, the solution does not look like the one in the book, even the quiver/arrow. Is the book plots it wrongly, since I am using Julia, and the author used Maple/Mathematica as plotting tool.

1 Like

Your plot seems to be correct to me. I’m not sure why you’re dividing by 25 in your gradient calculation (just to decrease the magnitude?).

Could you provide an image of the plot from the book to compare?

1 Like

this is the plot,

why I divide it by magnitude of 25? so the arrow can have proper size, otherwise it will look like messy threads… If you try it you will know why, when you do not divide by 25… it becomes full of mess.

I need advice and critics from others here, what did I do wrong, is my plot or code correct?

The image from the book is at my top post. OP.

Thanks all

1 Like

Ah okay, so it looks like you’re plotting the gradient field when you actually want the slope field. When you change it out the plots match:

using Plots
gr()
#gr(size=(600,400))

function example()
X = range(-2, stop=2, length=50)
Y = range(-2, stop=2, length=50)
f(x, y) = -x^3 + 3y - y^3
contour(X, Y, f)

x = range(-2, stop=2, length=15)
y = range(-2, stop=2, length=15)

dydx_norm(x, y) = [1; x^2/(1 - y^2)] / 25

quiver!(repeat(x,11), vec(repeat(y',11)), quiver=dydx_norm, c=:blue)

xlims!(-2, 2)
ylims!(-2, 2)

# to save as png uncomment the code below and gr(size=(600,400))
#png("example")
end

example()


The default plot options look kind of bad for this plot. I would actually recommend using Makie.jl for something that looks as good as the Maple/Mathematica version (also not need to manually normalize your plot):

using CairoMakie

fig = Figure()
ax = Axis(fig[1,1])

X = range(-2, stop=2, length=50)
Y = range(-2, stop=2, length=50)
f(x, y) = -x^3 + 3y - y^3

contour!(X, Y, f, color = :blue, linewidth=2)

dydx(x,y) = Point2f(1, x^2/(1 - y^2))

streamplot!(dydx, -2.0..2.0, -2.0..2.0, colormap=:blues)

fig


Hope that clears things up!

1 Like

It looks really beautiful, I do not even know I was plotting gradient field, I am learning the basic of Elementary Differential Equations and here I am…

But I want to ask how did one can obtain the function 1; x^{2} / (1 - y^{2}) from you line of code:

  dydx_norm(x, y) = [1; x^2/(1 - y^2)] / 25


and for the CairoMakie, why it won’t work? I tried @lazarusA codes too that use point2f, it does not work for me

UndefVarError: Point2f not defined

Do you use REPL, which Julia version you are using?

could you please try the example the example that you mention just with the dependencies listed at the bottom of the example? And then we could start populating your env with other packages?

I try this code with IJulia / Jupyter Notebook:

using CairoMakie

fig = Figure()
ax = Axis(fig[1,1])

X = range(-2, stop=2, length=50)
Y = range(-2, stop=2, length=50)
f(x, y) = -x^3 + 3y - y^3

contour!(X, Y, f, color = :blue, linewidth=2)

dydx(x,y) = Point2f(1, x^2/(1 - y^2))

streamplot!(dydx, -2.0..2.0, -2.0..2.0, colormap=:blues)

fig


then it becomes like this:

I already set the environment with only CairoMakie and Makie only, no other packages…

I use Julia 1.7.3…

Is there anything wrong with my Julia version or is it the notebook?

Why CairoMakie can’t be used in REPL?

I know the problem is the IJulia / Jupyter Notebook

not Julia version I am using.

I try again in Julia REPL (through terminal) with this code to save the image as svg and it worked:

using CairoMakie

fig = Figure()
ax = Axis(fig[1,1])

X = range(-2, stop=2, length=50)
Y = range(-2, stop=2, length=50)
f(x, y) = -x^3 + 3y - y^3

contour!(X, Y, f, color = :blue, linewidth=2)

dydx(x,y) = Point2f(1, x^2/(1 - y^2))

streamplot!(dydx, -2.0..2.0, -2.0..2.0, colormap=:blues)

fig

save("/home/browni/LasthrimProjection/JupyterLab/CairoMakie/example.svg",fig)


thanks!!!