Open In Colab

Homework 1: NumPy Fundamentals#

Complete all exercises below. Show your work and include comments explaining your approach.

! curl -LsSf https://astral.sh/uv/install.sh | sh && \
  uv pip install -q --system "s26-06642 @ git+https://github.com/jkitchin/s26-06642.git"
from pycse.colab import pdf
downloading uv 0.10.3 x86_64-unknown-linux-gnu
no checksums to verify
installing to /home/runner/.local/bin
  uv
  uvx
everything's installed!
import numpy as np
import matplotlib.pyplot as plt

Problem 1: Array Creation and Basic Operations#

You have reactor temperature data collected every minute for 2 hours.

# Load temperature data (K) - collected every minute for 2 hours
import urllib.request

url = "https://raw.githubusercontent.com/jkitchin/s26-06642/main/dsmles/data/hw01_temperature_data.csv"
urllib.request.urlretrieve(url, "temperature_data.csv")

data = np.loadtxt("temperature_data.csv", delimiter=",", skiprows=1)
time_min = data[:, 0]
temperatures = data[:, 1]

print(f"Loaded {len(temperatures)} temperature measurements")
print(f"Time range: {time_min[0]} to {time_min[-1]} minutes")
Loaded 120 temperature measurements
Time range: 0.0 to 119.0 minutes

1a. Calculate the mean, standard deviation, minimum, and maximum temperature using NumPy functions.

# Your code here

1b. Use boolean indexing to find all time points (indices) where temperature exceeded 355 K. How many such points are there?

# Your code here

1c. Calculate the rate of temperature change (dT/dt) using np.diff(). What is the maximum heating rate? What is the maximum cooling rate?

# Your code here

Problem 2: Vectorized Calculations#

The Arrhenius equation describes reaction rate constants:

\[k = A \exp\left(-\frac{E_a}{RT}\right)\]

where:

  • A = 1.0 × 10⁸ s⁻¹ (pre-exponential factor)

  • Ea = 50,000 J/mol (activation energy)

  • R = 8.314 J/(mol·K) (gas constant)

  • T = temperature in Kelvin

2a. Create an array of temperatures from 300 K to 500 K in 20 K increments using np.arange().

# Your code here

2b. Calculate the rate constant k at each temperature using vectorized operations (no loops!).

# Your code here
A = 1.0e8  # s^-1
Ea = 50000  # J/mol
R = 8.314  # J/(mol·K)

2c. Plot k vs T. Use a logarithmic y-axis (plt.semilogy()) since k varies over several orders of magnitude.

# Your code here

2d. By what factor does the rate constant increase when going from 350 K to 400 K?

# Your code here

Problem 3: 2D Arrays and Broadcasting#

You have experimental data from a batch reactor stored in a 2D array where:

  • Rows represent different experiments

  • Columns are: Temperature (K), Pressure (atm), Conversion (%)

# Experimental data: each row is [Temperature, Pressure, Conversion]
np.random.seed(42)
data = np.array([
    [350, 1.0, 45],
    [350, 2.0, 52],
    [350, 3.0, 58],
    [400, 1.0, 62],
    [400, 2.0, 71],
    [400, 3.0, 78],
    [450, 1.0, 75],
    [450, 2.0, 83],
    [450, 3.0, 89]
])

print("Data shape:", data.shape)
print(data)
Data shape: (9, 3)
[[350.   1.  45.]
 [350.   2.  52.]
 [350.   3.  58.]
 [400.   1.  62.]
 [400.   2.  71.]
 [400.   3.  78.]
 [450.   1.  75.]
 [450.   2.  83.]
 [450.   3.  89.]]

3a. Extract the Temperature column (column 0) and the Conversion column (column 2) into separate arrays.

# Your code here

3b. Find all experiments where conversion exceeded 70%. Return the full rows for these experiments.

# Your code here

3c. Calculate the mean conversion for each temperature level (350, 400, 450 K).

Hint: Use boolean indexing to select rows for each temperature, then calculate mean of the conversion column.

# Your code here