Ejercicios y pruebas varias del Tema 2 : La Dinamica de Newton


In [1]:
%matplotlib inline

In [2]:
import numpy
import itertools
from matplotlib import pyplot as plt
#### Particle movement ####

In [10]:
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:"iterMovimientoable<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 [11]:
vector = r([0, 2, 0], [0, 49, 0], [0, -9.8, 0])

In [12]:
# Calculating position and velocity vector for each step
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 [13]:
# Taking components in the three dimensions
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 [21]:
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.hlines(0, 0, 10, 'grey', 'dashed')
ax2.set_ylabel("velocidad [m/s]")

# I do this, adding two new empty plots with the same color than real ones, and adding also
# a label to them, because is a mess doing it in the right way.
#
# A verty good explanation:
# 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 [1]:
5*7*13*29


Out[1]:
13195

In [ ]: