Arrays and plots
English translation
Learning objectives
Misinterpreting array axes or time intervals can give a physically incorrect meaning even when the calculation runs.
Array axes and two kinds of multiplication
Starting conditions
, .
Procedure
- Before execution, write down A's sums and means by axis and the shapes of A*A and A@B.
- Calculate using NumPy and check the (0,1) element of A@B by hand.
- Record the result array and shape for each axis and operation in a table.
Results to retain
- A table with at least 6 rows, including axis aggregation and the two kinds of multiplication.
import numpy as np
A=np.array([[1,2,3],[4,5,6]])
B=np.array([[2,0],[1,3],[0,1]])
print(A@B)
# TODO: Complete the axis aggregations and predicted-shape table.
Record results and self-check
Operation:
Expected shape:
Actual shape:
Value and meaning:
Points to check
- Do the inner dimensions match for matrix multiplication?
- Did you check (A@B)[0,1] rather than the meaning of A@B[0,1]?
Time series axes and units
Starting conditions
Use s and V.
Procedure
- Check the number of elements in the voltage and time arrays, and draw a line plot.
- Use
np.diff(V)/np.diff(t)to calculate interval slopes and plot them at interval midpoints. - Calculate the first interval's slope by hand and compare with the code.
Results to retain
- Voltage and slope plots, and a 4-row table.
import numpy as np
import matplotlib.pyplot as plt
t=np.array([0,.1,.2,.3,.4]);v=np.array([0,.8,1.3,1.6,1.8])
plt.plot(t,v,'o-');plt.xlabel('Time [s]');plt.ylabel('Voltage [V]');plt.grid()
# TODO: Calculate the slope and midpoint of each interval.
Record results and self-check
Interval [s]:
ΔV [V]:
Δt [s]:
Slope [V/s]:
Points to check
- Did you handle the difference in length between sample values and interval values?
- Did you label the axes with units as well as numbers?
Noise and smoothing
Starting conditions
With seed=7, create 101 points over s and add noise with a standard deviation of .2 to .
Procedure
- Apply
np.convolve(...,mode="same")to the same noisy signal using uniform kernels of lengths 3 and 9. - Plot the original signal, noisy signal, and two filter outputs on the same time axis. Calculate both RMSE values from the difference between the original signal and filter output over the common interval
[4:-4], excluding the four samples at each end. - Record kernel length, comparison sample count, and RMSE in two rows. Use the plots and RMSE to distinguish apparent smoothness from closeness to the original signal.
Results to retain
- Plots of the original signal, noisy signal, and two filter outputs, and an RMSE table for 2 conditions.
rng=np.random.default_rng(7);t=np.linspace(0,2,101)
truth=np.sin(2*np.pi*t);observed=truth+rng.normal(0,.2,len(t))
# TODO: Compare moving averages using np.convolve and the common interior interval.
Record results and self-check
Window:
Comparison interval:
RMSE:
Observed changes:
Points to check
- Did you use the same boundary handling and comparison interval?
- Did you avoid generating new noise for each condition?