Adding integral and derivative action
About this lesson
Proportional control leaves a steady offset and often overshoots. Take one plant, 1/((s+1)(s+2)), and watch what two extra terms do. An integral term adds up the error until the offset is gone. A derivative term watches how fast the output moves and brakes before it overshoots. Every curve is simulated from the model, and the same Python loop runs on each step.
P alone leaves a gap
Our plant is G(s) = 1/((s+1)(s+2)) and the goal steps to 1. Proportional control, u = 10·e, overshoots by 22.1% and then settles at 0.833. An error of 0.167 stays for ever.
# P only
Kp, Ki, Kd, dt = 10, 0, 0, 0.0001
y = v = I = 0.0 # output, its speed, integral
for n in range(100000): # 10 s
e = 1.0 - y # goal r = 1
u = Kp * e + I - Kd * v # P + I + D
a = u - 3 * v - 2 * y # plant: y'' + 3y' + 2y = u
y, v = y + v * dt, v + a * dt
I += Ki * e * dt
print(f"{y:.3f}") # 0.833Read the lesson step by step
P alone leaves a gap
Our plant is G(s) = 1/((s+1)(s+2)) and the goal steps to 1. Proportional control, u = 10·e, overshoots by 22.1% and then settles at 0.833. An error of 0.167 stays for ever.
# P only Kp, Ki, Kd, dt = 10, 0, 0, 0.0001 y = v = I = 0.0 # output, its speed, integral for n in range(100000): # 10 s e = 1.0 - y # goal r = 1 u = Kp * e + I - Kd * v # P + I + D a = u - 3 * v - 2 * y # plant: y'' + 3y' + 2y = u y, v = y + v * dt, v + a * dt I += Ki * e * dt print(f"{y:.3f}") # 0.833Why the gap stays
At rest this plant needs u = 2·y to hold y. With P alone u = 10·e, so balance needs e = 1/6 ≈ 0.167. A bigger Kp shrinks the gap but never closes it: Kp = 40 gives 0.048 and 47% overshoot.
at rest y' = y'' = 0, so 2·y = u P only u = Kp·e = Kp·(1 − y) 2·y = 10·(1 − y) → y = 10/12 = 0.833 e = 1/(1 + Kp·G(0)) = 1/6 = 0.167 Kp = 40 e = 1/21 = 0.048 overshoot 47% (ζ = 0.23) A type 0 loop always keeps a step error.Integral: add up the error
The integral term keeps a running total of the error: I = Ki·∫e dt, the area under e(t). While e is positive the total grows; when e is negative it shrinks. It stops changing only when e is exactly zero.
I(t) = Ki · ∫₀ᵗ e(τ) dτ Ki = 5 in code, every dt: I += Ki * e * dt e > 0 I grows e < 0 I shrinks e = 0 I holds its value In Laplace terms the term is Ki/s: a pole at s = 0.PI closes the gap
PI control: u = 10·e + 5·∫e dt. The total keeps growing while any gap remains, so y climbs past 0.833 to 1. The price: a peak of 1.19 (19.5% overshoot), settling within 2% after 2.76 s.
# PI Kp, Ki, Kd, dt = 10, 5, 0, 0.0001 y = v = I = 0.0 # output, its speed, integral for n in range(100000): # 10 s e = 1.0 - y # goal r = 1 u = Kp * e + I - Kd * v # P + I + D a = u - 3 * v - 2 * y # plant: y'' + 3y' + 2y = u y, v = y + v * dt, v + a * dt I += Ki * e * dt print(f"{y:.3f}") # 0.999The integral holds u
By 10 s the error is 0.001, so P is almost zero. The integral carries almost all of u: I = 1.99 of u ≈ 2, what the plant needs for y = 1. The 1/s pole makes the loop type 1: no step error.
t = 10 s e = 0.001 P = 10·e ≈ 0 I = 1.99 ≈ u plant needs u = y / G(0) = 1 / 0.5 = 2 slow pole s = -0.46 (time constant 2.2 s) sets the long tail loop L(s) = (10s + 5) / (s(s+1)(s+2)) characteristic s³ + 3s² + 12s + 5 = 0 Routh: 3·12 = 36 > 5, all coefficients > 0 → stable, type 1, step error 0Derivative: watch the speed
The derivative term reacts to how fast the output moves: D = −Kd·dy/dt. Rising fast means a strong brake before the goal is reached. It reads the output, not the error, so a step in the goal gives no sudden kick.
D(t) = −Kd · dy/dt Kd = 2 for t > 0 the goal is constant, so de/dt = −dy/dt: the same term as Kd·de/dt except at the instant r jumps (no kick). The block is Kd·s; acting on y in the feedback path it adds no closed-loop zero. It predicts: the slope says where y will be a moment from now.PD calms the swing
PD control: u = 10·e − 2·dy/dt. Overshoot falls from 22.1% to 3.8%, and settling from 2.42 s to 1.71 s. But at rest dy/dt = 0, so D adds nothing and the 0.167 gap remains.
# PD (derivative on the output) Kp, Ki, Kd, dt = 10, 0, 2, 0.0001 y = v = I = 0.0 # output, its speed, integral for n in range(100000): # 10 s e = 1.0 - y # goal r = 1 u = Kp * e + I - Kd * v # P + I + D a = u - 3 * v - 2 * y # plant: y'' + 3y' + 2y = u y, v = y + v * dt, v + a * dt I += Ki * e * dt print(f"{y:.3f}") # 0.833Why derivative damps
The closed loop obeys y'' + (3 + Kd)·y' + 12·y = 10·r. Kd adds straight to the y' term, like extra friction. The damping ratio rises from 0.43 to 0.72, while the natural frequency stays 3.46 rad/s.
closed loop s² + (3 + Kd)·s + (2 + Kp) = 0 ωn = √12 = 3.46 rad/s P ζ = 3 / (2·3.46) = 0.43 overshoot 22.1% PD ζ = 5 / (2·3.46) = 0.72 overshoot 3.8% overshoot = e^(−ζπ/√(1−ζ²)) Kd moves the poles left without changing their distance from 0.Past, present, future
Side by side: I removes the offset, D tames the overshoot. P acts on the error now, I on the error stored up from the past, D on where the output is heading. All three together make the PID controller.
final y overshoot settle (2%) P 0.833 22.1% 2.42 s PI 0.999 19.5% 2.76 s PD 0.833 3.8% 1.71 s P present Kp·e I past Ki·∫e dt D future Kd·(rate of change)What each extra costs
Integral action lags, so it adds overshoot, and it winds up when the actuator saturates. Derivative action amplifies noise: a ripple at 100 rad/s gets 100 times the gain of one at 1 rad/s, so real controllers filter D.
noise n(t) = 0.01·sin(100t) on the sensor D term −Kd · dn/dt = −2 · 0.01 · 100 · cos(100t) amplitude 2.0 (P sees only 10·0.01 = 0.1) fix: filter D, e.g. Kd·s / (1 + s·Kd/(N·Kp)) windup: if u hits its limit, stop adding to I (both in the lessons on PID and windup)
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01With P control alone (Kp = 10) the plant 1/((s+1)(s+2)) settles at 0.833 for a step goal of 1. What is the steady-state error, and why?
e = 1/(1 + Kp·G(0)) = 1/(1 + 10·0.5) = 1/6 ≈ 0.167. A type 0 loop needs a non-zero error to keep the actuator pushing.
02Why does adding Ki·∫e dt remove that error?
At rest nothing may change, including the integral, so e must be zero. Here the integral settles on u = 2 (1.99 at 10 s), exactly what the plant needs for y = 1. Its pole at s = 0 makes the loop type 1.
03With derivative action on the output, the closed loop becomes s² + (3 + Kd)s + 12. What does Kd = 2 change?
Kd adds to the s term only. ζ = (3 + Kd)/(2√12), so overshoot drops from 22.1% to 3.8%. The constant term, and so ωn and the final value, do not change.
04Which pairing of term and weakness is right?
The integral keeps adding while a saturated actuator cannot respond, and it overshoots when the limit lifts. A derivative's gain grows with frequency (Kd·ω), so real controllers filter it.
05P, I and D are often described as acting on the present, the past and the future. Which matches?
P scales the error now, I sums the error so far, and D uses the slope to anticipate. Together they make the PID controller.
Glossary
- Steady-state error (offset)
- The error left once the response has settled. P control alone leaves one on a type 0 plant.
- Integral action
- A controller term proportional to the error added up over time, Ki·∫e dt. In Laplace terms Ki/s, a pole at s = 0.
- Derivative action
- A controller term proportional to a rate of change, here −Kd·dy/dt. In Laplace terms Kd·s. It adds damping.
- PI / PD controller
- Proportional plus integral (Kp·e + Ki·∫e dt) or proportional plus derivative (Kp·e + Kd·de/dt, or Kp·e − Kd·dy/dt, as here). All three together make PID.
- System type
- The number of pure integrators (poles at s = 0) in the open loop. Type 1 follows a step with zero steady-state error.
- Damping ratio ζ
- How quickly oscillations die out in a second-order response. Larger ζ means less overshoot; ζ = 1 is critical damping.
- Derivative kick
- A spike in the controller output when the goal steps, caused by differentiating the error. Differentiating the output instead avoids it.
- Integrator windup
- The integral keeps growing while the actuator is at its limit, causing a large overshoot afterwards.
Notes and sources
- The plant 1/((s+1)(s+2)) is a textbook example in normalised units, not a measured machine. Every curve and number is simulated from it with the same Python loop shown on the steps (Euler steps of 0.1 ms); the final values and overshoots agree with the closed-form results.
- Textbooks often write PD as Kp·e + Kd·de/dt, which adds a zero at s = −Kp/Kd = −5 and gives an impulse in u when the goal steps. Taking the derivative of the output instead (as here) keeps the same closed-loop poles and avoids that kick; Åström and Murray, Feedback Systems, ch. 11, discuss this under setpoint weighting.
- Integral action adds a pole at s = 0, so it raises the system type by one (see the lesson "System type and error constants"). That is why the step error goes from 1/6 to zero.
- The Python on the run steps is plain Python 3 with no libraries. python-control can check the same loops: control.feedback(C * G) with C = control.tf([10, 5], [1, 0]) for PI.
- N. S. Nise, Control Systems Engineering, 8th ed., Wiley (ch. 7 steady-state error; §9.2–9.3 PI and PD compensation)
- K. Ogata, Modern Control Engineering, 5th ed., Pearson (§5-7 integral and derivative control actions; ch. 8 PID controllers)
- G. F. Franklin, J. D. Powell, A. Emami-Naeini, Feedback Control of Dynamic Systems, 8th ed., Pearson (ch. 4, a first analysis of feedback)
- K. J. Åström and R. M. Murray, Feedback Systems, ch. 11 PID Control (FBSwiki)
- MIT OpenCourseWare 16.06 Principles of Automatic Control (S. Hall)
- MIT OpenCourseWare 6.302 Feedback Systems (Roberge, Dawson, Lundberg)
- Python Control Systems Library (python-control) documentation
Quiz Unlocks after the last step.