In [99]:
%matplotlib inline
import random
import matplotlib.pyplot as plt
import numpy as np
import math
In [100]:
nsteps = 1000
rg = np.array(range(nsteps))
velocity = np.zeros(nsteps)
heading = np.zeros(nsteps)
In [123]:
velocity[0] = 1.25
heading[0] = 0
change = np.cumsum(np.random.poisson(200, 5))
#change_head =
for i in range(1,1000):
velocity[i] = velocity[i-1]
heading[i] = heading[i-1]
if i in change:
velocity[i] += random.choice([-1,1]) * random.uniform(-0.5,0.5)
heading[i] += random.choice([-1,1]) * random.uniform(-math.pi/8,math.pi/8)
plt.plot(velocity)
plt.plot(heading)
Out[123]:
In [124]:
def rect(row):
r = row[0]
theta = row[1]
x = r * math.cos(theta)
y = r * math.sin(theta)
return np.array([x,y])
trajectory_polar = np.vstack((velocity,heading)).transpose()
trajectory_cart = np.apply_along_axis(rect, 1, trajectory_polar)
plt.plot(trajectory_cart)
Out[124]: