Solving a Control System: PID Tuning from Scratch

You have a thermal chamber that needs to hold temperature within 0.5 degrees of setpoint. The plant is a first-order system with a 10-second time constant, and the heater responds linearly to your control signal. Here is how to design, tune, and verify a PID controller in Forge.

The Code

  1. G = tf([1], [10 1]) — define the plant model
  2. C = pid(2, 0.5, 0.1) — set PID gains
  3. T = feedback(C*G, 1) — compute closed-loop transfer function
  4. step(T) and stepinfo(T) — analyze the step response
  5. margin(C*G) — check stability margins
  6. pidtune(G, 'pid') — auto-tune for comparison
PID step response

Walkthrough

Defining the plant transfer function

tf([1], [10 1]) gives G(s) = 1/(10s+1), a first-order system with DC gain of 1 and a time constant of 10 seconds. This models the thermal mass of the chamber - it reaches 63% of its final temperature in 10 seconds.

Creating the PID controller

pid(2, 0.5, 0.1) creates a PID controller with three gains. Kp = 2 reacts to the current error between setpoint and measurement. Ki = 0.5 integrates accumulated error over time, eliminating any steady-state offset. Kd = 0.1 acts on the rate of change of error, damping oscillations before they develop.

Computing the closed-loop system

feedback(C*G, 1) computes the closed-loop transfer function T = CG/(1+CG) with unity feedback. This is the transfer function from the setpoint input to the temperature output - the system your thermal chamber actually runs.

Step response analysis

step() simulates the system's response to a unit step input, and stepinfo() extracts the key performance metrics - rise time, overshoot, and settling time. For the thermal chamber application, you want fast rise time to reach setpoint quickly, with minimal overshoot to stay within the 0.5-degree tolerance band.

Stability margins

margin(C*G) plots the Bode diagram of the open-loop transfer function with gain margin and phase margin annotations overlaid. Your targets here are a gain margin greater than 6 dB and a phase margin greater than 45 degrees - these give the system enough robustness to handle modeling errors and component variations without going unstable.

Auto-tuning for comparison

pidtune(G, 'pid') uses numerical optimization to find PID gains that balance performance and robustness automatically. By overlaying both the manual and auto-tuned step responses on the same plot, you can see exactly where your hand-tuned gains trade off differently - maybe faster rise time but more overshoot, or vice versa.

PID tuning comparison

What Just Happened

You went from a plant model to a fully tuned controller with stability margins verified - in under 15 lines of code. The step response, Bode diagram, and performance metrics are all visible immediately in Forge, giving you a complete picture of your control system's behavior without switching between tools or writing boilerplate.

Going Further