One infamous incident involving roundoff error led to the death of 28 Americans. A patriot missile defense system, used during the Gulf War, was designed to track and intercept incoming scud missiles. The potential for a roundoff error had been identified, but the software patch arrived the day after the incident (the full report is available at GAO/IMTEC-92-26).
When the computer that controlled the defense system was booted up, a clock kept track of time elapsed by incrementing an integer every tenth of a second. But the value for a tenth of a second was stored as a 24 bit binary number:
\[0.00011001100110011001100 \text{ (base 2)}\]Because this number is not exactly 0.1, error would accumulate the longer the computer was on. In this case the computer had been on for over 100 hours (much longer than they originally anticipated).
Assuming the computer was on for 100 hours, and the missiles had a speed of 1670 m/s (near Mach 5), what would be the distance error in the predicted missile location due the internal time error (or in other words how far would the missile travel during the time offset)?
Report your answer in meters.
In aerospace applications we need a model of atmospheric properties. The International Standard Atmosphere provides a common reference for temperature and pressure as a function of altitude. The temperature varies in altitude (where \(T\) is in Kelvin and \(h\) is in km) as (we’ll only go up to 47 km for brevity):
\[T(h) = \begin{cases} 288.15 - 6.5 h & 0 < h < 11 \text{ km} \\ 216.65 & 11 < h < 20 \text{ km} \\ 216.65 + (h - 20) & 20 < h < 32 \text{ km} \\ 228.65 + 2.8 (h - 32) & 32 < h < 47 \text{ km} \\ \end{cases}\]Plot the temperature as a function of altitude. Normally we put the dependent variable on the \(y\)-axis, but in this case it usually makes interpretation easier to plot altitude on the \(y\) axis (since it is vertical). Be sure to add appropriate labels and units.
In robotics applications we frequently want to convert between coordinate systems. Make a function polar2cart(r, theta) that converts from polar coordinates to Cartesian where the inputs r and theta are numpy arrays and theta is a numpy array in degrees. Then create the inverse function cart2polar(x, y) (where theta is also returned in degrees). Check your results by converting from polar to Cartesian then back again.
Hint: converting from polar to Cartesian is straightforward, but the opposite is a bit trickier. This is because atan(z) takes in only one input z, which represents a ratio z = y/x. So if z is positive we don’t know if y and x are both positive (angles between 0 and 90 deg) or they are both negative (angles between 180 to 270 deg). The function atan assumes the former, which obviously won’t always be correct for many applications. Instead, you’ll want to use a two-argument version of atan: math.atan2(y, x) or np.arctan2(y, x).
In this exercise we will create a set of functions that are all related to averaging. This will give us more practice with functions, keyword arguments, reading files, and using functions as inputs. I’d recommend reusing functions where possible (for example many of the latter functions can call your avg function).
a) Create a function avg(x) that takes in a numpy array x and returns the average. The function np.mean does this already, but we’re just writing our own for practice working with numpy arrays.
b) Create a new function mavg(x, c) that takes in a constant c that multiplies against the average. Make the default c = 1 if the user doesn’t enter a value for c.
c) Create a new function wavg(x, w) that computes a weighted average:
Typically, the weights are chosen to sum to one (though that’s just a convenience for interpretation, it doesn’t affect the weighted average). So as a simple example, let’s say you took 3 classes and got an A (4.0), B (3.0), and a C (2.0). But the classes have a different number of credits so a simple average doesn’t make sense for your semester GPA. The credits were 3, 2, and 1 respectively giving a semester GPA (weighted average) of 3.33.
d) Create a function tavg(filename) that can take in the name of a text file containing multiple columns of data, read the file, and return the average of each column of data.
Hint: In CS110, you learned to read files using methods that are well suited for files with text. However, as an engineer, you will more often deal with data files, and NumPy has convenient built-in functions for writing and reading data files (see for example: np.loadtxt).
e) Finally, create a function favg(x, f) that takes in an array x and a function f and return the average of f(x).