Algorithmintro
Process reaction curve method
Also called FOPDT model fitting, step response model fitting.
Fits a first-order plus dead time model to the open-loop step response from its gain, its delay and its time constant.
- Solves
- System identification
- Time
- O(n) worst
- Space
- O(n) extra
- Where
- n = the number of samples
- Model
- Real RAM
- Properties
- deterministicnot exactofflineiterative
- Evidence
- Proof sketchCode in 1 languages
- Origin
- John G. Ziegler, Nathaniel B. Nichols, 1942
The process reaction curve method fits the first-order plus dead time (FOPDT) model
to the recorded response of a process to one step of its input, taken in open loop. The gain is the final change of the output divided by the size of the step. The dead time and the time constant come from the shape of the curve: either from the tangent at its steepest point, the classical reading of Ziegler and Nichols in 1942, or from the two times at which the normalized output reaches 28.3 and 63.2 percent of its final change. The work is one pass over the samples, . The two-point formulas return and exactly for a true FOPDT response, and for any other S-shaped response they return an approximation. The three numbers , and are the input of many rules of thumb for tuning PID controllers.
In plain words
Put the process in a steady state, change its input by one fixed step, and record how the output moves until it settles again. Many processes answer in the same way: for a while nothing happens, then the output rises along an S-shaped curve and levels off. Three numbers describe that curve well enough to tune a controller. The gain says how far the output moved for each unit of input. The dead time says how long the process waited before it began to move. The time constant says how long the rise took. You can read them with a ruler from a printed curve, or compute them from the samples in one pass.
The problem
Input. Sample times , the output samples , and the step size . The step is applied at , the process is at rest at before it, and the output has settled at .
Output. , and of the FOPDT model, with measured from . Its unit step response is 0 for and for .
The algorithm
FOPDT-FIT(t, y, Δu, ⟨method⟩)- 1
K ← (y_(n-1) - y_0) / Δu - 2
z_i ← (y_i - y_0) / (y_(n-1) - y_0) for every i - 3
if ⟨method⟩ = two-point then - 4
t28, t63 ← the first times, after t_0, at which z reaches 1 - e^(-1/3) and 1 - e^(-1), by linear interpolation between samples - 5
T ← 1.5 · (t63 - t28) - 6
L ← t63 - T - 7
else: (s, tm, zm) ← the slope and the midpoint of the steepest chord (z_(i+1) - z_i) / (t_(i+1) - t_i) - 8
L ← tm - zm / s - 9
T ← 1 / s - 10
return (K, L, T)
Line L2 scales the curve so that it goes from 0 to 1; the levels of L4 are then and . For an FOPDT curve these levels are reached at and , so their distance is , which gives L5 and L6. Lines L7 to L9 are the tangent method on sampled data: the steepest chord stands for the tangent at the inflection point, and the line through the chord midpoint with slope crosses at and at .
Example
Take the process , three equal first-order lags in series, a unit step and samples every s. Its step response is , so and .
| slope of the chord ending at |
Two-point. L4 interpolates between and , and between and . L5 gives and L6 gives .
Tangent. The steepest chord is the one from to , with and midpoint . L8 gives and L9 gives .
Fine sampling (computation). The exact curve has its inflection point at with slope , so the true tangent gives and , and the exact crossing times give and . With samples every s the algorithm returned and .
The two models are different approximations of the same curve. At rad/s the true gain is and the true phase rad; the tangent model gives and , the two-point model and (computation). The tangent model has the larger time constant and the smaller delay, so it underestimates the gain at middle frequencies.
Correctness
Claim. Suppose the samples come from an FOPDT response, for and before, except that the last sample holds the settled value , and that the record is long enough (made precise in the proof). Then L1 returns exactly. If the crossing times of L4 are read from the continuous curve, the two-point method returns and exactly; with linear interpolation and sample spacing , each crossing time is off by at most . If is a sample time and the spacing near it is , the tangent method returns exactly and as .
Key invariant. After L2, for . This is concave, so the chords to the right of get flatter, and the steepest chord is the first one after the dead time.
The two-point formula follows from and . The tangent result follows because the line through the first chord after the dead time meets exactly at its left end. The full proof is below.
Proof: The FOPDT fit recovers K, L and T from a first-order plus dead time responsefull
We work in exact real arithmetic, with and as exact operations. Measure time from the step, so , and let , and . Let
the normalized step response of the FOPDT model.
Hypotheses. The samples are for and . The record is long enough in this sense: , and the last chord of , from to , is not steeper than any chord of that lies in . (The second condition holds when the last interval is not much shorter than the others, since is small.)
Lemma 1 (gain and normalization). L1 returns , and after L2, for and .
Proof. , which is nonzero, so L1 gives , and L2 gives .
Note that the sign of and of plays no role after L2: always rises from 0 to 1.
Lemma 2 (the two levels). For , the first time at which reaches is . In particular reaches at and at .
Proof. before , and on it is continuous and strictly increasing, so the first time is the unique solution of , which is . For this is , and for it is .
Theorem 1 (two-point, exact crossings). If L4 returns the exact first crossing times of , then L5 and L6 return and .
Proof. By Lemma 2, , so L5 gives , and L6 gives .
Theorem 2 (two-point, linear interpolation). Let every sample interval have length at most . Then each of the times returned by L4 differs from the exact crossing time by at most .
Proof. Fix a level and let , so by Lemma 2. Since and is nondecreasing, the scan of L4 stops at the first with ; then and so . Hence , and is smooth on with
Let be the line through and , and the time with , which L4 returns; . The error of linear interpolation is on the interval. So . By the mean value theorem, , both extremes taken over . Therefore
Theorem 3 (tangent). Suppose is a sample time with , and let . Then L7 selects the chord from to , L8 returns exactly, and L9 returns , which lies in and equals as .
Proof. For , , so those chords have slope 0. The chord from to has slope . On , is strictly concave, so the slopes of consecutive chords between samples in strictly decrease from left to right. By the hypothesis on the last chord, no chord is steeper than , and every chord after it in is strictly less steep. L7 keeps the first chord of largest slope, so it selects the chord at , with midpoint and . L8 gives , and L9 gives .
For the bounds, put ; then . Since , and the ratio is greater than 1. Since , , so and the ratio is at most . Multiplying by gives . Finally, is analytic at with the expansion , so .
Corollary. Under the hypotheses, the algorithm returns exactly by both methods; the two-point method returns and exactly with exact crossings, and within with interpolated crossings, since L5 and L6 are linear in and ; and the tangent method returns exactly and within , with error , when the dead time is a sample time.
Complexity
Line L1 is , line L2 is , the two scans of L4 and the scan of L7 are each, and the rest is . The total is arithmetic operations in the real RAM, plus two evaluations of for the levels, with extra space for the normalized curve; computing on the fly lowers this to .
Properties
- Exact for FOPDT data (theorem). As stated in the claim, for the two-point method with exact crossings and for the tangent method with the dead time on the sample grid.
- Not exact for other curves. A process of higher order has no exact FOPDT model, and the two methods give different models, as in the example. The two-point model matches the curve at two points by design; the tangent model matches the steepest slope.
- Noise sensitivity (measurement). With Gaussian noise of standard deviation added to the s samples of the example, the tangent method gave and , because the steepest chord between two noisy samples is set by the noise. The two-point method gave and .
- Gain error from an unsettled record. If the record stops at time before the output has settled, L1 returns for FOPDT data, and the levels of L4 shift with it.
- Open loop only. The method needs the process to be BIBO stable and to settle; an integrating or unstable process has no final value.
Variants and improvements
- Other pairs of levels. Several published two-point rules use other pairs of levels, such as 35.3 and 85.3 percent; any two levels give .
- Area method. Compute the area between the final value and the curve, which equals for an FOPDT model, and a second area to separate from . Integrals average the noise, so this is much less sensitive than the tangent method.
- Least squares fit. Fit , and to all samples by nonlinear least squares, an output error fit with a step input and a delay.
- Second-order plus dead time. Fit two time constants and a delay when the curve is clearly more sluggish than one lag.
- Integrating processes. Fit from the slope of the final ramp and its intercept with the time axis.
- Closed-loop alternatives. The relay autotuning test identifies one point of the frequency response with the loop closed.
When to use it
Use it to get a first model of a stable, well-damped process that can be run in open loop for one step test, as for many temperature, level and flow loops in the process industry, and to tune a PID controller from that model. Prefer the two-point or area method to the tangent method on measured data. Use a parametric fit or a frequency method when the process oscillates, when the dead time must be known accurately, or when the model will be used for more than tuning.
The tuning rules that start from , and are not part of the fit; they are rules of thumb.
- Ziegler and Nichols. For a PID controller, , and (Ziegler and Nichols 1942); see Ziegler-Nichols tuning. On the tangent model of the example this gives , and ; on the two-point model, , and . The rule aims at a quarter amplitude decay, which is often too aggressive.
- Cohen and Coon. Settings derived for the same model, with more attention to processes where is large compared with (Cohen and Coon 1953).
- SIMC. For a PI controller with a desired closed-loop time constant , and , with as a default (Skogestad 2003). The internal model control view gives rules of the same kind.
Pitfalls
- Normalizing by an unsettled value. Stopping the record early makes in L1 and L2 too small, so is too small and every level of L4 is reached too early. Record until the output has settled, and use the settled value in L1 and L2.
- The tangent on raw noisy samples. The steepest chord of L7 on noisy data is a noise spike. Filter the samples or fit a smooth curve before L7, or use the two-point lines L4 to L6.
- Measuring from the wrong origin. L4 and L8 must measure time from the moment of the step. Measuring it from the start of the record adds the idle time before the step to .
- A step that is too small or too large. A small step drowns in noise and a large one drives the process out of its linear range. Choose in L1 so that the output change is several times the noise level, and check that a step of the opposite sign gives a similar .
- Disturbances during the test. A load change during the recording is read as part of the response. Repeat the test, and in L1 and L2 use only records where the output starts from a steady value.
Implementations
Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.
# SPDX-License-Identifier: MIT
# Shared helper of the control pages of https://algorithm.tamnd.com/. The authors are listed in prov/.
"""Fixed step simulation for the control pages.
Conventions:
- A continuous-time system is a function f(t, x, u) that returns x' as a
flat list. x is a flat list. u is whatever the control returns (a flat
list, a scalar, or None when there is no control).
- A discrete-time system is a function F(k, x, u) that returns x_(k+1).
- The integrator is the classical 4th order Runge-Kutta method with a fixed
step dt. The input is held constant over each step.
- Time t_k = t0 + k dt is computed by multiplication, not by summation.
- The number of steps round((t1 - t0) / dt) and the hold ratio
round(hold / dt) must match the real ratios within 1e-9 (relative), or
ValueError is raised.
This file holds no controller or observer of a control page.
"""
__all__ = ["rk4_step", "simulate", "simulate_discrete", "lti"]
def _axpy(a, x, y):
return [yi + a * xi for xi, yi in zip(x, y)]
def rk4_step(f, t, x, u, dt):
"""Return x(t + dt) after one RK4 step of x' = f(t, x, u), u held constant."""
k1 = f(t, x, u)
k2 = f(t + dt / 2, _axpy(dt / 2, k1, x), u)
k3 = f(t + dt / 2, _axpy(dt / 2, k2, x), u)
k4 = f(t + dt, _axpy(dt, k3, x), u)
return [xi + dt / 6 * (a + 2 * b + 2 * c + d) for xi, a, b, c, d in zip(x, k1, k2, k3, k4)]
def _ratio(num, den, what):
r = round(num / den)
if abs(r * den - num) > 1e-9 * max(abs(num), abs(den)):
raise ValueError(what + " is not an integer multiple of dt")
return r
def simulate(f, x0, t0, t1, dt, control=None, hold=None):
"""Simulate x' = f(t, x, u) from x(t0) = x0 to t1 with RK4 and step dt.
control(t, x) gives the input. It is called at each sampling instant
t0 + j hold, and its value is held (zero-order hold) until the next
instant. hold must be an integer multiple of dt; hold = None means
hold = dt. With control = None the input is None.
Returns (ts, xs, us): ts has N + 1 times, xs the N + 1 states, and us the
N inputs, where us[k] acts on [ts[k], ts[k + 1]].
"""
if dt <= 0:
raise ValueError("dt must be positive")
n_steps = _ratio(t1 - t0, dt, "t1 - t0")
if n_steps < 0:
raise ValueError("t1 is before t0")
r = 1 if hold is None else _ratio(hold, dt, "hold")
if r < 1:
raise ValueError("hold must be at least dt")
x = list(x0)
ts = [t0]
xs = [list(x)]
us = []
u = None
for k in range(n_steps):
t = t0 + k * dt
if control is not None and k % r == 0:
u = control(t, list(x))
x = rk4_step(f, t, x, u, dt)
ts.append(t0 + (k + 1) * dt)
xs.append(x)
us.append(u)
return ts, xs, us
def simulate_discrete(F, x0, N, control=None):
"""Run x_(k+1) = F(k, x_k, u_k) for k = 0, ..., N - 1.
u_k = control(k, x_k), or None if control is None. Returns (xs, us) with
N + 1 states and N inputs. Exact if F and control are exact.
"""
x = list(x0)
xs = [list(x)]
us = []
for k in range(N):
u = control(k, list(x)) if control is not None else None
x = list(F(k, x, u))
xs.append(x)
us.append(u)
return xs, us
def lti(A, B=None):
"""Return f(t, x, u) = A x + B u for the system x' = A x + B u.
u may be a flat list, a scalar (one input) or None (zero input). B may
be None for a system with no input.
"""
def f(t, x, u):
dx = [sum(a * xi for a, xi in zip(row, x)) for row in A]
if B is None or u is None:
return dx
uu = u if isinstance(u, (list, tuple)) else [u]
return [d + sum(b * ui for b, ui in zip(row, uu)) for d, row in zip(dx, B)]
return f
def _self_test():
import math
f = lti([[-1.0]])
_, xs, _ = simulate(f, [1.0], 0.0, 1.0, 0.01)
assert abs(xs[-1][0] - math.exp(-1.0)) < 1e-9
return True
if __name__ == "__main__":
_self_test()
process_reaction_curve.py 84 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/process-reaction-curve/. The authors are listed in prov/QACT9Z.jsonl.
"""Fit a first order plus dead time (FOPDT) model to a recorded step response.
The model is G(s) = K e^(-L s) / (T s + 1). Its unit step response is 0
for t < L and K (1 - e^(-(t - L) / T)) for t >= L.
Input: sample times t[0] < t[1] < ... (the step of size du is applied at
t[0]) and the output samples y[i], from rest at y[0] until the response has
settled at y[-1]. The normalized response is z = (y - y[0]) / (y[-1] - y[0]).
Two methods:
- "tangent": the line of largest slope through the curve (the tangent at the
inflection point). It meets z = 0 at t = L and z = 1 at t = L + T. The
slope is taken from the chords of the samples, so the result is exact for
a sampled FOPDT response whose dead time is a sample time, and close to
the tangent of a smooth curve when the sampling is fine.
- "two-point": the times t28 and t63 where z reaches 1 - e^(-1/3) (about
28.3 percent) and 1 - e^(-1) (about 63.2 percent), by linear
interpolation. Then T = 1.5 (t63 - t28) and L = t63 - T. These are the
exact values for a FOPDT response.
Precondition: at least 3 samples, strictly increasing times, du != 0, and
y[-1] != y[0]. z should rise monotonically; with noise the result is
sensitive, see the page.
Cost: O(n) for n samples, in the real RAM.
"""
import math
from typing import Sequence, Tuple
P28 = 1.0 - math.exp(-1.0 / 3.0)
P63 = 1.0 - math.exp(-1.0)
def _crossing(t: Sequence, z: Sequence, level: float) -> float:
"""First time where z reaches level, by linear interpolation."""
for i in range(1, len(z)):
if z[i] >= level:
if z[i] == z[i - 1]:
return t[i]
return t[i - 1] + (level - z[i - 1]) * (t[i] - t[i - 1]) / (z[i] - z[i - 1])
raise ValueError("the response never reaches the level")
def fit_fopdt(t: Sequence, y: Sequence, du: float,
method: str = "two-point") -> Tuple[float, float, float]:
"""Return (K, L, T) of the FOPDT model fitted to the step response."""
if len(t) != len(y) or len(t) < 3:
raise ValueError("need at least 3 samples of t and y")
if any(t[i] >= t[i + 1] for i in range(len(t) - 1)):
raise ValueError("times must be strictly increasing")
if du == 0 or y[-1] == y[0]:
raise ValueError("need a nonzero step and a nonzero change of the output")
k = (y[-1] - y[0]) / du # @line L1
z = [(v - y[0]) / (y[-1] - y[0]) for v in y] # @line L2
if method == "two-point": # @line L3
t28 = _crossing(t, z, P28) - t[0] # @line L4
t63 = _crossing(t, z, P63) - t[0]
tau = 1.5 * (t63 - t28) # @line L5
dead = t63 - tau # @line L6
elif method == "tangent":
best = None
for i in range(len(z) - 1): # @line L7
s = (z[i + 1] - z[i]) / (t[i + 1] - t[i])
if best is None or s > best[0]:
best = (s, 0.5 * (t[i] + t[i + 1]) - t[0], 0.5 * (z[i] + z[i + 1]))
s, tm, zm = best
if s <= 0:
raise ValueError("the normalized response does not rise")
dead = tm - zm / s # @line L8
tau = 1.0 / s # @line L9
else:
raise ValueError("method must be 'two-point' or 'tangent'")
return k, dead, tau # @line L10
def fopdt_step(k: float, dead: float, tau: float, t: float) -> float:
"""Unit step response of K e^(-L s) / (T s + 1) at time t."""
if t < dead:
return 0.0
return k * (1.0 - math.exp(-(t - dead) / tau))
test_process_reaction_curve.py 81 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/process-reaction-curve/. The authors are listed in prov/QACT9Z.jsonl.
import math
import random
from ctlsim import lti, simulate
from process_reaction_curve import fit_fopdt, fopdt_step
def _third_order(dt, t_end):
# 1/(s+1)^3 as three first order lags in series, unit step at t = 0.
a = [[-1.0, 0.0, 0.0], [1.0, -1.0, 0.0], [0.0, 1.0, -1.0]]
b = [[1.0], [0.0], [0.0]]
ts, xs, _ = simulate(lti(a, b), [0.0, 0.0, 0.0], 0.0, t_end, dt, control=lambda t, x: 1.0)
return ts, [x[2] for x in xs]
def test_worked_example_third_order_lag():
ts, y = _third_order(0.01, 20.0)
k, dead, tau = fit_fopdt(ts, y, 1.0, "tangent")
assert abs(k - 1.0) < 1e-5
assert abs(dead - 0.805472) < 1e-3 and abs(tau - 3.694528) < 1e-3
k, dead, tau = fit_fopdt(ts, y, 1.0, "two-point")
assert abs(dead - 1.149590) < 1e-3 and abs(tau - 2.108662) < 1e-3
def test_coarse_table_of_the_page():
ts = [0.5 * i for i in range(41)]
y = [1 - math.exp(-t) * (1 + t + t * t / 2) for t in ts]
_, dead, tau = fit_fopdt(ts, y, 1.0, "two-point")
assert abs(dead - 1.1387) < 1e-4 and abs(tau - 2.1315) < 1e-4
_, dead, tau = fit_fopdt(ts, y, 1.0, "tangent")
assert abs(dead - 0.7832) < 1e-4 and abs(tau - 3.7633) < 1e-4
def test_random_fopdt_responses_are_recovered():
# Oracle: the closed form FOPDT step response. The dead time is a sample
# time, so the chord tangent gives L exactly and T = h / (1 - e^(-h/T)).
rng = random.Random(42)
for _ in range(500):
k = rng.choice((-1, 1)) * rng.uniform(0.2, 5.0)
tau = rng.uniform(0.5, 5.0)
h = tau / 20
dead = h * rng.randint(0, 40)
du = rng.choice((-1, 1)) * rng.uniform(0.5, 2.0)
y0 = rng.uniform(-3, 3)
ts = [h * i for i in range(int((dead + 16 * tau) / h) + 1)]
y = [y0 + du * fopdt_step(k, dead, tau, t) for t in ts]
k2, l2, t2 = fit_fopdt(ts, y, du, "two-point")
assert abs(k2 - k) < 1e-5 * abs(k)
assert abs(l2 - dead) < 0.01 * tau and abs(t2 - tau) < 0.01 * tau
k3, l3, t3 = fit_fopdt(ts, y, du, "tangent")
assert abs(l3 - dead) < 1e-9 * (1 + dead)
assert abs(t3 - h / (1 - math.exp(-h / tau))) < 1e-5 * tau
def test_negative_gain_and_offset():
ts = [0.1 * i for i in range(301)]
y = [2 + 0.5 * fopdt_step(-6, 1.0, 2.0, t) for t in ts]
k, dead, tau = fit_fopdt(ts, y, 0.5, "two-point")
assert abs(k + 6) < 1e-4 and abs(dead - 1.0) < 0.01 and abs(tau - 2.0) < 0.01
def test_no_dead_time():
ts = [0.05 * i for i in range(400)]
y = [fopdt_step(1.0, 0.0, 1.0, t) for t in ts]
_, dead, _ = fit_fopdt(ts, y, 1.0, "tangent")
assert abs(dead) < 1e-12
def test_bad_arguments():
ts = [0.0, 1.0, 2.0, 3.0]
y = [0.0, 0.5, 0.9, 1.0]
for args in ((ts, y, 0.0), (ts, [0.0] * 4, 1.0), (ts[:2], y[:2], 1.0),
([0.0, 2.0, 1.0, 3.0], y, 1.0), (ts, y, 1.0, "other")):
try:
fit_fopdt(*args)
except ValueError:
continue
raise AssertionError("expected ValueError for %r" % (args,))
History
Ziegler and Nichols in 1942 proposed to record the reaction curve of a process after a step of the controller output, to draw the tangent at its steepest point, and to read from it an apparent dead time and a reaction rate; their controller settings were given in terms of these two numbers. Cohen and Coon in 1953 derived settings from the first-order plus dead time model that the curve suggests. Two-point readings based on fixed percentages of the final change, and area methods, came later to make the reading less dependent on drawing a tangent. Skogestad's rules of 2003 are a modern set of tuning formulas built on the same three numbers.
References
- John G. Ziegler and Nathaniel B. Nichols, Optimum settings for automatic controllers, Transactions of the ASME 64, 759-768, 1942, the reaction curve method, the apparent dead time and the reaction rate read from the tangent at the steepest point, and the controller settings derived from them
- G. H. Cohen and G. A. Coon, Theoretical consideration of retarded control, Transactions of the ASME 75, 827-834, 1953, controller settings for a first-order process with dead time identified from the reaction curve
- Karl Johan Åström and Tore Hägglund, Advanced PID Control, ISA, 2006, the chapter on process models (step response methods, the tangent and area methods, and first-order plus dead time models)
- Sigurd Skogestad, Simple analytic rules for model reduction and PID controller tuning, Journal of Process Control 13(4), 291-309, 2003, the first-order plus dead time model and the SIMC tuning rules
Where this leads
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H1 sketch |
|
| Lean | L0 no Lean part |
|
| Code (python) | I0 not checked yet |
|
| Visual | V0 none |
|
| Writing | W2 basic |
|
Tag QACT9Z Edit this page History Report an error Markdown