Lab 4 - Curlss/Computational_physics GitHub Wiki

In Lab 4, the goal was to use the Euler method to draw the x and y positions of an item moving like a bullet. We imagined an object moving like a projectile that was encountering air resistance. Because of this, we couldn't use a final max time to end the calculation; we had to find another way to do so. When we added air resistance to our motion, we wanted to see how the Euler method would work. We also wanted to see how different directions would change the way our object moved. We had to make a graph that goes through these ranges so that it is easy to see how the range changes when we aim our weapon at different angles.

This lab's main objective is to determine an object's x and y positions in projectile motion using Euler’s method. Unlike simple projectile motion, our analysis accounts for air resistance, meaning both gravity and drag force must be considered. These forces are crucial because they determine acceleration, which in turn affects velocity and position.

Euler’s method relies on the equations:

  • Position: ( x_i = x_{i-1} + v_{i-1} \cdot dt )
  • Velocity: ( v_i = v_{i-1} + a_{i-1} \cdot dt )

We are working with air resistance, so we needed to determine the coefficient of air resistance, which is myB2.

# calculate B2 here and save it as myB2
C = .5 #constant
rho = 1.225 #kg/m^3
A = np.pi*r**2 #m^2
myB2 = .5*C*rho*A

We first need acceleration equations for both the x and y directions to calculate position, as these incorporate the forces acting on the projectile. After defining these acceleration functions, we implement them in our position function.

def calculate_Ay(v, vy,m, myB2):
  ay = -g - myB2/m * v * vy
  return ay
def calculate_Ax(v, vx,m, myB2):
  ax = -myB2/m * v * vx
  return ax

A key part of the implementation is determining when to stop the calculation. Since the projectile will hit the ground at ( y = 0 ), we set a condition in our while loop to continue calculations until ( y ) reaches zero. By initializing our parameters and setting this stopping criterion, we ensure an accurate simulation of projectile motion with air resistance.

# write your calculate function to compute the Euler solution for position and velocity
# NOTE: This function will initialize the x/y position and velocities as empty lists

def calculate(x0, y0, v0x, v0y, dt, m , myB2):
  x = [x0]
  vx = [v0x]
  y = [y0]
  vy = [v0y]
  t = [0]
  i = 1
  while y[i-1] >= 0: #while
    x.append(x[i-1] + vx[i-1]*dt)
    y.append(y[i-1] + vy[i-1]*dt)
    v = np.sqrt(vx[i-1]**2 + vy[i-1]**2)
    ax = calculate_Ax(v, vx[i-1], m, myB2)
    ay = calculate_Ay(v, vy[i-1], m, myB2)
    vx.append(vx[i-1] + ax*dt)
    vy.append(vy[i-1] + ay*dt)
    t.append(t[i-1] + dt)
    i = i + 1
  return x, vx, y, vy, t
#this function will initialize the x/y position and velocities as empty lists

Now we ran it and plotted our results for the projectile with air resistance and explored different staring angles. This is what our Projectile looked like image

When we explored different angles we needed to create a loop that would run different theta points

plt.figure()
theta0_deg = np.arange(15,90,15)
theta0 = np.radians(theta0_deg)
for i in theta0:
  v0x =v0*np.cos(i)
  v0y =v0*np.sin(i)
  x, vx, y, vy, t = calculate(x0, y0, v0x, v0y, dt, m, myB2)
  plt.plot(x, y, '.-', label='air resistance' )
  plt.title('Projectile Motion with Air Resistance')
  plt.xlabel('x (m)')
  plt.ylabel('y (m)')
  plt.legend()
plt.show()

image

We looked into how the range varies with the initial angle of the projectile, which varies between 15 and 90 degrees, where the air resistance is every 15 degrees. Not only that, but we wanted to compare the results of an object with air resistance and without, so we also created a graph where there is no air resistance, so there is no drag coefficient. There are two graphs, one with the projectile motion and the other for the different angles between 15 and 90 degrees

image

image

When comparing the ranges, you can see that the projectile motion with air resistance x and y are a lot smaller because there is a force pushing against and the other projectile with no air resistance has a higher number for the x and y cause nothing is slowing it down.