Projectile Motion-Euler Method

#Projectile Motion-Euler Method
from pylab import *
g=-9.8
y=input('Height from which projectile is launched: ')
v,theta=input('Enter velocity and angle of projection (in degrees): ')
theta=radians(theta)
vx=v*cos(theta)
vy=v*sin(theta)
dt=0.01
t=x=0
tdata=[]
xdata=[]
ydata=[]
vdata=[]
while y>=0.0:
    tdata.append(t)
    xdata.append(x)
    ydata.append(y)
    vdata.append(v)
    t=t+dt
    x=x+vx*dt
    vy=vy+g*dt
    y=y+vy*dt
    v=hypot(vx,vy)
#Printing
for (t,x,y,v) in zip(tdata,xdata,ydata,vdata):
    print '\t%10.3f\t\t%10.3f\t\t%10.3f\t\t%10.3f'%(t,x,y,v)
#Plotting
title('Projectile Motion')
xlabel('Horizontal distance')
ylabel('Height')
plot(xdata,ydata)
show()

#for damped case, vx=vx+ax*dt, vy=vy+ay*dt, ax=-d*vx2*v, ay=-d*vy2*v+g

Output


#Trajectory of projectile with & without air drag
from pylab import*
g=9.8
h=y1=y2=input('Height from which projected? ')
k=input('Air drag factor (k)? ')
u,theta=input('Velocity and angle(deg) of projection? ')
theta=radians(theta)
vx=u*cos(theta)
vy=u*sin(theta)
dt=0.01
t=x1=x2=0
v=u
t1data=[]
t2data=[]
x1data=[]
x2data=[]
y1data=[]
y2data=[]
v1data=[]
v2data=[]

while y1>=0:
        t1data.append(t)
        x1data.append(x1)
        y1data.append(y1)
        v1data.append(v)
        x1=x1+vx*dt
        y1=y1+vy*dt
        vy=vy-g*dt
        v=hypot(vx,vy)
        t=t+dt
  

t=0
v=u
vy=u*sin(theta)
ax=0
ay=-g
while y2>=0:
        t2data.append(t)
        x2data.append(x2)
        y2data.append(y2)
        v2data.append(v)
        x2=x2+vx*dt
        y2=y2+vy*dt
        vx=vx+ax*dt
        vy=vy+ay*dt
        v=hypot(vx,vy)
        ax=ax-k*v*vx
        ay=ay-k*v*vy
        t=t+dt
                
print '\nWithout Air drag\t\t   With Air drag'
print '-'*48
print'distance\theight\tdistance\theight'
for(x1,y1,x2,y2) in zip(x1data,y1data,x2data,y2data):
        print('\n%.3f\t%.3f\t%.3f\t%.3f'%(x1,y1,x2,y2))
title('Projectile motion')
xlabel('Range')
ylabel('Height')
text(x2/2,max(y2data)/2,'k = '+str(k))
p1=plot(x1data,y1data)
p2=plot(x2data,y2data)
legend(['without air drag','with air drag'])
show()


output 
 

Comments