tomemos en cuenta que vx=˙x y vy=˙y y con ello obtenemos: ˙x=vx˙vx=0˙y=vy˙vy=−g
from pylab import *
from scipy.integrate import *
from matplotlib import animation,rc
from IPython.display import HTML
%config InlineBackend.figure_format = 'retina' # Solo para pantallas de alta resolucion
def tiro_sf(x_v,t,g=9.8):
# x_v son x,v_x,y,v_y
# con indices 0, 1 ,2, 3
dx = x_v[1]
dvx = 0
dy = x_v[3]
dvy = -g
return array([dx,dvx,dy,dvy])
t = linspace(0,0.5)
cond_ini = [0,2,0,2]
x_v = odeint(tiro_sf,cond_ini,t)
plot(x_v[:,0],x_v[:,2],'o-')
grid()
show()
def trayectoria(sist_ecs,ci,dt=0.01,N=50):
x_t,y_t = zeros(N),zeros(N)
x_t[0],y_t[0] = ci[0],ci[2]
for i in range(N-1):
x_v = odeint(sist_ecs,ci,[0,dt])
ci = x_v[-1,:]
if x_v[0,2]*x_v[1,2] < 0:
print("Hay un rebote, con indice: ",i)
ci = x_v[0,:]
ci = rebote(sist_ecs,ci,dt)
ci[3] = -ci[3]
x_t[i+1],y_t[i+1] = ci[0],ci[2]
return x_t,y_t
def rebote(sist_ecs,ci,dt,error=1e-6,max_iter=100):
# calcula el rebote unicamente en y, es decir con el indice 2
dif,i = 1,0
while dif > error or i < max_iter:
dt = 0.5*dt
x_v_rebotada = odeint(sist_ecs,ci,[0,dt])
if x_v_rebotada[0,2]*x_v_rebotada[1,2] < 0:
ci = x_v_rebotada[0,:]
else:
ci = x_v_rebotada[1,:]
dif = abs( ci[2] )
i += 1
return ci
x,y = trayectoria(tiro_sf,[0,2,0,2],N=100)
plot(x,y,'o-')
grid()
show()
print(y[40],y[41],y[42])
tomemos en cuenta que vx=˙x y vy=˙y y con ello obtenemos: ˙x=vx˙vx=−γvx˙y=vy˙vy=−g−γvy
def tiro_cf_pv(x_v,t,g=9.8,gam=0.1):
# x_v son x,v_x,y,v_y
# con indices 0, 1 ,2, 3
dx = x_v[1]
dvx = - gam*x_v[1]
dy = x_v[3]
dvy = -g -gam*x_v[3]
return array([dx,dvx,dy,dvy])
x_fpv,y_fpv = trayectoria(tiro_cf_pv,[0,2,0,2],N=100)
plot(x,y,'o-',label="Sin fricción")
plot(x_fpv,y_fpv,'o-',label="Fricción $\sim v$")
legend(),grid()
show()
tomemos en cuenta que vx=˙x y vy=˙y y con ello obtenemos: ˙x=vx˙vx=−γvvx˙y=vy˙vy=−g−γvvy donde v=√v2x+v2y
def tiro_cf_pv2(x_v,t,g=9.8,gam=0.1):
# x_v son x,v_x,y,v_y
# con indices 0, 1 ,2, 3
v = sqrt(x_v[1]**2 + x_v[3]**2)
dx = x_v[1]
dvx = - gam*v*x_v[1]
dy = x_v[3]
dvy = -g -gam*v*x_v[3]
return array([dx,dvx,dy,dvy])
x_fpv2,y_fpv2 = trayectoria(tiro_cf_pv2,[0,2,0,2],N=100)
plot(x,y,'-',label="Sin fricción")
plot(x_fpv,y_fpv,'-',label="Fricción $\sim v$")
plot(x_fpv2,y_fpv2,'-',label="Fricción $\sim v^2$")
legend(),grid()
show()
tomemos en cuenta que vx=˙x y vy=˙y y con ello obtenemos: ˙x=vx˙vx=−γvvx−f0˙y=vy˙vy=−g−γvvy donde v=√v2x+v2y
def tiro_cv(x_v,t,g=9.8,gam=0.1,f0=3):
# x_v son x,v_x,y,v_y
# con indices 0, 1 ,2, 3
v = sqrt(x_v[1]**2 + x_v[3]**2)
dx = x_v[1]
dvx = - gam*v*x_v[1] - f0
dy = x_v[3]
dvy = -g -gam*v*x_v[3]
return array([dx,dvx,dy,dvy])
x_cv,y_cv = trayectoria(tiro_cv,[0,2,0,2],N=100)
plot(x,y,'-',label="Sin fricción")
plot(x_fpv,y_fpv,'-',label="Fricción $\sim v$")
plot(x_fpv2,y_fpv2,'-',label="Fricción $\sim v^2$")
plot(x_cv,y_cv,'-',label="Fricción $\sim v^2$ y $f_0$")
legend(),grid()
show()
%%capture
fig,ejes = plt.subplots(figsize=(3.5,2),dpi=200)
ejes.set_xlim((0,2)),ejes.set_ylim((0,.21)),ejes.grid()
ejes.set_title(r'Tiro parabólico')
t1, = ejes.plot([],[],'-',color='blue')
t2, = ejes.plot([],[],'-',color='orange')
t3, = ejes.plot([],[],'-',color='green')
t4, = ejes.plot([],[],'-',color='red')
def animar(i):
t1.set_data([x[:i]],[y[:i]])
t2.set_data([x_fpv[:i]],[y_fpv[:i]])
t3.set_data([x_fpv2[:i]],[y_fpv2[:i]])
t4.set_data([x_cv[:i]],[y_cv[:i]])
return (t1,t2,t3,t4,)
pelicula = animation.FuncAnimation(fig,animar,frames=100,interval=40,blit=True)
HTML( pelicula.to_html5_video() )