pythonpython-3.xmatplotlibgraphprojectile

the graphs of the two projectiles does not work properly when complementary angles(eg 30 and 60) are passes to xy_plot1 and xyplot2 function


import matplotlib.pyplot as plt

import numpy as np

import math

import matplotlib.gridspec as gridspec

from matplotlib.animation import FuncAnimation

fig = plt.figure()

plt.xlabel('X')

plt.ylabel('Y')

# limiting the y and x axis
plt.ylim(0, 10)

plt.xlim(0, 10)

def xy_plot1(u, theta):

    y_arr1= []
    x_arr1 = []

    # displacement in the y_direction is zero
    x_disp = (u*u)*(math.sin(2*theta))/9.8  # disp_x = (u^2)*sin(2theta)/g {horizontal                                                                                  range}

    x = 0           # distance from the origin

    while(x <= x_disp):
        # below is the equation of path of projectile 
        y = (x*(math.tan(theta))) - ((9.8*x*x)/(2*pow((u*math.cos(theta)), 2)))
        y_arr1.append(y)
        x_arr1.append(x)
        x = x + 0.1 # basically x = x + dx  

    plt.plot(x_arr1, y_arr1)

def xy_plot2(u, theta):

    y_arr2 = []
    x_arr2 = []

    # displacement in the y_direction is zero
    x_disp = (u*u)*(math.sin(2*theta))/9.8  # disp_x = (u^2)*sin(2theta)/g {horizontal                                                                                  range}

    x = 0           # distance from the origin
    dx = 0.1
    while(x <= x_disp):
        # below is the equation of path of projectile 
        y = (x*(math.tan(theta))) - ((9.8*x*x)/(2*pow((u*math.cos(theta)), 2)))
        y_arr2.append(y)
        x_arr2.append(x)
        x = x + dx

    plt.plot(x_arr2, y_arr2)

xy_plot1(10, 60)

xy_plot2(10, 30)

plt.show()

Solution

  • Be careful! Your theta argument for the xyplot() function is in degrees, but inside your function, the math.sin() function takes the argument for the angle in units of radians. The easiest fix is to provide your theta argument in units of radians instead of degrees.

    You also don't need both functions if they do the exact same thing, as plt.plot() will draw subsequent projectile curves along with previous ones as long as you don't clear the plot.

    import matplotlib.pyplot as plt
    import numpy as np
    import math
    import matplotlib.gridspec as gridspec
    from matplotlib.animation import FuncAnimation
    
    fig = plt.figure()
    
    plt.xlabel('X')
    plt.ylabel('Y')
    
    # limiting the y and x axis
    plt.ylim(0, 10)
    plt.xlim(0, 10)
    
    def xy_plot(u, theta):
        y_arr2 = []
        x_arr2 = []
    
        # displacement in the y_direction is zero
        x_disp = (u*u)*(math.sin(2*theta))/9.8  # disp_x = (u^2)*sin(2theta)/g {horizontal                                                                                  range}
    
        x = 0           # distance from the origin
        dx=0.1
        while(x <= x_disp):
            # below is the equation of path of projectile 
            y = (x*(math.tan(theta))) - ((9.8*x*x)/(2*pow((u*math.cos(theta)), 2)))
            y_arr2.append(y)
            x_arr2.append(x)
            x = x + dx
    
        plt.plot(x_arr2, y_arr2)
    
    # be careful about using degrees versus radians!!
    xy_plot(10, math.pi/3)
    xy_plot(10, math.pi/6)
    plt.show()
    

    enter image description here