Skip to content

Instantly share code, notes, and snippets.

@astellon
Created January 18, 2019 07:28
Show Gist options
  • Save astellon/9da2dd08cf752b8835fe6717d0365114 to your computer and use it in GitHub Desktop.
Save astellon/9da2dd08cf752b8835fe6717d0365114 to your computer and use it in GitHub Desktop.
using ODE, Plots
function lorenz(t, r)
# Extract the coordinates from the r vector
(x, y, z) = r
# The Lorenz equations
dx_dt = σ*(y - x)
dy_dt = x*(ρ - z) - y
dz_dt = x*y - β*z
# Return the derivatives as a vector
[dx_dt; dy_dt; dz_dt]
end
const σ = 10.0
const ρ = 28.0
const β = 8.0/3.0
(t, pos) = ode45(lorenz, [0.1; 0.0; 0.0], 0:0.001:50)
x = map(v->v[1], pos)
y = map(v->v[2], pos)
z = map(v->v[3], pos)
plot3d(x, y, z)
ENV["PLOTS_TEST"] = "true" # using test mode
jump = 10
anim = @animate for i in 1:cld(length(pos), jump)
i = 1 + (i-1)*jump
plot3d(x[1:i], y[1:i], z[1:i], xlim=(-25,25), ylim=(-25,25), zlim=(0,50),
    title = "Lorenz Attractor", label="")
end
gif(anim, "lorenz.gif", fps = 15)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment