Skip to content

Instantly share code, notes, and snippets.

@mbudisic
Created September 19, 2022 16:12
Show Gist options
  • Save mbudisic/50c71eeff5f9eacf50fca3f807ab020e to your computer and use it in GitHub Desktop.
Save mbudisic/50c71eeff5f9eacf50fca3f807ab020e to your computer and use it in GitHub Desktop.
Random walker with damping and inertia
import numpy as np
def xyindependent(x,b,c,dt,randomfun):
for idx in range(2,len(x)):
x[idx] =(2-b*dt)*x[idx-1] + (b*dt-c*dt*dt-1)*x[idx-2] + dt*dt*randomfun()
return x
import matplotlib.pyplot as plt
x0 = np.zeros(100,)
y0 = np.zeros(100,)
dt = 0.01
damping = -0.5 # play around
mass = 1/3 # play around
x0 = xyindependent(x0,-damping,1./mass,dt, lambda : np.random.uniform(-0.1,0.5))
y0 = xyindependent(y0,-damping,1./mass,dt, lambda : np.random.uniform(-0.1,0.5))
fig = plt.figure()
ax = fig.add_subplot(111)
plt.plot(x0,y0,label="x/y")
ax.set_aspect(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment