pythonprojectile

How to make a condition to terminate appending?


I am writing a code to plot several projectile trajectories of various theta values in Python.

theta = np.arange(np.pi/6, np.pi/3)
t = np.linspace(0,2,num=100)
while y0>=0:
    for i in theta:
        x = []
        y = []
        for k in t:
                x0= v_0*np.cos(i)*k
                y0= v_0*np.sin(i)*k - 1/2*g*(k**2)
                x.append(x0)
                x.append(y0)

After forming the arrays and putting in the necessary conditions for projectile, I have used a while loop to put the terminating instruction in the program. I think, I am missing a crucial point. Thanks!


Solution

  • I think you want your terminating condition inside your inner-most loop. See below, where I also defined a couple of missing constants (v_0, g) and fixed one x to y. also printing the results

    theta = np.arange(np.pi/6, np.pi/3)
    t = np.linspace(0,2,num=100)
    v_0 = 1
    g=10
    
    for i in theta:
        x = []
        y = []
        for k in t:
            x0= v_0*np.cos(i)*k
            y0= v_0*np.sin(i)*k - 1/2*g*(k**2)
            x.append(x0)
            y.append(y0)
            if y0 < 0: # the main change here. Stop looping when y_0 below zero
                break
        print(f'theta:{i}')
        print(f'x:{x}')    
        print(f'y:{y}')
    

    produces

    theta:0.5235987755982988
    x:[0.0, 0.017495462702715934, 0.03499092540543187, 0.052486388108147805, 0.06998185081086374, 0.08747731351357968]
    y:[0.0, 0.008060401999795939, 0.012039587797163551, 0.011937557392102841, 0.007754310784613805, -0.0005101520253035577]
    

    Plotting it (y vs x), looks reasonable

    ![trajectory

    It is also worth noting that your definition of theta = np.arange(np.pi/6, np.pi/3) looks rather strange, what are you trying to achieve here?