""""Exercise 2.8 from "A Primer on ..." Generate a nicely formatted table of t and y values, The t values should be n + 1 equally spaced t values on the interval [a,b], and the y values are computed with the given formula. """ #values for v0, g and n: v0 = 5 g = 9.81 n = 5 #compute the boundaries a and b, and the distance between points, h: a = 0 b = 2 * v0 / g h = (b - a) / n #a) using a for loop for i in range(n+1): t = a + h * i y = v0 * t - 0.5 * g * t**2 print(f'{t:5.5f} {y:5.5f}') print('######') #b) t = a eps = 1e-10 i = 0 while t < b + eps: y = v0 * t - 0.5 * g * t**2 print(f'{t:5.5f} {y:5.5f}') t = t + h """ Terminal> python ball_table1.py 0.00000 0.00000 0.20387 0.81549 0.40775 1.22324 0.61162 1.22324 0.81549 0.81549 1.01937 0.00000 ###### 0.00000 0.00000 0.20387 0.81549 0.40775 1.22324 0.61162 1.22324 0.81549 0.81549 1.01937 0.00000 """ """ Comment on the solution: The recipe for generating the t values is exactly the same as in Ex 2.7 (coor.py). There are two main points to this exercise: 1. Using an f-string to print the output as a "nicely formatted table" 2. Getting the limit right in the while loop. Because of roundoff error a condition like t <= b may give inconsistent results. It usually works, but sometimes it fails because the numbers are supposed to be equal, but roundoff error makes t > b. The solution is to introduce a small tolerance (here called eps) and formulate the condition as in the code. """