""" Exercise 5.13 This is a standard plotting exercise, except for the requirement to plot y=f(x), for y >=0. This means we need to find the x-interval where y>=0, and use this to construct our x array. Since f(x) is quadratic in x, we can solve it with the standard formula. """ import numpy as np import matplotlib.pyplot as plt import sys g = 9.81 y0, theta, v0 = [float(p) for p in sys.argv[1:]] """ First solve f(x) = 0, using the standard formula, since f is on the form a*x**2 + b*x + c = 0 """ a = -(1/(2*v0**2))*(g/np.cos(theta)**2) b = np.tan(theta) c = y0 x1 = (-b + np.sqrt(b**2-4*a*c))/(2*a) x2 = (-b - np.sqrt(b**2-4*a*c))/(2*a) #choose the largest (the other should be negative) x_max = max(x1,x2) #now we know the x range and can make the array x = np.linspace(0,x_max,101) #evaluate formula using a,b,c, since we defined these above: y = a*x**2 + b*x +c plt.plot(x,y) #decorate the plot plt.xlabel('distance (m)') plt.ylabel('height (m)') plt.title('Trajectory of a ball') plt.show() """ Terminal> python plot_trajectory.py 1 0.78 5 (output is a plot) """