Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

4. State-Space Form & Numerical Integration

ISAE-SUPAERO

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"] = True

4.1 From second-order to first-order: the state vector

Start from mx¨+cx˙+kx=Fm\ddot x+c\dot x+kx=F and define the state vector x(t)=[x(t)    x˙(t)]T\mathbf{x}(t) = [x(t)\;\; \dot x(t)]^T (note the bold, capital x\mathbf{x} for the state, vs. the plain xx for the displacement). Adding the trivial identity x˙=x˙\dot x=\dot x:

{x˙=x˙x¨=−kmx−cmx˙+Fm\begin{cases} \dot x = \dot x \\ \ddot x = -\dfrac{k}{m}x-\dfrac{c}{m}\dot x+\dfrac{F}{m} \end{cases}

Exercise (handwritten) — identify A and B

Write this in compact form x˙=Ax+BF\dot{\mathbf x}=\mathbf{A}\mathbf{x}+\mathbf{B}F:

[x˙x¨]=[01−k/m−c/m]⏟A[xx˙]+[01/m]⏟BF\begin{bmatrix}\dot x\\\ddot x\end{bmatrix} = \underbrace{\begin{bmatrix}0&1\\-k/m&-c/m\end{bmatrix}}_{\mathbf A} \begin{bmatrix}x\\\dot x\end{bmatrix} + \underbrace{\begin{bmatrix}0\\1/m\end{bmatrix}}_{\mathbf B} F
State space block diagram

If we want to observe the position only, the output equation y=Cx+DF\mathbf y=\mathbf{Cx}+\mathbf{D}F uses C=[1    0]\mathbf C=[1\;\;0], D=[0]\mathbf D=[0].

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 x0=1,x˙0=0x_0=1,\dot x_0=0.

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()
<Figure size 700x420 with 1 Axes>

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 x˙=Ax\dot{\mathbf x}=\mathbf{A}\mathbf x 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])))
<Figure size 700x420 with 1 Axes>
Max abs difference: 9.435979775318515e-10

4.4 Exercise “EX3c” — forced vibration via numerical integration

When the right-hand side is not null, mx¨+cx˙+kx=F0cos⁡ωtm\ddot x+c\dot x+kx=F_0\cos\omega t. In state form:

x˙=Ax+f(t),f(t)=[0f0cos⁡ωt]\dot{\mathbf x} = \mathbf A\mathbf x + \mathbf f(t), \qquad \mathbf f(t) = \begin{bmatrix}0\\f_0\cos\omega t\end{bmatrix}

Numerically compute the forced vibration for m=100, k=1000, c=25m=100,\,k=1000,\,c=25, sinusoidal input at ω=2.5\omega=2.5,rad/s, F0=1000F_0=1000,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()
<Figure size 700x420 with 1 Axes>

4.5 Homework 3 — a rectangular force pulse

🟦 Homework. A common excitation is a constant force applied for a short time and then removed:

mx¨+cx˙+kx=F0[1−H(t−t1)]m\ddot x+c\dot x+kx = F_0\left[1-H(t-t_1)\right]

where HH 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 +F0+F_0 starting at t=0t=0 and a step of −F0-F_0 starting at t=t1t=t_1 (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)))
<Figure size 700x420 with 1 Axes>
Max abs difference: 3.999301466184324e-05