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()
Asked
Active
Viewed 68 times
1
-
this is a program of plotting projectile motion using the matplotlib library. the functions xy_plot1 and xy_plot2 take two inputs(u, theta), "u" is the initial speed of the projectile, and theta is the angle it is thrown at. the program works fine for most values of theta except when the angles are complementary i.e. if I input 60 and 30 as theta for the to functions I am unable to figure out why this error is arising... – sandy Jun 08 '20 at 02:13
-
Please reformat your code; it looks like you were able to make some of it formatted correctly, but some is not. What error are you getting? – asylumax Jun 08 '20 at 04:32
1 Answers
0
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()

Derek O
- 16,770
- 4
- 24
- 43