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

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