Control Systems and Simulation
Dynamic systems are described by differential equations, transfer functions G(s) = Y(s)/U(s) or state-space models ẋ = Ax + Bu, y = Cx + Du.
Concept
Dynamic systems are described by differential equations, transfer functions G(s) = Y(s)/U(s) or state-space models ẋ = Ax + Bu, y = Cx + Du. A first-order system G(s) = K/(τs + 1) has gain K and time constant τ: its step response reaches 63.2 % of the final value after τ.
10 % → 90 % of the final value (peak − final)/final × 100 % Last time outside a ±2 % (or 5 %) band setpoint − final value u = Kp·e + Ki·∫e dt + Kd·de/dt. P reacts to the present error, I removes steady-state error, D damps. Real implementations add actuator saturation and anti-windup, and run in discrete time (Δt).
The Control System Toolbox and Simulink In MATLAB with the Control System Toolbox, the same analysis is a few lines. *MATLAB toolbox — not run in this course:*
Simulink builds the same loop graphically from blocks (Transfer Fcn, PID Controller, Saturation, Scope) and can generate C code for ECUs (Embedded Coder).
The example below simulates the loop with core MATLAB only, which is also how a controller runs inside an ECU: a discrete update every Δt.
% Control System Toolbox - not run in this course
G = tf(2, [0.5 1]); % K / (tau s + 1)
C = pid(2, 8); % Kp, Ki
T = feedback(C * G, 1); % closed loop
step(T); stepinfo(T) % overshoot, rise time, settling time
bode(G); margin(C * G) % frequency response, stability marginsWhy It Matters
Motor speed control, cruise control, battery thermal management, power converters and ADAS functions are closed-loop control systems designed and verified in MATLAB/Simulink before code runs on hardware.
Syntax
w(k) = w(k-1) + dt / tau * (K * u(k) - w(k-1)); % Euler step of the plant
u(k) = min(max(Kp * e + Ki * integ, umin), umax); % PI with saturationExample
26.1 Open-loop step response and a PI speed controller with actuator limits
% DC motor speed as a first-order plant: tau * dw/dt + w = K * u
K = 2.0; tau = 0.5; % gain (rad/s per V), time constant (s)
dt = 0.001; T = 3; n = round(T / dt);
t = (0:n - 1) * dt;
% open-loop step response
w = zeros(1, n);
for k = 2:n
w(k) = w(k - 1) + dt / tau * (K * 1.0 - w(k - 1));
end
i63 = find(w >= 0.632 * K, 1);
fprintf('open loop: final speed %.4f, reaches 63.2%% at t = %.3f s (tau = %.1f)\n', w(end), t(i63), tau)
% closed loop with a PI controller, setpoint 10 rad/s
Kp = 2.0; Ki = 8.0; setpoint = 10;
w = zeros(1, n); u = zeros(1, n); integ = 0;
for k = 2:n
e = setpoint - w(k - 1);
integ = integ + e * dt;
u(k) = min(max(Kp * e + Ki * integ, 0), 24); % actuator limits 0..24 V
w(k) = w(k - 1) + dt / tau * (K * u(k) - w(k - 1));
end
overshoot = (max(w) - setpoint) / setpoint * 100;
rise = t(find(w >= 0.9 * setpoint, 1)) - t(find(w >= 0.1 * setpoint, 1));
settle_idx = find(abs(w - setpoint) > 0.02 * setpoint, 1, 'last') + 1;
fprintf('PI control: overshoot %.2f%%, rise time %.3f s, settling time (2%%) %.3f s, final error %.2e\n', ...
overshoot, rise, t(settle_idx), setpoint - w(end))
fprintf('peak actuator volt
…Expected Result
The open-loop response reached 63.2 % of its final value at t ≈ τ = 0.5 s, as first-order theory predicts. The PI controller brought the speed to the 10 rad/s setpoint with the printed overshoot, rise and settling times and a steady-state error close to zero (the integral action removes it). The actuator stayed within its 24 V limit.
open loop: final speed 1.9951, reaches 63.2% at t = 0.500 s (tau = 0.5)
PI control: overshoot 7.19%, rise time 0.190 s, settling time (2%) 0.863 s, final error -3.37e-06
peak actuator voltage 20.08 V (limit 24 V)Common Mistakes
Integrating the error without anti-windup while the actuator is saturated (large overshoot).
A simulation step Δt too large compared with the time constants.
Confusing continuous-time gains with discrete-time implementations.
Tuning on the model only and never checking robustness (gain/phase margins).
Best Practices
Model → simulate → tune → verify margins → implement → test on hardware (the V-model of model-based design).
Keep the simulation step at least 10× smaller than the fastest time constant.
Include saturation, delays and sensor noise in the simulation.
Interview Relevance
High for control/automotive roles: "what does each PID term do?", "how do you measure overshoot and settling time?", "what is integrator windup?", "what is model-based design?", "what does Simulink do?".
Practice Questions
Add a derivative term and compare overshoot for Kd = 0 and Kd = 0.1.
Implement anti-windup (stop integrating while saturated) and compare with a lower actuator limit.
Change τ to 2 s and retune the controller.
(MATLAB with toolbox) Reproduce the example with tf, pid, feedback and stepinfo.
Quick Quiz
Q1. (Multiple choice) Which PID term removes steady-state error? A) P B) I C) D D) None Answer: B.
Q2. (Predict the output) When does a first-order system with τ = 0.5 s reach 63.2 % of its final value?
Answer: At about 0.5 s (verified).
Q3. (True/False) Simulink can generate C code for embedded controllers.
Answer: True (Embedded Coder).
Q4. (Find the bug) A PI controller with a saturated actuator overshoots massively after a large setpoint change.
Answer: Integrator windup: the integral keeps growing while the output is saturated; add anti-windup.
Coding Challenge
Simulate a first-order plant (K = 1, τ = 1 s) under a P-only controller (Kp = 4) for a setpoint of 1, and show that a steady-state error of 1/(1 + Kp·K) = 0.2 remains.
K = 1; tau = 1; Kp = 4; dt = 0.001; n = 10000;
y = 0;
for k = 1:n
u = Kp * (1 - y);
y = y + dt / tau * (K * u - y);
end
fprintf('final output %.4f, steady-state error %.4f (theory %.4f)\n', y, 1 - y, 1 / (1 + Kp * K))