Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
restrepo
GitHub Repository: restrepo/ComputationalMethods
Path: blob/master/homework/homework_2018_1_10_1216730080.ipynb
934 views
Kernel: Python 3
%pylab inline import numpy as np # JSAnimation import available at https://github.com/jakevdp/JSAnimation from JSAnimation import IPython_display from matplotlib import animation from scipy import optimize from scipy import interpolate
Populating the interactive namespace from numpy and matplotlib
import pandas as pd df=pd.DataFrame() m=.5 g=9.8 for c in [0,0.1,0.5]: x=np.array([0,50,0]) v=np.array([5,0,0]) p=m*v deltat=0.01 t=0 df=df.append({'x':x,'p':p,'t':t,'c':c},ignore_index=True) for t in np.arange( 0,3,deltat): Fg=np.array([0,-m*g,0])-c*(p/m) p=p+Fg*deltat x=x+(p/m)*deltat t=t+deltat df=df.append({'x':x,'p':p,'t':t,'c':c},ignore_index=True)
c=0 plt.plot(df[df.c==c].x.str[0], df[df.c==c].x.str[1]) c=0.1 plt.plot(df[df.c==c].x.str[0], df[df.c==c].x.str[1]) c=0.5 plt.plot(df[df.c==c].x.str[0], df[df.c==c].x.str[1]) plt.xlabel('$x$ [m]',size=15) plt.ylabel('$y$ [m]',size=15) plt.grid()
Image in a Jupyter notebook
#para c=0 c1=0 t1=np.array(df[df.c==c1].t) Y1=np.array(df[df.c==c1].x.str[1]) plt.plot(t1,Y1) plt.xlabel("Tiempo[s]") plt.ylabel("Altura[m]") plt.grid()
Image in a Jupyter notebook
#tiempos muestra para la interpolación tm1=np.array([t1[0],t1[150],t1[300]]) Ym1=np.array([Y1[0],Y1[150],Y1[300]]) position1=interpolate.lagrange(tm1,Ym1)
nt1=np.linspace(0,3.5) plt.plot(nt1,position1(nt1),label="Interpolación") plt.xlabel("Tiempo[s]") plt.ylabel("Altura[m]") plt.legend() plt.grid()
Image in a Jupyter notebook
cero1=optimize.newton(position1,3) print("Para c={} la altura y=0[m] se alcanza en t={}[s]".format(c1,cero1))
Para c=0 la altura y=0[m] se alcanza en t=3.189386738116265[s]
#para c=0.1 c2=0.1 t2=np.array(df[df.c==c2].t) Y2=np.array(df[df.c==c2].x.str[1]) plt.plot(t2,Y2,'r') plt.xlabel("Tiempo[s]") plt.ylabel("Altura[m]") plt.grid()
Image in a Jupyter notebook
#tiempos muestra para la interpolación tm2=np.array([t2[0],t2[150],t2[300]]) Ym2=np.array([Y2[0],Y2[150],Y2[300]]) position2=interpolate.lagrange(tm2,Ym2)
nt2=np.linspace(0,3.7) plt.plot(nt2,position2(nt2),'r',label="Interpolación") plt.xlabel("Tiempo[s]") plt.ylabel("Altura[m]") plt.legend() plt.grid()
Image in a Jupyter notebook
cero2=optimize.newton(position2,5) print("Para c={} la altura y=0[m] se alcanza en t={}[s]".format(c2,cero2))
Para c=0.1 la altura y=0[m] se alcanza en t=3.5334838873417995[s]
#para c=0.5 c3=0.5 t3=np.array(df[df.c==c3].t) Y3=np.array(df[df.c==c3].x.str[1]) plt.plot(t,Y,'g') plt.xlabel("Tiempo[s]") plt.ylabel("Altura[m]") plt.grid()
Image in a Jupyter notebook
#tiempos muestra para la interpolación tm3=np.array([t3[0],t3[150],t3[300]]) Ym3=np.array([Y3[0],Y3[150],Y3[300]]) position3=interpolate.lagrange(tm3,Ym3)
nt3=np.linspace(0,5.5) plt.plot(nt3,position3(nt3),'g',label="Interpolación") plt.xlabel("Tiempo[s]") plt.ylabel("Altura[m]") plt.legend() plt.grid()
Image in a Jupyter notebook
cero3=optimize.newton(position3,5) print("Para c={} la altura y=0[m] se alcanza en t={}[s]".format(c3,cero3))
Para c=0.5 la altura y=0[m] se alcanza en t=5.2049952381131614[s]