due 9/12/2024 before midnight via Learning Suite 25 possible points
1.1 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)?
Create a function called patriot()
that returns your answer in meters.
1.2 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.
1.3 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 scalars and theta
is 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.
Now create the same two functions but for numpy array inputs. Let’s call them polar2cart_np(r, theta)
and cart2polar_np(x, y)
. Again, check by converting then converting back an array of inputs.
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)
.
Note: Creating separate names with _np
is not really desirable. In other programming languages you can overload a function name with different types, but that isn’t really supported in Python. The usual approach in numpy is to have one function name, but add checks on whether the inputs are scalars or arrays then proceed accordingly. Not required, but if interested, see if you can make that work.
1.4
a) Create a function avg(x)
that takes in a numpy array x
and returns the average. Note that np.mean
does this already, but we’re going to write our own for this exercise.
b) Create a new function mavg(x, c)
that takes in a constant c
that multiplies against the average. Make the default for c = 1 if the user doesn’t enter a value.
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: 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)
.