Plotting a direction field of a differential equation

Hi all,

I have read similar title on this discourse, none of them in my opinion help to plot a direction field.

The one above is really great, too bad the code does not work… and the link is unreachable again for Makie.

I want to plot the direction field / slope field for

\frac{dv}{dt} = 9.8 - \frac{v}{5}

the solution is at v=49, it is basically page 3 of the ODE book I read…

There is a code that can be used but I do not know how to change the equation in the code (put the ODE above into this code):

xs = -5:0.5:2
ys = -5:0.5:2

using LinearAlgebra, Plots

df(x, y) = normalize([-y, x]) ./ 10

xxs = [x for x in xs for y in ys]
yys = [y for x in xs for y in ys]

Plots.quiver(xxs, yys, quiver=df)

I use the code here and it is working, but still want to ask people is my code correct

for this very simple ODE:

\frac{dv}{dt} = 9.8 - \frac{v}{5}

xs = 0:5:50
ys = -10:5:80

using LinearAlgebra, Plots

# dx/dy = f(y,x)
df(x, y) = normalize([9.8-(y/5), 9.8-(y/5)])  

xxs = [x for x in xs for y in ys]
yys = [y for x in xs for y in ys]

Plots.quiver(xxs, yys, quiver=df)
plot!([49], seriestype="hline", linestyle=:dash, color=:green, label="y(t)", legend=:outerright)

I am always ready for any input / help to make the code above better / more efficient…
(e.g. instead of putting 49, I could put a variable, thus what is the source/reference/package to get the simple solution for simple ODE with 1 variable?)

Another words, the arrow looks very sad too, big head and almost no body.

Hi,
sorry for the inconvenience. But, the gallery used to be in personal account, and now is part of the MakieOrg. Here are the examples you are looking for:

using CairoMakie, Meshes

odeSol(x, y) = Point2f(-x, 2y) # x'(t) = -x, y'(t) = 2y
fig = Figure(resolution = (600, 400))
ax = Axis(fig[1, 1], xlabel = "x", ylabel = "y", backgroundcolor = :black)
streamplot!(ax, odeSol, -2 .. 4, -2 .. 2, colormap = Reverse(:plasma),
    gridsize = (32, 32), arrow_size = 10)
fig

Function passed to streamplot must return Point2 or Point3

Stacktrace:
** [1] error(s::String)**
** @ Base ./error.jl:33**
** [2] streamplot_impl(CallType::Type, f::typeof(odeSol), limits::GeometryBasics.HyperRectangle{2, Int64}, resolutionND::Tuple{Int64, Int64}, stepsize::Float64, maxsteps::Int64, dens::Float64)**
** @ Makie ~/.julia/packages/Makie/c5WJV/src/basic_recipes/streamplot.jl:112**
** [3] (::Makie.var"#611#616")(f::Function, limits::GeometryBasics.HyperRectangle{2, Int64}, resolution::Tuple{Int64, Int64}, stepsize::Float64, maxsteps::Int64, density::Float64)**
** @ Makie ~/.julia/packages/Makie/c5WJV/src/basic_recipes/streamplot.jl:167**
** [4] lift(::Function, ::Observable{typeof(odeSol)}, ::Observable{GeometryBasics.HyperRectangle{2, Int64}}, ::Vararg{Any}; kw::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})**
** @ Makie ~/.julia/packages/Makie/c5WJV/src/interaction/nodes.jl:13**
** [5] lift(::Function, ::Observable{typeof(odeSol)}, ::Observable{GeometryBasics.HyperRectangle{2, Int64}}, ::Observable{Any}, ::Vararg{Observable{Any}})**
** @ Makie ~/.julia/packages/Makie/c5WJV/src/interaction/nodes.jl:10**
** [6] plot!(p::StreamPlot{Tuple{typeof(odeSol), GeometryBasics.HyperRectangle{2, Int64}}})**
** @ Makie ~/.julia/packages/Makie/c5WJV/src/basic_recipes/streamplot.jl:161**
** [7] plot!(scene::Scene, P::Type{StreamPlot{Tuple{typeof(odeSol), IntervalSets.ClosedInterval{Int64}, IntervalSets.ClosedInterval{Int64}}}}, attributes::Attributes, input::Tuple{Observable{typeof(odeSol)}, Observable{IntervalSets.ClosedInterval{Int64}}, Observable{IntervalSets.ClosedInterval{Int64}}}, args::Observable{Tuple{typeof(odeSol), GeometryBasics.HyperRectangle{2, Int64}}})**
** @ Makie ~/.julia/packages/Makie/c5WJV/src/interfaces.jl:787**
** [8] plot!(::Scene, ::Type{StreamPlot}, ::Attributes, ::Function, ::Vararg{Any}; kw_attributes::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})**
** @ Makie ~/.julia/packages/Makie/c5WJV/src/interfaces.jl:698**
** [9] plot!(::Scene, ::Type{StreamPlot}, ::Attributes, ::Function, ::IntervalSets.ClosedInterval{Int64}, ::IntervalSets.ClosedInterval{Int64})**
** @ Makie ~/.julia/packages/Makie/c5WJV/src/interfaces.jl:667**
** [10] plot!(::Axis, ::Type{StreamPlot}, ::Attributes, ::Function, ::Vararg{Any}; kw_attributes::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})**
** @ Makie.MakieLayout ~/.julia/packages/Makie/c5WJV/src/makielayout/layoutables/axis.jl:643**
** [11] plot!(::Axis, ::Type{StreamPlot}, ::Attributes, ::Function, ::IntervalSets.ClosedInterval{Int64}, ::IntervalSets.ClosedInterval{Int64})**
** @ Makie.MakieLayout ~/.julia/packages/Makie/c5WJV/src/makielayout/layoutables/axis.jl:638**
** [12] plot!(::Type{StreamPlot}, ::Axis, ::Function, ::Vararg{Any}; kw_attributes::Base.Pairs{Symbol, Any, Tuple{Symbol, Symbol, Symbol}, NamedTuple{(:colormap, :gridsize, :arrow_size), Tuple{Reverse{Symbol}, Tuple{Int64, Int64}, Int64}}})**
** @ Makie.MakieLayout ~/.julia/packages/Makie/c5WJV/src/makielayout/layoutables/axis.jl:655**
** [13] streamplot!(::Axis, ::Vararg{Any}; attributes::Base.Pairs{Symbol, Any, Tuple{Symbol, Symbol, Symbol}, NamedTuple{(:colormap, :gridsize, :arrow_size), Tuple{Reverse{Symbol}, Tuple{Int64, Int64}, Int64}}})**
** @ Makie ~/.julia/packages/Makie/c5WJV/src/recipes.jl:19**
** [14] top-level scope**
** @ In[39]:6**
** [15] eval**
** @ ./boot.jl:373 [inlined]**
** [16] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)**
** @ Base ./loading.jl:1196**

I just tested it again, and it works. Are you up to date with your Makie versions?

  [13f3f980] CairoMakie v0.8.13
  [5c1252a2] GeometryBasics v0.4.4
  [ee78f7c6] Makie v0.17.13
  [eacbb407] Meshes v0.25.1
  [91a5bcdd] Plots v1.33.0

I use JupyterLab / IJulia