In [1]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
In [2]:
from scipy.interpolate import interp1d
The file trajectory.npz
contains 3 Numpy arrays that describe a 2d trajectory of a particle as a function of time:
t
which has discrete values of time t[i]
.x
which has values of the x position at those times: x[i] = x(t[i])
.x
which has values of the y position at those times: y[i] = y(t[i])
.Load those arrays into this notebook and save them as variables x
, y
and t
:
In [3]:
with np.load('trajectory.npz') as data:
t = data['t']
x = data['x']
y = data['y']
print(t,x,y)
In [4]:
assert isinstance(x, np.ndarray) and len(x)==40
assert isinstance(y, np.ndarray) and len(y)==40
assert isinstance(t, np.ndarray) and len(t)==40
Use these arrays to create interpolated functions $x(t)$ and $y(t)$. Then use those functions to create the following arrays:
newt
which has 200 points between $\{t_{min},t_{max}\}$.newx
which has the interpolated values of $x(t)$ at those times.newy
which has the interpolated values of $y(t)$ at those times.
In [5]:
x_interp = interp1d(t,x, kind='cubic')
y_interp = interp1d(t,y, kind='cubic')
newt = np.linspace(np.min(t), np.max(t), 200)
newx = x_interp(newt)
newy = y_interp(newt)
In [6]:
assert newt[0]==t.min()
assert newt[-1]==t.max()
assert len(newt)==200
assert len(newx)==200
assert len(newy)==200
Make a parametric plot of $\{x(t),y(t)\}$ that shows the interpolated values and the original points:
In [15]:
f = plt.figure(figsize=(12,12))
ax = plt.subplot(111)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.yaxis.set_ticks_position('left')
ax.xaxis.set_ticks_position('bottom')
plt.plot(x, y, 'bo')
plt.plot(newx, newy, 'r-')
plt.title("Trajectory of a Particle")
plt.xlabel("X")
plt.ylabel("Y");
In [ ]:
assert True # leave this to grade the trajectory plot