ENع
←PI and PD
Lesson 38 · PID control and compensators

Adding integral and derivative action

16 min10 steps5 questionsAdvanced
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.

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig 38 · Interactive
01 / 10

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.833
Loading figure…
or ← →
Read the lesson step by step
  1. 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.833
  2. Why 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.
  3. 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.
  4. 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.999
  5. The 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 0
  6. Derivative: 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.
  7. 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.833
  8. Why 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.
  9. 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)
  10. 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)

Quiz Unlocks after the last step.