From s to code
A motor's shaft position is 1/(s(s + 1)). The lead controller C(s) = 50(s + 1)/(s + 10) makes the closed loop 50/(s² + 10s + 50): poles −5 ± 5j and 4.3% overshoot. It is a continuous-time design. A computer must now run it.
The computer reads y once every T seconds and holds u between readings, as in "The sampled control loop: ADC, computer, DAC". It cannot integrate or differentiate a signal. It can only do arithmetic on stored numbers, so C(s) must become a difference equation.
Two recipes
Forward Euler estimates a slope from the next sample: dx/dt ≈ (x[k+1] − x[k])/T. Writing z for 'one sample later', that is s ≈ (z − 1)/T. Put it into C(s) and the pole at s = −10 lands at z = 1 − 10T.
That becomes u[k] = A·u[k−1] + B0·e[k] + B1·e[k−1], with A = 1 − 10T, B0 = 50, B1 = −50(1 − T). At T = 0.02 s: A = 0.80, B0 = 50, B1 = −49. The code stays fixed; only the numbers change.
Tustin's method adds up area with trapezoids, averaging the old and new samples. That gives s ≈ (2/T)(z − 1)/(z + 1), the bilinear transform from the Signals course. The pole at s = −10 now lands at z = (2 − 10T)/(2 + 10T).
Same code, different numbers: A = (2 − 10T)/(2 + 10T), B0 = 50(2 + T)/(2 + 10T), B1 = −50(2 − T)/(2 + 10T). At T = 0.02 s: A = 0.818, B0 = 45.9, B1 = −45.0. The exact pole e^(−0.2) is 0.819.
Fast and slow
Run both at T = 0.02 s, fifty samples a second. Both stay close to the continuous design: peaks 1.043 (Euler) and 1.058 (Tustin) against 1.043. When T is small next to the system's time scales, any sensible recipe works.
Now slow the sampling and follow the controller's pole. A difference equation is stable only while its pole stays inside |z| < 1. Euler's 1 − 10T passes −1 at T = 0.2 s. Tustin's (2 − 10T)/(2 + 10T) never reaches −1.
At T = 0.25 s Euler sets A = −1.50: every sample the controller multiplies its last output by −1.50. The drive signal u goes 50, −135, 268, −541 and keeps growing, flipping sign each time. The motor leaves ±2 after 0.73 s.
At T = 0.25 s Tustin gives A = −0.111, inside the circle: it settles, but overshoots 53%. The hold adds a delay the design never saw. Tustin kept it stable. To keep it accurate, sample faster or design the controller with the hold included.
Euler maps the stable left half s-plane onto everything left of z = 1, which spills outside the unit circle. Tustin maps it exactly onto the inside of the circle, so a stable C(s) always gives a stable C(z). The price: warped frequencies.
The working
# step 1/11 · design # about: Design C(s), Motor 1/(s(s+1)), Position y, continuous plant G(s) = 1 / (s(s + 1)) y'' + y' = u controller C(s) = 50(s + 1) / (s + 10) C·G = 50 / (s(s + 10)) (the zero cancels the pole at -1) closed loop 50 / (s² + 10s + 50) poles -5 ± 5j ζ = 0.707 ωn = 7.07 rad/s step y = 1 - e^(-5t)(cos 5t + sin 5t) peak 1.043 at t = 0.63 s (4.3 % overshoot)
The motor, the controller and both sample periods are round illustrative numbers. Every curve is computed from the sampled loop: the controller runs its difference equation every T, the DAC holds u, and the motor follows the exact solution of y'' + y' = u between samples. The Python on each step is plain Python 3 with the math module and prints the same numbers as the figure. python-control's sample_system and TransferFunction.sample accept method='euler' (forward difference) and method='tustin' or 'bilinear'.