Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.A toolbox of proven numerical recipes
Solving an equation, finding the lowest point of a function, adding up the area under a curve, removing noise from a signal, testing whether two measurements differ: engineers need these constantly, and writing them correctly is hard. SciPy is a carefully tested toolbox of such recipes, many built on Fortran and C libraries refined over decades. Your job becomes choosing the right tool, calling it correctly, and checking the answer makes sense.
Orbit propagation (solve_ivp), attitude estimation (linalg), trajectory and controller tuning (optimize), sensor filtering (signal), test-campaign statistics (stats): SciPy is the everyday engine of aerospace and robotics analysis in Python, and many flight-software prototypes start as SciPy scripts.
- 1. Which SciPy module solves differential equations?
- 2. Which module fits a model curve to data?
- 3. The big-swing pendulum's period is…
Level 1: Beginner
★★☆☆☆ Basic⏱ 25 minSecondary-school mathematics: arithmetic and elementary algebra.The main modules
| module | typical calls |
|---|---|
scipy.linalg |
solve, lu_factor/lu_solve, cho_factor, lstsq, svd, expm, eigh |
scipy.optimize |
brentq, root, minimize, least_squares, curve_fit, linprog |
scipy.integrate |
quad, solve_ivp (RK45, DOP853, Radau, BDF, LSODA), events |
scipy.signal |
butter, sosfilt, sosfiltfilt, welch, spectrogram, find_peaks |
scipy.stats |
distributions (norm, t, chi2), ttest_ind, pearsonr, bootstrap |
scipy.special |
ellipk, gamma, erf, Bessel functions |
scipy.spatial |
cKDTree, Rotation (quaternions and Euler angles) |
A first ODE
from scipy.integrate import solve_ivp
sol = solve_ivp(lambda t, y: [y[1], -9.81 * np.sin(y[0])], (0, 10), [1.0, 0.0],
rtol=1e-9, atol=1e-12, dense_output=True)
sol.t, sol.y, sol.sol(3.2) # time points, states, interpolant- 1. For a stiff ODE (fast and slow dynamics) use…
- 2. To find E with E − e sin E = M on a known bracket, use…
- 3. scipy.spatial.transform.Rotation handles…
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 40 minUndergraduate students and practising engineers.Choosing and checking
- Linear systems: never invert, use
solveor a factorization. Reuselu_factoracross many right-hand sides. Prefercho_factorfor SPD matrices. - Root finding: bracketing (
brentq) is guaranteed when you have a sign change; Newton-type (root,newton) is faster but needs a good start. - Minimization: gradients help (
jac=). Use BFGS/L-BFGS-B for smooth problems, Nelder–Mead for rough ones,least_squaresfor residual problems with bounds. - ODEs: set
rtol/atoldeliberately; check by halving the tolerances; use events for crossings (apsis, ground impact). - Filters: design as second-order sections (
output="sos") for numerical stability. Offline,sosfiltfiltgives zero phase; in real time only causal filtering exists, and it lags. - Always verify: residuals, conservation laws, known special cases.
- 1. Why design filters as second-order sections?
- 2. filtfilt-style filtering is…
- 3. To check an ODE solution's accuracy, you can…
Level 3: Advanced
★★★★☆ Advanced⏱ 35 minGraduate level, with full derivations and proofs.The pendulum period exactly
Energy conservation gives , so . The substitution with turns this into , with . Note that SciPy’s ellipk(m) takes the parameter , a classic gotcha. As , K diverges logarithmically.
Why causal filters lag
A causal low-pass filter’s phase response is negative, delaying each frequency by (group delay). For a Butterworth filter of fixed order the low-frequency group delay scales like (and grows with the order): in exercise 2 the order-4 filter delays by 205 ms at 2 Hz and 80 ms at 5 Hz, a ratio of 2.6 for a 2.5× change in cutoff. Forward–backward filtering applies and then , giving with zero phase, at the cost of needing the whole signal.
Welch’s method
Average periodograms of overlapping, windowed segments: the variance drops by roughly the number of segments, at the cost of frequency resolution ().
- 1. scipy.special.ellipk(m) expects…
- 2. Lower low-pass cutoff → group delay…
- 3. Welch averaging trades…
Level 4: Expert
★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.SciPy, measured
Exercise 1. Event-based periods from solve_ivp (rtol 10⁻¹⁰) match the elliptic formula to better than 10⁻⁸ s. At θ0 = 3 rad the period is 5.158 s against 2.006 s for the small-angle formula (2.6× longer). brentq solves Kepler’s equation to 10⁻¹² even at e = 0.95. curve_fit recovers A = 2.00, γ = 0.300 and ω = 3.097 from noisy data (true 2, 0.3, 3.1).
Exercise 2: filtering a 0.5 Hz attitude signal with 37 Hz vibration and noise (raw SNR 4.7 dB; Welch finds the vibration at 36.9 Hz). Butterworth, order 4:
| cutoff | zero-phase SNR | causal SNR | causal delay |
|---|---|---|---|
| 2 Hz | 22.2 dB | 3.9 dB | 205 ms |
| 5 Hz | 20.8 dB | 11.3 dB | 80 ms |
| 20 Hz | 14.9 dB | 14.0 dB | 20 ms |
| 50 Hz | 6.4 dB | 6.2 dB | 5 ms |
Offline, the lowest cutoff is best. In real time, the 2 Hz filter’s 205 ms lag makes it worse than the raw signal. The best causal choice here is a 20 Hz cutoff: the classic noise-versus-lag trade-off of every control loop.
SciPy in AI, robotics and aerospace
- Orbit and attitude propagation with solve_ivp and events (eclipse entry, apoapsis, impact).
- Estimation: least_squares for orbit determination and calibration; linalg for Kalman filters.
- Signal conditioning of IMU, vibration and audio data; causal filters in loops, zero-phase in post-processing.
- Statistics for test campaigns, sensor characterization and Monte Carlo dispersion analysis.
References
- P. Virtanen et al. (2020). “SciPy 1.0: fundamental algorithms for scientific computing in Python.” Nature Methods, 17.
- SciPy documentation (docs.scipy.org):
integrate.solve_ivp,optimize,signal,special.ellipk. - E. Hairer, S. P. Nørsett, G. Wanner (1993). Solving Ordinary Differential Equations I. Springer.
- A. V. Oppenheim, R. W. Schafer (2010). Discrete-Time Signal Processing, 3rd ed. Pearson.
- P. D. Welch (1967). “The use of fast Fourier transform for the estimation of power spectra.” IEEE Transactions on Audio and Electroacoustics, 15(2).