""" Exercise 5.13 from "A primer on..." Plot the trajectory of a ball. The task is made slightly more complicated by the fact that we are supposed to plot for y >0, which means that we must solve the equation f(x) = 0 to find the upper bound for x. The equation is quadratic, so the solution can be found using the standard abc-formula for quadratic equations. """ import numpy as np import matplotlib.pyplot as plt import sys y0, theta, v0 = [float(e) for e in sys.argv[1:]] g = 9.81 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) x_max = max(x1, x2) x = np.linspace(0, x_max, 101) y = a * x**2 + b * x + c plt.plot(x,y) plt.show()