due 9/19/2024 before midnight via Learning Suite 25 possible points
Roundoff error occurs because numbers must be represented with limited precision on a computer. Write a function to compute the following quantity for an integer input \(n\) (use a for loop rather than a built-in sum function, built-in functions usually do smart things like pairwise summation to reduce/eliminate this issue):
\[f(n) = \left| \frac{n}{10} - \sum_{i = 1}^n 0.1 \right|\]Mathematically the result should be zero, but with finite precision arithmetic it will not be zero because of roundoff error (and the error will get worse as \(n\) increases).
Write a loop that computes the absolute error for \(n = 10, 100, 1000, \dots, 1000000\).
Plot the roundoff error with \(n\) on the x-axis and roundoff error on the y-axis. When data spans multiple orders of magnitude, as it does in this case, a log scale is usually preferable. (see pyplot.loglog
). Always labels your axes!
Truncation Error. One method to calculate \(\pi\) is the Leibniz formula:
\[\pi = 4 \sum_{k = 0}^\infty \frac{(-1)^k}{2k + 1}\]Because we cannot use an infinite number of terms, the resulting error is called truncation error (note that this is different than roundoff error as it would still exist even if we were able to use exact precision numbers with no rounding). Create a Python script that uses a loop to estimate \(\pi\) using the above formula for \(n = 0, 1, 2, \ldots, 20\) terms. For each value of \(n\), compute the relative percent error. Plot the truncation percent error on the y-axis.
The equations for projectile motion in two-dimensions (neglecting air resistance) are:
\[s_x = V\cos(\theta) t\\ s_y = V\sin(\theta) t - \frac{g}{2}t^2\]where \(s_x\) and \(s_y\) are the x and y position of the projectile (relative to your starting position), \(V\) is the magnitude of the initial velocity, \(t\) is time, and \(g\) is the acceleration of gravity. Let’s say you fire a projectile at 100 m/s and are aiming at a target 400 meters in \(x\) and 50 meters up in \(y\). Determine the angle that you should fire the projectile at. Hint: combine these equations into one equation by solving for \(t\) then plugging into the other, then you will have a one-dimensional residual function.
Write your own bisection method to solve this. A skeleton function is provided here. Also solve this problem with scipy (see scipy.optimize.root_scalar
) to check your answer. Note that there are two possible answers (a low angle and a high angle). You should report the low angle.