Python Lab 4 - Cart1233/PHYS250 GitHub Wiki
Goal
The goal of this lab is to use the Euler method to model system that are changing with time. If we can describe how the system changes with time, then we can determine how the system changes by stepping it forward in time using small intervals of time.
Overview
In this lab we practiced using the Euler method by modeling it using 2D projectile motion.
Functions
We first did a practice function to calculate our course grade according to their respective weights.
def my_grades(grades, weight):
"""This function takes in a list of grades and their weights and returns the course grades"""
cat_grade=grades*weight
course_grade=0
for i in cat_grade:
course_grade+=i
return course_grade
We then created two lists and assigned one with weights and another with grades.
Now into the Euler method, we first created a function to calculate acceleration in the y and x direction using air resistance with a loop to make sure it was a negative value. I will only show the y acceleration since the equations are the same besides the use of $v_x$ instead of $v_y$
def acceleration_y(v_y,m,myB2):
g=9.8
if v_y<0:
a_y=-g+(myB2/m)*v_y**2
else:
a_y=-g-(myB2/m)*v_y**2
return a_y
We then created a function to calculate our velocity and position arrays with a loop that will give us only 0 and positive numbers for position.
def calculate(x0,y0,v0x,v0y,dt,myB2,m):
v_x=[v0x]
v_y=[v0y]
x=[x0]
y=[y0]
t=[0]
i=1
while y[i-1]>=0:
t.append(t[i-1]+dt)
x.append(x[i-1] + v_x[i-1]*dt)
v_x.append(v_x[i-1] + acceleration_x(v_x[i-1],m,myB2)*dt)
y.append(y[i-1] + v_y[i-1]*dt)
v_y.append(v_y[i-1] + acceleration_y(v_y[i-1],m,myB2)*dt)
i=i+1
return v_x,v_y,x,y,t
Finally we called our function so that we would be able to graph our results.
v_x_calc,v_y_calc,x_calc,y_calc,t_calc=calculate(x0,y0,v0x,v0y,dt,myB2,m)
Graphs and Plots
After obtaining all the data from our Euler method functions we were able to make plots of position and velocity vs time using the following pot code,
# plot your results
plt.subplot(2,2,4)
plt.plot(t_calc,v_x_calc,'m-',label='Velocity in the x vs Time',)
plt.xlabel('Time(s)')
plt.ylabel('Velocity(m/s)')
plt.legend()
plt.subplot(2,2,3)
plt.plot(t_calc,v_y_calc,'r-',label='Velocity in the y vs Time' )
plt.xlabel('Time(s)')
plt.ylabel('Velocity(m/s)')
plt.legend()
plt.subplot(2,2,2)
plt.plot(t_calc,x_calc,'g-',label='x-Position vs Time')
plt.xlabel('Time(s)')
plt.ylabel('Position(m)')
plt.legend()
plt.subplot(2,2,1)
plt.plot(t_calc,y_calc,'b-',label='y-Position vs Time')
plt.xlabel('Time(s)')
plt.ylabel('Position(m)')
plt.legend()
which makes 4 different subplots for each velocity and position. The graphs looked as so.
We then made a graph of y vs x values with different starting angles by making a loop and running it through the different $\Theta$ values.
theta0_deg = np.arange(15,90,15)
plt.figure()
for theta in theta0_deg:
theta01=np.radians(theta)
v0x=v0*np.cos(theta01)
v0y=v0*np.sin(theta01)
v_x_calc,v_y_calc,x_calc,y_calc,t_calc=calculate(x0,y0,v0x,v0y,dt,myB2,m)
plt.plot(x_calc,y_calc,label=r"$\theta ={}^\circ$".format(theta))
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
This gave the following results.
Summary Questions
The ranges vary for each launch angle, with 75 degrees going the shortest in the x direction and 30 degrees just nudging ahead of 45 degrees. The higher the launch angle the higher the projectile will travel in the y direction, but the x distance reaches its maximum at the mid-range of the angles.