""" Exercise 5.13 from "A primer on... Animate the trajectory of a ball. We should plot the curve for y(x) > 0, so we first need to solve y(x) = 0 to find the upper bound for x. The formula is quadratic, so we solve a second-order equation with the regular abc-formula. """ import numpy as np import matplotlib.pyplot as plt import sys y0 = float(sys.argv[1]) theta = float(sys.argv[2]) v0 = float(sys.argv[3]) g = 9.81 a = -g / (2 * v0**2 * 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) #Use the largest of the two solutions (the positive one): x_stop = max(x1, x2) #create the x array, compute y, and plot the function: x = np.linspace(0, x_stop, 101) y = a * x**2 + b * x + c plt.plot(x, y) plt.show()