Simulation of control systems in MATLAB/Simulink

Modelling and analysing control systems in MATLAB (tf, ss, feedback, step, stepinfo, damp, margin, rlocus) and Simulink (blocks, solvers, step size), with hand checks of every simulated result.

Drafted with Aria, reviewed by the AiCanCode.org team. Spotted an error? Use Give Feedback at the bottom of the page.

Why it matters

Nobody commissions a servo drive or a robot joint straight from hand calculations. Engineers model the plant, close the loop and check step response, margins and actuator effort in simulation first, because it is cheap, fast and safe. MATLAB's Control System Toolbox and Simulink are the standard tools in industry and in university labs (Python's python-control and Scilab/Xcos work the same way), and being able to check a hand result against a simulation is a core interview and lab skill.

Key ideas

Two ways of working.

  • MATLAB scripts (Control System Toolbox): build LTI objects and analyse them with commands. Best for linear analysis — poles, step response, Bode, root locus, margins, design.
  • Simulink: drag-and-connect block diagrams solved numerically in time. Best when the model has nonlinearities (saturation, dead zone, friction, quantisation), delays, mixed continuous and discrete parts, or several interacting subsystems.

Building models in MATLAB.

  • G = tf([1 3],[1 4 5]) creates (s + 3)/(s² + 4s + 5); coefficient vectors are in descending powers of s.
  • s = tf('s'); G = 10/(s*(s+2)) — the same thing written algebraically.
  • zpk([-3],[-2+1i -2-1i],1) gives zero–pole–gain form; ss(A,B,C,D) gives state space; tf(sys) and ss(sys) convert.
  • Interconnection: series(G1,G2) or G1*G2; parallel(G1,G2) or G1+G2; feedback(G,H) for negative feedback, feedback(G,H,+1) for positive.
  • Analysis: pole, zero, damp (ωn and ζ of each pole), dcgain, step, impulse, lsim (arbitrary input), stepinfo (rise time, settling time, overshoot), bode, margin (GM, PM and crossovers), nyquist, rlocus, rlocfind.
  • Design: pidtune and the PID Tuner app, place and acker for pole placement, ctrb and obsv with rank, c2d(G,T,'zoh') for discretisation.

Building models in Simulink. Typical blocks: Step (source), Sum (set signs "+−" for negative feedback), Gain, Transfer Fcn, State-Space, PID Controller, Integrator, Saturation, Transport Delay, Zero-Order Hold, Scope and To Workspace. Set the Step block's step time and final value, the Sum signs, and the simulation stop time; log signals to compare with hand results.

Solvers. Simulink integrates the model's differential equations numerically. Variable-step solvers (ode45 by default; ode15s for stiff systems with widely separated time constants) adapt the step for accuracy. Fixed-step solvers (ode1 Euler, ode4 Runge–Kutta) are used for code generation and real-time targets. The step size must be small compared with the fastest time constant: explicit Euler on dx/dt = −a·x is stable only for step h < 2/a, and accurate only for h much smaller (about a tenth of the time constant or less).

Good practice. Check every simulation against something you can compute by hand: DC gain, pole locations, final value, overshoot. Model actuator limits (saturation) — a linear model happily asks for 1000 V. Use realistic sensor noise and sample times when testing a digital controller. Keep units consistent throughout (SI).

Formulas

G(s) = num(s)/den(s) — coefficients in descending powers of s, as entered in tf(num, den).

T(s) = G(s)/(1 + G(s)·H(s)) — what feedback(G,H) computes.

ωn = √(den₀), ζ = den₁/(2ωn) for den = s² + den₁·s + den₀ — what damp reports for a complex pair.

DC gain = G(0) = num₀/den₀ — what dcgain reports (stable G).

Explicit Euler: x_(k+1) = x_k + h·f(x_k, u_k); stable for dx/dt = −a·x only if h < 2/a (h in s, a in s⁻¹).

Worked examples

Example 1 (standard — check a step response). Simulate G(s) = 1/(s² + 3s + 2) and predict what MATLAB reports.

  1. Script: G = tf(1,[1 3 2]); step(G); stepinfo(G); damp(G); dcgain(G).
  2. Poles: s² + 3s + 2 = (s + 1)(s + 2) → −1 and −2, both real, so damp shows ζ = 1 for each pole (overdamped overall, no overshoot).
  3. DC gain = 1/2 = 0.5, so the step response settles at 0.5, not 1.
  4. Analytic response: y(t) = 0.5 − e^(−t) + 0.5e^(−2t).
  5. 10–90 % rise time: y reaches 0.05 at t = 0.380 s and 0.45 at t = 2.970 s → tr ≈ 2.59 s.
  6. 2 % settling time (|y − 0.5| ≤ 0.01): ts ≈ 4.60 s; overshoot 0 %. stepinfo should report the same values; if it does not, the model was entered wrongly.
  7. Simulink version: Step → Transfer Fcn (numerator [1], denominator [1 3 2]) → Scope, stop time 10 s.

Example 2 (GATE level — closed loop and margins). For G(s) = 10/(s(s + 2)) with unity feedback, predict the output of T = feedback(G,1); damp(T); stepinfo(T); margin(G).

  1. T(s) = 10/(s² + 2s + 10): ωn = √10 = 3.16 rad/s, ζ = 2/(2 × 3.16) = 0.316.
  2. Overshoot Mp = exp(−π × 0.316/√(1 − 0.1)) = 35.1 %.
  3. Peak time tp = π/(ωn√(1 − ζ²)) = π/3.0 = 1.05 s.
  4. Settling time (2 % envelope estimate) ≈ 4/(ζωn) = 4/1 = 4 s (stepinfo, which uses the true curve, will give a somewhat smaller value).
  5. Margins of G: gain crossover where 10 = ω√(ω² + 4) → ω⁴ + 4ω² − 100 = 0 → ω² = 8.20, ω_gc = 2.86 rad/s; PM = 180° − 90° − tan⁻¹(2.86/2) = 90° − 55.1° = 34.9°. The phase never reaches −180°, so GM = ∞ (margin reports Inf).
  6. Steady-state error to a unit ramp: Kv = 10/2 = 5 s⁻¹ → ess = 0.2 — check with lsim(T, t, t).

Common mistakes

  • Entering coefficients in ascending order or dropping a zero coefficient: s² + 5 is [1 0 5], not [1 5].
  • Using G*H/(1+G*H) by hand instead of feedback, which creates non-minimal models with cancelled poles; use minreal if needed.
  • Wrong signs on the Simulink Sum block, giving positive feedback.
  • Too short a stop time, so the response has not settled, or too large a fixed step, giving numerical instability that is not physical.
  • Trusting a linear simulation that demands impossible actuator signals; add a Saturation block.
  • Confusing margin of the open loop (correct) with margin of the closed loop (meaningless).

For GATE ME

GATE does not test software syntax, but simulation-based thinking is exactly what its numerical questions check: ωn, ζ, overshoot, settling time, DC gain, poles and margins of a given loop. Use this topic to practise predicting the result before you simulate, and to verify hand answers for problems from every other topic in this subject.

Quick check

  1. What does tf(1,[1 2]) represent, and what is its time constant?
  2. What is ωn of 5/(s² + 6s + 5)?
  3. Which command gives the closed-loop transfer function for unity negative feedback around G?
  4. What is the damping ratio of 1/(s² + 4s + 3)?
  5. For explicit Euler on dx/dt = −10x, what step size makes the simulation unstable?

Answers: 1. 1/(s + 2); τ = 0.5 s. 2. √5 = 2.24 rad/s. 3. feedback(G,1). 4. ωn = √3, ζ = 4/(2√3) = 1.15 (overdamped). 5. Any h > 0.2 s.

Try answering each one aloud before you open it.

  1. 1.What is a control system and how is it represented in MATLAB/Simulink?Concept

    A control system is a set of devices or algorithms that manage, command, direct, or regulate the behavior of other devices or systems. In MATLAB/Simulink, control systems are represented using block diagrams where each block represents a mathematical operation or a system component. Simulink provides a graphical interface to model, simulate, and analyze dynamic systems.

  2. 2.Explain the role of transfer functions in control systems and how they are used in MATLAB.Concept

    Transfer functions are mathematical representations of the relationship between the input and output of a linear time-invariant system. They are used to analyze the stability and response of control systems. In MATLAB, transfer functions can be created using the 'tf' command, which allows users to define the numerator and denominator coefficients of the transfer function.

  3. 3.What is the purpose of using Simulink for control system simulation?Concept

    Simulink is used for control system simulation because it provides a visual platform to model, simulate, and analyze dynamic systems. It allows engineers to design complex control systems using block diagrams, making it easier to understand and modify the system. Simulink also supports real-time simulation and testing, which is crucial for validating control strategies before implementation.

  4. 4.Why is feedback important in control systems, and how can it be implemented in Simulink?Application

    Feedback is important in control systems because it helps maintain the desired output by automatically correcting any deviations from the setpoint. It improves system stability and performance. In Simulink, feedback can be implemented by connecting the output of a system back to its input through a feedback loop, often using a 'Sum' block to subtract the feedback signal from the reference input.

  5. 5.What happens if a control system is simulated without considering time delays in Simulink?Application

    If a control system is simulated without considering time delays, the simulation results may not accurately reflect the real-world behavior of the system. Time delays can affect system stability and performance, leading to incorrect conclusions about the system's response. In Simulink, time delays can be modeled using blocks like 'Transport Delay' to ensure accurate simulation results.

  6. 6.How can PID controllers be tuned in MATLAB/Simulink?Application

    PID controllers can be tuned in MATLAB/Simulink using various methods such as manual tuning, Ziegler-Nichols method, or using the PID Tuner app. The PID Tuner app provides an interactive interface to automatically tune the PID gains to achieve desired performance criteria like rise time, settling time, and overshoot. It allows users to visualize the system response and adjust the parameters accordingly.

  7. 7.What is the significance of the 'step' function in MATLAB for control systems?Application

    The 'step' function in MATLAB is used to simulate the step response of a control system. It is significant because the step response provides insights into the system's transient and steady-state behavior, such as rise time, settling time, and overshoot. Analyzing the step response helps in assessing the performance and stability of the control system.

  8. 8.How would you obtain the step response of G(s) = 1/(s + 2) in MATLAB, and what should you expect to see?Numerical

    Define G = tf(1,[1 2]) and call step(G) to plot it, or stepinfo(G) for the numbers. Before running it, predict: the time constant is τ = 1/2 = 0.5 s and the DC gain is G(0) = 0.5, so y(t) = 0.5(1 − e^(−2t)) rises without overshoot to 0.5, reaching 63 % of that at 0.5 s and settling (2 %) at about 4τ = 2 s. If the plot does not match these hand values, the model was entered wrongly.

  9. 9.Determine the poles of H(s) = (s + 3)/(s² + 4s + 5), and show how to confirm them in MATLAB.Numerical

    Setting s² + 4s + 5 = 0 gives s = (−4 ± √(16 − 20))/2 = −2 ± j1. So ωn = √5 = 2.24 rad/s and ζ = 4/(2√5) = 0.894: a stable, well-damped complex pair with only a very small overshoot; the zero is at −3. In MATLAB: H = tf([1 3],[1 4 5]); pole(H) returns −2 ± 1i, zero(H) returns −3, and damp(H) lists ωn and ζ for each pole.

  10. 10.Explain how state-space representation is used in Simulink for control systems.Concept

    State-space representation is used in Simulink to model control systems in terms of state variables, which describe the system's internal state. It is particularly useful for multi-input multi-output (MIMO) systems. In Simulink, state-space models can be implemented using the 'State-Space' block, where users define matrices A, B, C, and D that represent the system dynamics. This representation allows for efficient simulation and analysis of complex systems.

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?