""" Exercise 5.11 This is the same as 5.10, but we will also compute the max values of t and y, and use these to set the size of the axes. The axes are normally set automatically by matplotlib, but in some cases (for instance animation) it is better to set them manually. """ import numpy as np import matplotlib.pyplot as plt import sys v0_list = [float(v) for v in sys.argv[1:]] g = 9.81 #the largest t occurs for the largest v0: v0_max = max(v0_list) t_max = 2*v0_max/g """ Thinking like a physicist, one may see from the parabolic shape of the formula that the highest y value will occur for the highest v0 value and for t = v0/g (the midpoint). Therefore, max_y = v0_max*t-0.5*g*t**2, with t = v0_max/g. However, we will think like programmers, and use another approach to find y_max: """ y_max = 0 #choose a value we know is < the true max for v0 in v0_list: t_stop = 2*v0/g t = np.linspace(0,t_stop,101) y = v0*t -0.5*g*t**2 if max(y) > y_max: y_max = max(y) #update y_max if we found a higher value plt.plot(t,y,label='v0 = %g' %(v0)) plt.xlabel('time (s)') plt.ylabel('height (m)') plt.title('Height of ball') plt.axis([0,t_max,0,y_max*1.1]) plt.grid() plt.legend() plt.show() """ Terminal> python plot_ball3.py 5 10 15 (output is a plot) """