Interpolation Exercise 1


In [1]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np

In [2]:
from scipy.interpolate import interp1d

2D trajectory interpolation

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)


[ 0.          0.1025641   0.20512821  0.30769231  0.41025641  0.51282051
  0.61538462  0.71794872  0.82051282  0.92307692  1.02564103  1.12820513
  1.23076923  1.33333333  1.43589744  1.53846154  1.64102564  1.74358974
  1.84615385  1.94871795  2.05128205  2.15384615  2.25641026  2.35897436
  2.46153846  2.56410256  2.66666667  2.76923077  2.87179487  2.97435897
  3.07692308  3.17948718  3.28205128  3.38461538  3.48717949  3.58974359
  3.69230769  3.79487179  3.8974359   4.        ] [ 0.          0.46611028  0.77169706  0.85695573  0.72231261  0.42244838
  0.04750816 -0.30261323 -0.54388671 -0.62734994 -0.54777832 -0.34071147
 -0.06970442  0.19209565  0.38094522  0.45732667  0.41334402  0.27159009
  0.07659602 -0.11830239 -0.26499214 -0.33195345 -0.31042482 -0.21434785
 -0.07471184  0.06975958  0.18292364  0.23989085  0.23207351  0.16771697
  0.06822291 -0.0384174  -0.12517681 -0.17257156 -0.17273666 -0.13023529
 -0.05972056  0.018666    0.08480265  0.1235537 ] [ 1.          0.82780523  0.4680027   0.02771914 -0.37648983 -0.64833759
 -0.73360478 -0.62942591 -0.37999092 -0.06104728  0.241879    0.45555381
  0.53591895  0.47612645  0.30460221  0.07458882 -0.15141965 -0.31800988
 -0.38984117 -0.35841475 -0.24155564 -0.07644546  0.09144496  0.22039082
  0.28235041  0.2685551   0.18979566  0.07185125 -0.05233675 -0.15149649
 -0.20358419 -0.20032746 -0.14792537 -0.0641072   0.02736911  0.10317062
  0.14610919  0.14878632  0.11446568  0.0552279 ]

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:

  • For the interpolated points, use a solid line.
  • For the original points, use circles of a different color and no line.
  • Customize you plot to make it effective and beautiful.

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