S9 · Lesson 50 · Control lab
Writing three tools every control engineer uses, a step-response simulator for any proper transfer function, a calculator for crossover frequencies and stability margins, and Ackermann's pole-placement formula, each checked against results from the lessons
Control toolboxes hide three routines that you've used by hand all course: simulate a transfer function's step response, find a loop's crossovers and margins, and compute the state-feedback gain that puts the poles where you want them. In this lab you'll write all three in NumPy and test them against numbers you already know: the 16.3% overshoot of ζ = 0.5, the 3× gain margin of , and the servo gains of lesson 34.
Predict
Your margin calculator scans the phase of for −180°. Why must it unwrap the phase first?
np.angle returns values in (−π, π], so a phase falling past −180° reappears just below +180°. A naive sign test sees the jump, not a smooth crossing, and a root finder lands on the discontinuity. np.unwrap removes the jumps over a dense grid; inside a small bracket, measure the phase relative to the bracket's end, , to keep it continuous.
Name it
Simulating a transfer function
For , the controllable canonical form has and , with output . A fixed-step RK4 integrator is accurate to about per step, plenty for step responses with Δt a hundredth of the fastest time constant.
Margins
Evaluate on a dense logarithmic grid, unwrap the phase, find the first sign change of and of , and refine each by bisection. Report None when a crossing doesn't exist in the range.
Ackermann's formula
where is the desired characteristic polynomial evaluated at the matrix A. np.poly(poles) gives the polynomial's coefficients from its roots.
Worked example
What should margins([10], [0.1, 1, 0]) return?
Show the solution
- Recognize the loop. That's from lesson 23.
- Gain crossover. It crosses over at 7.8615 rad/s with 51.83° of phase margin.
- Phase crossover. Its phase only approaches −180° as ω → ∞, so there's no phase crossover and no finite gain margin.
Answer (7.8615, 51.83, None, None).
Worked example
What does place return for the double integrator with poles at −2 and −3?
Show the solution
- Companion form. The desired polynomial is and the double integrator's is , so
- Check with Ackermann's formula.
- Read off K. The last row after is (6, 5), the same gain.
Answer K = (6, 5).
Check
Step 1. Write step_response(num, den, T, dt): the controllable canonical form integrated with RK4.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Step 2. Write margins(num, den): gain and phase crossovers, phase margin and gain margin.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Step 3. Write place(A, B, poles): Ackermann's formula.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
Why does Ackermann's formula need the controllability matrix to be invertible?
Ackermann's formula works by transforming the system into companion form, where each feedback gain adds directly to one coefficient of the characteristic polynomial, choosing the gains there, and transforming back. The transformation is built from the controllability matrix, so it only exists when that matrix is invertible. If it isn't, some mode of the system can't be influenced by the input at all, and no choice of gains can move that mode's pole. The formula then has no solution, which is the algebraic form of the physical fact.
Practice
Keep practicing
3 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.