TExcelTAD
Home › Control Systems & Automation › State-Space Models from Differential Equations

State-Space Models from Differential Equations

The speed loop of the small DC motor works in simulation, but on the bench the supply's current limit trips at every start. The transfer function from voltage to speed, Ω(s)V(s)\frac{\Omega(s)}{V(s)}, cannot warn you: the armature current is inside it, hidden. A second problem: a test rig has two heated zones on one aluminium plate, each with its own heater and sensor, and heating one zone warms the other. A single-input, single-output transfer function cannot describe two inputs that both move two outputs. Both problems are solved by the state-space model, which keeps every internal energy store visible and handles many inputs and outputs with the same matrix equations. It is also the starting point for the rest of this section: controllability and observability, pole placement, observers and LQR.

State, input, output

The state x(t)x(t) is the smallest set of variables whose present values, together with the future input, determine the future of the system. For physical systems a safe choice is one variable per independent energy store: capacitor voltages, inductor currents, positions and velocities of masses, temperatures of heat capacities, levels of tanks. A linear time-invariant system with nn states, mm inputs and pp outputs is

x˙=Ax+Bu,y=Cx+Du\dot{x} = A x + B u, \qquad y = C x + D u

with AA (n×nn \times n, the dynamics), BB (n×mn \times m, how inputs push the states), CC (p×np \times n, what the sensors see) and DD (p×mp \times m, direct feedthrough, usually zero for physical plants).

u B + + dx/dt 1/s integrator x C + + y A D

The link to the transfer function comes from the Laplace transform with zero initial state, sX=AX+BUsX = AX + BU:

G(s)=C (sI−A)−1B+D,poles of G=eigenvalues of AG(s) = C\,(sI - A)^{-1} B + D, \qquad \text{poles of } G = \text{eigenvalues of } A

(except for eigenvalues that cancel, which the next lesson explains). The free response is x(t)=eAtx(0)x(t) = e^{At}x(0), the matrix exponential of Linear Algebra and Differential Equations; you will rarely compute it by hand, because simulation does it for you.

From differential equations to matrices

Physical states. Write one first-order equation per energy store and read off the matrices. The motor has two stores (armature inductance, rotor inertia), so two states, x=[ ia, ω ]Tx = [\,i_a,\ \omega\,]^T:

Ladiadt=v−Raia−k ωarmature loop with back-EMF kωJdωdt=k ia−b ωtorque kia against friction\begin{aligned} L_a \frac{di_a}{dt} &= v - R_a i_a - k\,\omega && \text{armature loop with back-EMF } k\omega \\ J \frac{d\omega}{dt} &= k\,i_a - b\,\omega && \text{torque } k i_a \text{ against friction} \end{aligned}
A=[−RaLa−kLakJ−bJ],B=[1La0],C=[01],D=0A = \begin{bmatrix} -\frac{R_a}{L_a} & -\frac{k}{L_a} \\ \frac{k}{J} & -\frac{b}{J} \end{bmatrix}, \quad B = \begin{bmatrix} \frac{1}{L_a} \\ 0 \end{bmatrix}, \quad C = \begin{bmatrix} 0 & 1 \end{bmatrix}, \quad D = 0

The derivation of these two equations is in Modelling a DC Motor.

Phase variables. An nn-th order ODE y(n)+an−1y(n−1)+⋯+a0y=b0uy^{(n)} + a_{n-1}y^{(n-1)} + \dots + a_0 y = b_0 u becomes a state model with x1=yx_1 = y, x2=y˙x_2 = \dot{y}, …: the controllable canonical form, with ones on the superdiagonal of AA and the coefficients −a0,…,−an−1-a_0, \dots, -a_{n-1} in its last row. ct.tf2ss produces a canonical form from a transfer function, but its states have no physical meaning, so prefer physical states whenever you can.

A state model is not unique: any invertible change of variables x=Pzx = Pz gives z˙=P−1AP z+P−1B u\dot{z} = P^{-1}AP\,z + P^{-1}B\,u with the same transfer function and the same eigenvalues.

Worked example: the motor as a state model

Given: Ra=2 ΩR_a = 2\ \Omega, La=1 mHL_a = 1\ \text{mH}, k=0.02 V⋅s/radk = 0.02\ \text{V} \cdot \text{s/rad}, J=2×10−6 kg⋅m2J = 2 \times 10^{-6}\ \text{kg} \cdot \text{m}^2, b=1×10−6 N⋅m⋅sb = 1 \times 10^{-6}\ \text{N} \cdot \text{m} \cdot \text{s}, a 12 V12\ \text{V} step.

Find: AA and BB, the poles, the final speed and the peak current.

Solve:

  • A=[−2000−2010 000−0.5] s−1A = \begin{bmatrix} -2000 & -20 \\ 10\,000 & -0.5 \end{bmatrix}\ \text{s}^{-1} (units mixed per entry), B=[ 1000, 0 ]T A/(V⋅s)B = [\,1000,\ 0\,]^T\ \text{A/(V} \cdot \text{s)}.
  • Eigenvalues: λ2+2000.5λ+(1000+200 000)=0\lambda^2 + 2000.5\lambda + (1000 + 200\,000) = 0, so λ=−106.1\lambda = -106.1 and −1894.4 s−1-1894.4\ \text{s}^{-1}: a slow mechanical mode (9.4 ms9.4\ \text{ms}) and a fast electrical mode (0.53 ms0.53\ \text{ms}).
  • Steady state (x˙=0\dot{x} = 0): ω=kVRab+k2=0.02×122×10−6+4×10−4=597 rad/s\omega = \frac{k V}{R_a b + k^2} = \frac{0.02 \times 12}{2 \times 10^{-6} + 4 \times 10^{-4}} = 597\ \text{rad/s} (5700 rpm5700\ \text{rpm}), with ia=bω/k=0.030 Ai_a = b\omega/k = 0.030\ \text{A}.
  • At the first instant the rotor is still, so there is no back-EMF: the current heads for V/Ra=6 AV/R_a = 6\ \text{A} and is only cut off by the rising speed. The simulation gives the peak.

Check: the transfer function from C(sI−A)−1BC(sI - A)^{-1}B must have the DC gain 597/12=49.75 rad/(s⋅V)597/12 = 49.75\ \text{rad/(s} \cdot \text{V)}.

python · try it yourself
import numpy as np
import control as ct

Ra, La, k, J, b = 2.0, 1e-3, 0.02, 2e-6, 1e-6
A = np.array([[-Ra / La, -k / La], [k / J, -b / J]])
B = np.array([[1 / La], [0.0]])
C = np.eye(2)                                  # measure both states: current and speed
motor = ct.ss(A, B, C, 0)
print("eigenvalues of A (1/s):", np.round(np.linalg.eigvals(A).real, 1))

G = ct.ss2tf(ct.ss(A, B, [[0, 1]], 0))         # speed output only
num, den = G.num[0][0], G.den[0][0]
print("G(s) = " + f"{num[-1]:.4g} / (s^2 + {den[1]:.1f} s + {den[2]:.4g})")
print(f"DC gain {ct.dcgain(G):.2f} rad/s per V")

t = np.linspace(0, 0.1, 10001)
resp = ct.forced_response(motor, T=t, U=12.0 * np.ones_like(t))
i_a, w = resp.outputs
kpk = int(np.argmax(i_a))
print(f"12 V step: peak current {i_a[kpk]:.2f} A at {1e3 * t[kpk]:.2f} ms, "
      f"final current {i_a[-1]:.3f} A, final speed {w[-1]:.1f} rad/s")
eigenvalues of A (1/s): [-1894.4  -106.1]
G(s) = 1e+07 / (s^2 + 2000.5 s + 2.01e+05)
DC gain 49.75 rad/s per V
12 V step: peak current 5.34 A at 1.61 ms, final current 0.030 A, final speed 597.0 rad/s

The current peak of about 5.3 A5.3\ \text{A} is what tripped the supply. With the state model you can now put a limit on the current state itself: a current loop inside the speed loop (cascade control), or a state-feedback design that weighs current (LQR).

A multivariable system: two heated zones

The roadmap asks you to simulate a multivariable system. Take two zones on one plate, each with heat capacity Ch=75 J/KC_h = 75\ \text{J/K}, thermal resistance R=8 K/WR = 8\ \text{K/W} to the room and R12=4 K/WR_{12} = 4\ \text{K/W} between them. With the temperature rises T1T_1, T2T_2 as states and the heater powers P1P_1, P2P_2 as inputs:

ChT˙1=P1−T1R−T1−T2R12,ChT˙2=P2−T2R−T2−T1R12C_h \dot{T}_1 = P_1 - \frac{T_1}{R} - \frac{T_1 - T_2}{R_{12}}, \qquad C_h \dot{T}_2 = P_2 - \frac{T_2}{R} - \frac{T_2 - T_1}{R_{12}}

The steady-state gain matrix −CA−1B-CA^{-1}B says how much each heater moves each temperature.

python · try it yourself
import numpy as np
import control as ct

Ch, R, R12 = 75.0, 8.0, 4.0
g, g12 = 1 / R, 1 / R12
A = np.array([[-(g + g12), g12], [g12, -(g + g12)]]) / Ch
B = np.eye(2) / Ch
plate = ct.ss(A, B, np.eye(2), 0)
K0 = -np.eye(2) @ np.linalg.solve(A, B)
print("DC gain matrix (K per W):")
print(np.round(K0, 3))
print("time constants (s):", np.round(np.sort(-1 / np.linalg.eigvals(A).real), 1))

t = np.linspace(0, 1800, 1801)
U = np.vstack([5.0 * np.ones_like(t), np.zeros_like(t)])    # 5 W in zone 1 only
y = ct.forced_response(plate, T=t, U=U).outputs
for ts in (60, 300, 1800):
    print(f"t = {ts:4d} s: T1 = {y[0, ts]:5.2f} K, T2 = {y[1, ts]:5.2f} K")
DC gain matrix (K per W):
[[4.8 3.2]
 [3.2 4.8]]
time constants (s): [120. 600.]
t =   60 s: T1 =  3.48 K, T2 =  0.33 K
t =  300 s: T1 = 11.54 K, T2 =  4.20 K
t = 1800 s: T1 = 23.00 K, T2 = 15.00 K

Five watts in zone 1 alone lift zone 2 by more than half as much as zone 1 in the end: the off-diagonal gains are large, so two independent single-loop controllers will fight each other. The two time constants belong to two modes: both zones rising together (the slow mode, set by RR only) and the zones moving apart (the fast mode, which also uses the path R12R_{12}). Decoupling and multivariable design start from exactly this matrix picture.

Lab: a second-order circuit as a state model

A series RLC driven by a step has the states x=[ i, vC ]Tx = [\,i,\ v_C\,]^T and A=[−R/L−1/L1/C0]A = \begin{bmatrix} -R/L & -1/L \\ 1/C & 0 \end{bmatrix}. Its eigenvalues are −α±jωd-\alpha \pm j\omega_d with α=R/(2L)\alpha = R/(2L). With R=200 ΩR = 200\ \Omega, L=10 mHL = 10\ \text{mH}, C=100 nFC = 100\ \text{nF}: α=10 000 s−1\alpha = 10\,000\ \text{s}^{-1}, ωn=1/LC=31 623 rad/s\omega_n = 1/\sqrt{LC} = 31\,623\ \text{rad/s}, ωd=ωn2−α2=30 000 rad/s\omega_d = \sqrt{\omega_n^2 - \alpha^2} = 30\,000\ \text{rad/s}, ζ=0.316\zeta = 0.316, overshoot e−πζ/1−ζ2=35.1 %e^{-\pi\zeta/\sqrt{1 - \zeta^2}} = 35.1\,\%. The lab shows exactly these: α=10.00 ks−1\alpha = 10.00\ \text{ks}^{-1}, ωd=30.00 krad/s\omega_d = 30.00\ \text{krad/s}, overshoot 35.09 %35.09\,\%.

0.000.501.000200 us400 us600 ustvC (V)underdamped, zeta = 0.316peak +35.1 % at 105 usvC(t) after a step of V at t = 0envelopecritical R = 2 sqrt(L/C) = 632 Ohm

Raise RR to 632.5 Ω632.5\ \Omega and the two complex eigenvalues meet on the real axis (ζ=1\zeta = 1); above that they split into two real modes, like the motor's.

Guided lab: measure the motor's two modes

Hand calculation. From the worked example: mechanical time constant 9.4 ms9.4\ \text{ms}, electrical 0.53 ms0.53\ \text{ms}, no-load speed 597 rad/s597\ \text{rad/s} at 12 V12\ \text{V}, start current peak 5.3 A5.3\ \text{A}. With a supply limited to 1 A1\ \text{A}, the start is current-limited: the speed ramps at about k⋅1 A/J=10 000 rad/s2k \cdot 1\ \text{A}/J = 10\,000\ \text{rad/s}^2 until the back-EMF takes over.

Simulation. Change U in the first program to a 3 V3\ \text{V} step (peak current 1.3 A1.3\ \text{A}) and compare.

What you would measure. A small brushed motor (no load, shaft free) on a bench supply at 3 V3\ \text{V}, current limit 1.5 A1.5\ \text{A}; a 0.1 Ω0.1\ \Omega shunt in the return lead for the current, and an optical encoder or a second motor used as a tachogenerator for the speed. On a two-channel scope, the current jumps to roughly 1.2–1.4 A1.2\text{–}1.4\ \text{A} within a millisecond and decays with the mechanical time constant (expect 5–20 ms5\text{–}20\ \text{ms} for hobby motors: your JJ and kk differ from the example); the speed rises with the same time constant. Brush noise adds ±10 %\pm 10\,\% ripple on the current trace, so fit the time constant to an averaged trace and expect ±10 %\pm 10\,\% uncertainty.

Hazards and safe setup. Clamp the motor; nothing on the shaft that can fly off; keep fingers and hair away; set the current limit before connecting; a stalled motor heats quickly, so keep stall tests to a second or two. Low voltage only.

Pitfalls

  • Choosing derivatives of the input as states: if u˙\dot{u} appears, rewrite the model (or use a canonical form) so that BB multiplies uu only.
  • Forgetting that ct.tf2ss gives a non-physical realisation; you cannot put a current limit on a state that is not a current.
  • Mixed units in AA: each entry has the unit of (row state per second) per (column state). Check them; a wrong power of ten in LaL_a moves an eigenvalue by a factor of ten.
  • Reading a MIMO plant as independent loops when the off-diagonal gains are large.

Practice

  1. A mass m=2 kgm = 2\ \text{kg} on a spring ks=50 N/mk_s = 50\ \text{N/m} with damping c=4 N⋅s/mc = 4\ \text{N} \cdot \text{s/m} is pushed by a force FF. Write AA, BB, CC for x=[ p, v ]Tx = [\,p,\ v\,]^T and output position pp, and give the eigenvalues.
  2. For the plate, what are the steady temperature rises with P1=P2=5 WP_1 = P_2 = 5\ \text{W}?
  3. Why does the first column of the motor's step response (current) end near 0.03 A0.03\ \text{A} rather than zero?
Answer
  1. A=[01−25−2]A = \begin{bmatrix} 0 & 1 \\ -25 & -2 \end{bmatrix}, B=[ 0, 0.5 ]TB = [\,0,\ 0.5\,]^T, C=[ 1, 0 ]C = [\,1,\ 0\,]; λ2+2λ+25=0\lambda^2 + 2\lambda + 25 = 0 gives λ=−1±j4.90 s−1\lambda = -1 \pm j4.90\ \text{s}^{-1}.
  2. Symmetric heating: the coupling carries no heat, each zone sees only RR: T1=T2=5×8=40 KT_1 = T_2 = 5 \times 8 = 40\ \text{K} (the sum of the two columns of the gain matrix, 4.8+3.2=8 K/W4.8 + 3.2 = 8\ \text{K/W}, times 5 W5\ \text{W}).
  3. Friction: at constant speed the motor still needs a torque bω=kiab\omega = k i_a, so ia=bω/k=0.030 Ai_a = b\omega/k = 0.030\ \text{A}.

Test yourself

Which variables make a good state vector for a physical system?

  1. the inputs and their derivatives
  2. one variable per independent energy store (capacitor voltage, inductor current, position, velocity, temperature)
  3. the outputs only
  4. the coefficients of the transfer function

The poles of G(s)=C(sI−A)−1B+DG(s) = C(sI - A)^{-1}B + D are

  1. the eigenvalues of AA (apart from cancellations)
  2. the entries of BB
  3. the zeros of CC
  4. the eigenvalues of CTCC^T C

Why does a 12 V step draw about 5 A from the small motor even though it runs at 0.03 A?

  1. the inductance stores energy
  2. at standstill there is no back-EMF, so only Ra=2 ΩR_a = 2\ \Omega limits the current
  3. friction is highest at start
  4. the motor is unstable

In the two-zone plate, heating zone 1 alone with 5 W ends with

  1. only zone 1 warmer
  2. both zones warmer, zone 2 by a large fraction of zone 1's rise
  3. zone 2 cooler
  4. an unstable temperature