Back to Home

PID controller: modeling P I D parts

The article demonstrates modeling of PID controller via FDTD simulation of heater and mechanical system. Effects of P, I, D components are analyzed with Mathematica code examples. Elimination of static error and response optimization are discussed.

How PID works: simulation with code and graphs
Advertisement 728x90

Modeling a PID Controller: From Proportional Basics to Full Feedback Loop

A PID controller generates a control signal based on the error e(t) = setpoint - measured_value. In its simplest form, the proportional component u(t) = K_p * e(t) corrects deviations. For example, in water level control: when e(t) > 0, a valve opens proportionally to the error, but the signal is capped at maximum flow.

Real-world systems exhibit delays. For a heater, heat spreads according to the diffusion equation ∂w/∂t = Δw + f(x,y,t), where f represents the heat source. This is a diffusive process with damping—similar to waves with energy loss.

Numerical Simulation of Heat Diffusion

Analytical solutions in Mathematica show temperature evolution:

Google AdInline article slot
sol = NDSolveValue[
  {
    D[w[x, y, t], t] == Laplacian[w[x, y, t], {x, y}] + 
      If[(x + 2)^2 + y^2 < 0.1 && t > 0.0, 100.0, 0],
    w[x, y, 0] == 0
  },
  w,
  {x, y} ∈ Rectangle[{-2, -1}, {2, 1}],
  {t, 0, 10}
];

Temperature at points far from the source rises with delay. To simulate feedback, we switch to discrete time-domain modeling using FDTD (Finite-Difference Time-Domain).

Basic step for 2D heat equation:

solveHeat[w_, f_, dt_: 0.0025, dx_: 0.1] := Table[
  If[i > 1 && i < 50 && j > 1 && j < 50,
    w[[i, j]] + dt (
      w[[i - 1, j]] + w[[i, j - 1]] - 4 w[[i, j]] + 
      w[[i, j + 1]] + w[[i + 1, j]]
    )/dx^2 + dt f[i, j],
    w[[i, j]]
  ],
  {i, 50}, {j, 50}
];

CFL stability condition: δt / δx² ≤ 0.25. Simulations of turning the heater on/off confirm system inertia.

Google AdInline article slot

Proportional Controller: Persistent Error

P-controller connection: heater = K_p * Clip[error, {0, ∞}]. At low heater power, temperature stabilizes below target due to losses. Increasing K_p reduces settling time but amplifies oscillations—and steady-state error remains.

Graph shows:

  • Low power: slow rise, fails to reach setpoint.
  • High power: oscillations without convergence.

Integral Component: Eliminating Steady-State Error

The integral accumulates error: accError += error; u(t) = K_p error + K_i accError. This compensates for static losses.

Google AdInline article slot

Example with reduced power:

Module[{w = Table[0., {50}, {50}], accError = 0.0},
  Table[
    With[{error = (0.0022 - w[[25, 25]])},
      {
        heater = 20000.0 Clip[error + 0.001 accError, {0, Infinity}]
      },
      accError += error;
      w = solveHeat[w, Function[{i, j}, If[Max[Abs[{i, j} - {25, 2}]] < 1, heater, 0.0]]];
      {{steps, w[[25, 25]]}, {steps, heater/30000.0}}
    ],
    {steps, 1, 3000}
  ]
];

Temperature converges to setpoint without oscillation.

Derivative Component: Predicting Oscillations

The derivative reacts to the rate of change of error: u(t) = K_p e + K_i ∫e + K_d * de/dt. It dampens future overshoots.

Full PID reduces rise time but demands precise tuning. Excessive K_d introduces artifacts.

Comparison:

  • P-only: slow response, persistent error.
  • PI: convergence, possible overshoot.
  • PID: optimal response, minimal oscillation.

Formal Definition and Mechanical System

Complete formula: u(t) = K_p e(t) + K_i ∫₀ᵗ e(τ) dτ + K_d de/dt.

For a mass m: x''(t) = u(t)/m. Differentiating the PID simplifies it: u'(t) = K_i e + K_p e' + K_d e''.

Without controller: constant u=1 yields x(t) = t²/(2m)—parabolic trajectory.

With P-controller: e(t) = a - x(t) forms a closed loop. Tuning coefficients is an iterative process accounting for system dynamics.

Key Takeaways:

  • Proportional term provides base response but leaves steady-state error.
  • Integral eliminates accumulated deviation in inertial systems.
  • Derivative predicts changes, accelerating stabilization.
  • FDTD simulation visualizes effects for debugging.
  • Stability requires satisfying CFL condition and limiting u(t).

— Editorial Team

Advertisement 728x90

Read Next