fromscipy.integrateimportodeintimportmatplotlib.pyplotaspltimportnumpyasnp# These are our constantsN=5# Number of variablesF=8# ForcingdefL96(x,t):"""Lorenz 96 model with constant forcing"""return(np.roll(x,-1)-np.roll(x,2))*np.roll(x,1)-x+Fx0=F*np.ones(N)# Initial state (equilibrium)x0[0]+=0.01# Add small perturbation to the first variablet=np.arange(0.0,30.0,0.01)x=odeint(L96,x0,t)# Plot the first three variablesfig=plt.figure()ax=fig.add_subplot(projection="3d")ax.plot(x[:,0],x[:,1],x[:,2])ax.set_xlabel("$x_1$")ax.set_ylabel("$x_2$")ax.set_zlabel("$x_3$")plt.show()
Julia simulation
usingDynamicalSystems,PyPlotPyPlot.using3D()# parameters and initial conditionsN=5F=8.0u₀=F*ones(N)u₀[1]+=0.01# small perturbation# The Lorenz-96 model is predefined in DynamicalSystems.jl:ds=Systems.lorenz96(N;F=F)# Equivalently, to define a fast version explicitly, do:structLorenz96{N}end# Structure for size typefunction(obj::Lorenz96{N})(dx,x,p,t)where{N}F=p[1]# 3 edge cases explicitly (performance)@inboundsdx[1]=(x[2]-x[N-1])*x[N]-x[1]+F@inboundsdx[2]=(x[3]-x[N])*x[1]-x[2]+F@inboundsdx[N]=(x[1]-x[N-2])*x[N-1]-x[N]+F# then the general casefornin3:(N-1)@inboundsdx[n]=(x[n+1]-x[n-2])*x[n-1]-x[n]+Fendreturnnothingendlor96=Lorenz96{N}()# create structds=ContinuousDynamicalSystem(lor96,u₀,[F])# And now evolve a trajectorydt=0.01# sampling timeTf=30.0# final timetr=trajectory(ds,Tf;dt=dt)# And plot in 3D:x,y,z=columns(tr)plot3D(x,y,z)