Gallery
Control systems
4 examples. Open any of them in the editor to change it.
Second-order systems: damping and overshoot Open ↗ Code
% G(s) = wn² / (s² + 2 ζ wn s + wn²) for several damping ratios ζ
s = tf('s'); wn = 2;
zetas = [0.1 0.3 0.5 0.7 1];
t = linspace(0, 8, 600);
hold on
for z = zetas
G = wn^2 / (s^2 + 2*z*wn*s + wn^2);
[y, ~] = step(G, t);
plot(t, y, 'LineWidth', 1.6)
end
hold off, yline(1, ':')
legend('ζ = 0.1', 'ζ = 0.3', 'ζ = 0.5', 'ζ = 0.7', 'ζ = 1'), xlabel('time (s)'), ylabel('output')
S = stepinfo(wn^2 / (s^2 + 2*0.3*wn*s + wn^2));
title(sprintf('Step responses (ζ = 0.3: %.0f %% overshoot, settles in %.1f s)', S.Overshoot, S.SettlingTime))
PID control of a DC motor Open ↗ Code
% Motor speed: G(s) = K / ((J s + b)(L s + R) + K²), classic textbook values
J = 0.01; b = 0.1; K = 0.01; R = 1; L = 0.5;
s = tf('s');
G = K / ((J*s + b)*(L*s + R) + K^2);
Cp = pid(100); % proportional only
Cpi = pid(100, 200); % + integral: removes the steady-state error
Cpid = pidtune(G, 'PID'); % tuned automatically
step(feedback(Cp*G, 1), feedback(Cpi*G, 1), feedback(Cpid*G, 1), 3)
legend('P', 'PI', 'PID (pidtune)')
[Kp, Ki, Kd] = piddata(Cpid);
title(sprintf('Speed step: PID gains Kp = %.1f, Ki = %.1f, Kd = %.1f', Kp, Ki, Kd))
Bode plot and stability margins Open ↗ Code
% Open loop with three lags and gain K: how much margin is left before the closed loop oscillates?
s = tf('s');
L = 4 / (s + 1)^3;
margin(L)
[Gm, Pm] = margin(L);
fprintf('gain margin %.2f (%.1f dB), phase margin %.1f degrees\n', Gm, 20*log10(Gm), Pm)
Code
% Where do the closed-loop poles go as the gain grows? Crossing into the right half plane = unstable.
G = tf(1, [1 3 2 0]); % 1 / (s (s + 1)(s + 2))
subplot(1, 2, 1), rlocus(G), xlim([-4 1]), ylim([-3 3])
subplot(1, 2, 2), nyquist(6 * G), xlim([-3 1]), ylim([-2 2])
title('Nyquist of 6 G: the loop passes −1 → unstable at K = 6')