In [1]:
%matplotlib inline
In [2]:
import numpy
import itertools
from matplotlib import pyplot as plt
In [3]:
class r(object):
""" Particle position
Keywords Arguments:
r0 -- initial position [m]
v0 -- initial velocity [m/s]
a -- acceleration [m/s^2]
"""
__slots__=("r0", "v0", "a")
def __init__(self, r0:"iterable<float>", v0:"iterable<float>", a:"iterable<float>"):
self.r0 = r0
self.v0 = v0
self.a = a
def calculo_coordenadas(self, t:"float") -> "iterable<float>":
return [i + j*t + k/2 * t**2 for i, j, k in itertools.zip_longest(self.r0, self.v0, self.a)]
def calculo_velocidad(self, t: "float") -> "iterable<float>":
return [i + j*t for i, j in itertools.zip_longest(self.v0, self.a)]
def __repr__(self) -> "str":
return '({}, {})i'.format(self.r0[0], self.v[0])
In [4]:
vector = r([0, 2, 0], [0, 49, 0], [0, -9.8, 0])
In [5]:
# Calculamos vector posicion y vector velocidad para cada
# instante de tiempo de la simulacion
t = numpy.linspace(0, 10, 50)
p = [vector.calculo_coordenadas(i) for i in t]
v = [vector.calculo_velocidad(i) for i in t]
In [6]:
# Extraemos las componentes en cada direccion
x = [i[0] for i in p]
y = [i[1] for i in p]
z = [i[2] for i in p]
vx = [i[0] for i in v]
vy = [i[1] for i in v]
vz = [i[2] for i in v]
In [7]:
fig, ax1 = plt.subplots()
ax1.plot(t, y, 'b-')
ax1.set_xlabel("tiempo [s]")
ax1.set_ylabel("position (y) [m]")
ax2 = ax1.twinx()
ax2.plot(t, vy, 'r')
ax2.set_ylabel("velocidad [m/s]")
# Lo hago asi, anadiendo dos dibujos vacios con label, porque si no es un lio
# importante. Explicacion para hacerlo bien y mal (asi) en:
# http://stackoverflow.com/questions/5484922/secondary-axis-with-twinx-how-to-add-to-legend
ax1.plot(0, 0, 'b-', label = 'altura')
ax1.plot(0, 0, 'r-', label = 'velocidad')
ax1.legend(loc=0)
plt.show()
In [ ]: