Cobweb plot



using Makie

f(x::Real, r::Real) = r * x * (1 - x)
function cobweb(
        xᵢ::Real,
        curve_f::Function,
        r::Real;
        nstep::Real = 30
    )::Vector{Point2f0} # col 1 is x, col 2 is y

    a = zeros(nstep*2, 2)
    a[1, 1] = xᵢ
    x = xᵢ
    y = curve_f(x, r)
    ret = similar(Vector{Point2f0}, nstep*2)

    for i  2:2:nstep*2-2
        a[i, 1] = x
        a[i, 2] = y
        x = y
        y = curve_f(x, r)
        a[i+1, 1] = x
        a[i+1, 2] = x
        ret[i] = Point2f0(a[i, 1], a[i, 2])
        ret[i+1] = Point2f0(a[i+1, 1], a[i+1, 2])
    end

    return ret

end

xᵢ = 0.1
rᵢ = 2.8
xr = 0:0.001:1
## setup sliders
sx, x = textslider(0:0.01:1, "xᵢ", start = xᵢ)
sr, r = textslider(0:0.01:4, "r", start = rᵢ)
## setup lifts
fs = lift(r -> f.(xr, r), r)
cw = lift((x, r) -> cobweb(x, f, r), x, r)
## setup plots
sc = lines(               # plot x=y, the bisector line
    xr,                   # xs
    xr,                   # ys
    linestyle = :dash,    # style of line
    linewidth = 3,        # width of line
    color = :blue         # colour of line
)

sc[Axis][:names][:axisnames] = ("x(t)", "x(t+1)") # set axis names

lines!(sc, xr, fs) # plot the curve

lines!(sc, cw) # plot the cobweb

final = hbox(sc, vbox(sx, sr))

record(final, "output.mp4", range(0.01, stop = 5, length = 100)) do i
    r[] = i
end