Exercises

Problem 1

In a notebook cell, plot the function

\[ f(x) = e^{-x/3} \cos(2x) \]

over the interval \([0, 10]\), using enough points to produce a smooth curve. Style your plot as follows.

  • Set the \(x\) limits to \([0, 10]\).

  • Label both axes and add the title “Problem 1”.

Create the figure in a single cell ending with plt.show().

Reminder: The function np.exp() applies the exponential \(e^x\) to each entry of an array all at once.

Problem 2

The Bernstein basis polynomials are a family of polynomials with certain properties that are advantageous for use in applications such as computer graphics. The five Bernstein basis polynomials of degree four are the following:

\[\begin{split} \begin{aligned} b_{0,4}(x) &= (1 - x)^4, \\ b_{1,4}(x) &= 4x(1 - x)^3, \\ b_{2,4}(x) &= 6x^2(1 - x)^2, \\ b_{3,4}(x) &= 4x^3(1 - x), \\ b_{4,4}(x) &= x^4. \\ \end{aligned} \end{split}\]

Plot these five Bernstein basis polynomials over the interval \([0, 1]\), as well as their sum:

\[ B_4(x) = b_{0,4}(x) + b_{1,4}(x) + b_{2,4}(x) + b_{3,4}(x) + b_{4,4}(x). \]

Style your plot as follows.

  • Use a different color for each polynomial.

  • Use one line style for \(b_{0,4}(x), b_{1,4}(x), \ldots, b_{4,4}(x)\) and a different line style for the sum \(B_4(x)\).

  • Set the \(x\) limits to \([0, 1]\) and the \(y\) limits to \([-0.1, 1.75]\) to make room for a legend.

  • Label each polynomial and create a two-column legend in the upper right corner.

  • Label both axes and add the title “Problem 2”.

Create the figure in a single cell ending with plt.show().

Problem 3

The Squeeze Theorem says that if \(f(x) \le g(x) \le h(x)\) near \(a\), and if

\[ \lim_{x\to a} f(x) = \lim_{x\to a} h(x) = L, \]

then

\[ \lim_{x\to a} g(x) = L. \]

Visualize the Squeeze Theorem for the functions

\[ f(x) = -x^2, \qquad g(x) = x^2\sin(10x), \qquad h(x) = x^2 \]

and \(a = L = 0\) over the interval \([-1,1]\).

  • Plot \(g(x)\) as a solid line.

  • Shade in the region between \(f(x)\) and \(h(x)\).

  • Set the \(x\) limits to \([-1, 1]\).

  • Label the \(x\)-axis and add the title “Problem 3”.

Create the figure in a single cell ending with plt.show().

Problem 4

The file econ_data.npz contains three 1D NumPy arrays representing the annual gross domestic product (GDP) in trillions of US dollars, average life expectancy in years, and total population for 48 fictitious countries. Upload the file to Google Colab, then load it into Python with the following code.

econ = np.load("econ_data.npz")

gdp = econ["gross_domestic_product"]
life = econ["life_expectancy"]
pop = econ["population"]

# Sanity check that all three arrays have the same size.
print(len(gdp))
print(len(life))
print(len(pop))
48
48
48

Make a scatter plot exploring this dataset.

  • Plot life expectancy against GDP and label the axes accordingly.

  • Encode a third variable by setting the marker size to the population, divided by the largest population, then multiplied by 1000.

  • Choose alpha so that overlapping markers remain visible.

  • Add the title “Problem 4”.

Create the figure in a single cell ending with plt.show().

Finally, in a Markdown cell below the figure, briefly describe (two or three sentences) what the figure shows. Is there a visible trend between GDP and life expectancy? Do any countries stand out as unusual? What other insights does the figure show?

Problem 5

The file mystery_data.npy contains a 1D array of measurements. Upload it to Google Colab and load it with np.load():

data = np.load("mystery_data.npy")

Explore this dataset by drawing several histograms using different values for bins. Choose a value for bins that, in your judgment, best reveals the structure of the data. Produce a histogram using that number of bins and add the title “Problem 5”. Create the figure in a single cell ending with plt.show().

In a Markdown cell below the figure, briefly (in two or three sentences) describe what the figure shows. What are the key features of this dataset? Can using too few or too many bins be misleading?

Problem 6

The file weather_data.npz contains 90 days of fictional weather data for one city, including the day recorded, the high temperature, the amount of precipitation (rain) in inches, and the average relative humidity as a percentage. Upload the data to Google Colab and load it into Python with the following code.

weather = np.load("weather_data.npz")

day = weather["day"]
temperature = weather["high_temperature"]
precipitation = weather["precipitation"]
humidity = weather["humidity"]

Use one or more plots to answer the following questions:

  • How often does it rain? Were there any dry spells over the 90-day period?

  • How do temperature, humidity, and precipitation trend over the 90-day period?

  • Is precipitation more strongly associated with temperature or humidity? How?

Clearly label each plot and write an analysis of the data based on your visualizations.