Search code examples
pythonmatplotlibwhile-loopprojectile

Projectile Motion in Python: How to generate list giving vertical velocity over time?


I'm trying to find how the vertical velocity Vy of a spherical particle falling vertically with drag changes as a function of time t. I would like to generate a list of values that state what Vy is for several points in time from the time at which is is released to some t_max as well as plot Vy vs time.

The equation I've been given to solve for ∆Vy is:

dVy = -g*dt - (b/m)*Vy*dt

and I've been given a suggested algorithm for finding the values

"The code should ask for the values of m, g, b and ∆t. Once these values are defined, the code should provide a series of values of Vy for each time t."

Here's my attempt so far:

import numpy as np
import matplotlib.pyplot as plt
import math

g = 9.81 #acceleration due to gravity
b = 1.6*10**-8 #linear drag coefficient
m = 1.04*10**-9 #mass of particle
dt = 10**-3 #time step
t_max = 10 #final time
t = 0 #initial time
Vy = 0 #initial velocity

t_list = [t]
Vy_list = [Vy]
dVy = -g*dt - (b/m)*Vy*dt

while t <= t_max:

    t += dt
    Vy += dVy
    t_list.append(t)
    Vy_list.append(Vy)
    print(t, Vy)


plt.plot(t_list, Vy_list)
plt.show()

I'm not sure that the values it generates are quite right considering that no matter what size I set the mass to the change in velocity remains the same when I'd expect it to affect the velocity quite a bit for infinitesimally smaller masses.

I'll admit that I'm replicating a solution that was given to me for a completely different problem, but am I right in thinking that a while loop is the way to go?


Solution

  • You need to update dVy in the while loop:

    while t <= t_max:
    
        dVy = -g*dt - (b/m)*Vy*dt
    
        t += dt
        Vy += dVy
    

    Otherwise it will be a constant and not update as you expect over time.