Lab 4 - FouadM0426/comp.physics-spring-2025 GitHub Wiki
Goals
This lab will be a similar setup as the previous lab, we will still be using Euler's method but in a different application compared to the previous lab. In this lab we will be using Euler's method to model systems changing with time (Ît)
Projectile movement in 2D
In this part we will have to keep track of two different variables unlike the previous lab where we had only 1 variable to keep track of. In this lab we will be keeping track of position and velocity, for this 2d modeling of a projectile
x[i] = x[i-1] + vx[i-1]*dt vx[i] = vx[i-1] + ax(input_parameters)*dt
y[i] = y[i-1] + vy[i-1]*dt vy[i] = vy[i-1] + ay(input_parameters)*dt we will be using the following equations above to calculate the position and velocity on both x and y axis.
- setting up variables
# enter the mass, radius of cannon ball
m = 10
r = .5
# enter acceleration
g = -9.8
# enter initial conditions
theta0_deg = 30 # degrees
v0 = 700. #m/s for a cannon ball
# convert angle to radians - numpy functions need angle in radians!
theta0 = np.radians(theta0_deg)
y0 = 100
x0 = 0
# calculate the initial x and y velocities
v0x = v0* np.sin(theta0)
v0y = v0* np.cos(theta0)
# enter time step dt
dt =.05
The following code above will show our starting plots, initial data, and calculation of v0x and v0y
- Air resistance calculation We will have to add a function to calculate the air resistance for our lab, we will be using the following equation B_2=.5CĪA which will give us the following ~ 0.2405
C = 0.5
rho = 1.225
A = np.pi*r**2 # pi*r^2
myB2 = .5*C*rho*A
- Functions In this lab we will be using different functions to calculate and initialize our data to provide x,y positions for our graph
- For a_y
# write your function to calculate the vertical acceleration
def calculate_ay(v, vy, m, myB2):
ay = g + (-(myB2/(m))*v*vy)
return ay
- For a_x
# write your function to calculate the horizontal acceleration
def calculate_ax(v, vx, m, myB2):
ax = -(myB2/(m))*v*vx
return ax
- For our Euler function this is where the fun begins, we have to use one function to initialize the x,y positions, and velocity.
def calculate_euler(x0, y0, v0x, v0y, dt,myB2):
x = [x0]
y = [y0]
vx = [v0x]
vy = [v0y]
t = [0]
i=0
while y[i-1] >= 0:
i+=1
ax = calculate_ax(np.sqrt(vx[i-1]**2 + vy[i-1]**2), vx[i-1], m, myB2)
ay = calculate_ay(np.sqrt(vx[i-1]**2 + vy[i-1]**2), vy[i-1], m, myB2)
t.append(t[i-1] + dt)
vx.append(vx[i-1] + ax*dt)
vy.append(vy[i-1] + ay*dt)
x.append(x[i-1] + vx[i-1]*dt)
y.append(y[i-1] + vy[i-1]*dt)
return x, y, vx, vy, t
x, y, vx, vy, t = calculate_euler(x0, y0, v0x, v0y, dt, myB2)
- Plot data for Euler above
plt.plot(x,y)
plt.xlabel("x (m)")
plt.ylabel("y (m)")
plt.title("CANNON BALL")
plt.show()
- Varying angle effects The python code below will show you the effects of changing the range of angles from which the cannon ball is shot, that is done using theta0_deg = np.arange(15,90,15)
theta0_deg = np.arange(15,90,15)
for theta_deg in theta0_deg:
theta0 = np.radians(theta_deg)
v0x = v0* np.sin(theta0)
v0y = v0* np.cos(theta0)
x, y, vx, vy, t = calculate_euler(x0, y0, v0x, v0y, dt, myB2)
plt.plot(x,y, label=f"{theta_deg} degrees")
plt.xlabel("x (m)")
plt.ylabel("y (m)")
plt.title("Projectile Motion")
plt.legend()
plt.show()