Solved reference notebook. See 04_StateSpace_NumericalIntegration_Exercises.ipynb for the
blanked-out version.
import numpy as np
import matplotlib.pyplot as plt
import control
from scipy.integrate import solve_ivp
plt.rcParams["figure.figsize"] = (7, 4.2)
plt.rcParams["axes.grid"] = True4.1 From second-order to first-order: the state vector¶
Start from and define the state vector (note the bold, capital for the state, vs. the plain for the displacement). Adding the trivial identity :
Exercise (handwritten) — identify A and B¶
Write this in compact form :

If we want to observe the position only, the output equation uses , .
4.2 Exercise “EX3” — free vibration from a state-space model¶
Numerically compute the free vibration of a mass-spring-damper system by defining a state-space
object and using its initial-condition response (ss + initial in MATLAB;
control.ss + control.initial_response in Python), with .
m, c, k = 1.0, 0.1, 1.0
A = np.array([[0, 1], [-k / m, -c / m]])
B = np.array([[0], [1 / m]])
C = np.array([[1, 0]])
D = np.array([[0]])
sys = control.ss(A, B, C, D)
T = np.linspace(0, 50, 1000)
x0 = [1, 0]
t_out, y_out = control.initial_response(sys, T=T, X0=x0)
fig, ax = plt.subplots()
ax.plot(t_out, y_out)
ax.set_xlabel("Time [s]")
ax.set_ylabel("x(t)")
ax.set_title(r"Initial-condition response of the state-space model, $x_0=[1,0]^T$")
plt.show()
4.3 Exercise “EX3b” — the same, via direct numerical integration (ode45/solve_ivp)¶
scipy.integrate.solve_ivp is Python’s equivalent of MATLAB’s ode45 (both are adaptive
Runge-Kutta solvers). We integrate directly.
def sdof_state(t, x, m, c, k):
A = np.array([[0, 1], [-k / m, -c / m]])
return A @ x
sol = solve_ivp(sdof_state, [0, 50], x0, args=(m, c, k), t_eval=T, rtol=1e-9, atol=1e-11)
fig, ax = plt.subplots()
ax.plot(t_out, y_out, label="control.initial_response")
ax.plot(sol.t, sol.y[0], "--", label="solve_ivp")
ax.set_xlabel("Time [s]")
ax.set_ylabel("x(t)")
ax.legend()
ax.set_title("Cross-check: state-space vs direct ODE integration")
plt.show()
print("Max abs difference:", np.max(np.abs(y_out - sol.y[0])))
Max abs difference: 9.435979775318515e-10
4.4 Exercise “EX3c” — forced vibration via numerical integration¶
When the right-hand side is not null, . In state form:
Numerically compute the forced vibration for , sinusoidal input at
,rad/s, ,N, both via control.forced_response (the lsim equivalent)
and via solve_ivp on the raw state equation.
m, k, c = 100.0, 1000.0, 25.0
w, F0 = 2.5, 1000.0
A = np.array([[0, 1], [-k / m, -c / m]])
B = np.array([[0], [1 / m]])
C = np.array([[1, 0]])
D = np.array([[0]])
sys = control.ss(A, B, C, D)
t = np.arange(0, 10 + 1e-9, 0.01)
u = F0 * np.cos(w * t)
t_lsim, x_lsim = control.forced_response(sys, T=t, U=u, X0=[0, 0])
def sdof_forced_state(t, x, m, c, k, F0, w):
A = np.array([[0, 1], [-k / m, -c / m]])
f = np.array([0, F0 * np.cos(w * t) / m])
return A @ x + f
sol = solve_ivp(sdof_forced_state, [0, 5], [0, 0], args=(m, c, k, F0, w), rtol=1e-10, atol=1e-12)
fig, ax = plt.subplots()
ax.plot(t_lsim, x_lsim, label="control.forced_response (state-space, lsim-equivalent)")
ax.plot(sol.t, sol.y[0], "k*", label="solve_ivp on the raw ODE", markersize=4)
ax.set_xlabel("Time")
ax.set_ylabel("Amplitude")
ax.legend()
ax.set_title("Forced response: state-space simulation vs direct ODE integration")
plt.show()
4.5 Homework 3 — a rectangular force pulse¶
🟦 Homework. A common excitation is a constant force applied for a short time and then removed:
where is the Heaviside step function. Solve this numerically (Python’s
numpy.heaviside/a simple if, is the equivalent of MATLAB’s stepfun), and cross-check by
superposing two step responses: a step of starting at and a step of
starting at (linear superposition, exactly as in Chapter 3).
m, c, k = 1.0, 0.2, 4.0
F0, t1 = 5.0, 6.0
t = np.linspace(0, 30, 3000)
def pulse_ode(t, x, m, c, k, F0, t1):
A = np.array([[0, 1], [-k / m, -c / m]])
F = F0 * (1 - np.heaviside(t - t1, 1.0))
f = np.array([0, F / m])
return A @ x + f
sol = solve_ivp(pulse_ode, [0, 30], [0, 0], args=(m, c, k, F0, t1), t_eval=t,
rtol=1e-10, atol=1e-12, max_step=0.01)
# Superposition of two step responses
A = np.array([[0, 1], [-k / m, -c / m]])
B = np.array([[0], [1 / m]])
C = np.array([[1, 0]])
D = np.array([[0]])
sys = control.ss(A, B, C, D)
t_step, x_step = control.step_response(sys * F0, T=t)
x_super = x_step.copy()
mask = t >= t1
x_super[mask] -= np.interp(t[mask] - t1, t_step, x_step)
fig, ax = plt.subplots()
ax.plot(sol.t, sol.y[0], label="Direct numerical integration")
ax.plot(t, x_super, "--", label="Superposition of two shifted step responses")
ax.axvline(t1, color="gray", ls=":", label=r"$t_1$ (force removed)")
ax.set_xlabel("Time [s]")
ax.set_ylabel("x(t)")
ax.legend()
ax.set_title("Rectangular force pulse: two equivalent solution methods")
plt.show()
print("Max abs difference:", np.max(np.abs(sol.y[0] - x_super)))
Max abs difference: 3.999301466184324e-05