Skip to contents

Most tools of control engineering work on linear models. A nonlinear model x′=f(x,u,p,t),y=h(x,u,p,t) x' = f(x, u, p, t), \qquad y = h(x, u, p, t) with inputs uu and outputs yy is approximated near an operating point (x0,u0)(x_0, u_0) by the linear state space model δx′=Aδx+Bδu,δy=Cδx+Dδu, \delta x' = A\, \delta x + B\, \delta u, \qquad \delta y = C\, \delta x + D\, \delta u, with the Jacobians A=∂f/∂xA = \partial f / \partial x, B=∂f/∂uB = \partial f / \partial u, C=∂h/∂xC = \partial h / \partial x and D=∂h/∂uD = \partial h / \partial u. modelcompR computes these matrices from the symbolic model, as ModelingToolkit’s linearize does, and supports the analysis points of ModelingToolkit for the sensitivity functions of feedback loops. This article covers

  • linearize() with named inputs and outputs and operating points,
  • symbolic linearization, linearization functions and problems,
  • differential algebraic models,
  • using the state space matrices in base R: poles, frequency responses and step responses,
  • analysis points, sensitivity functions and loop transfer functions of a feedback loop built from components,
  • opening loops, and generating control functions.
suppressPackageStartupMessages(library(modelcompR))
t <- t_nounits
D <- D_nounits

Linearizing a model

A damped pendulum driven by a torque τ\tau has the angle θ\theta as its measured output. Inputs and outputs are marked with the input and output metadata of their variables, like [input = true] in Julia.

theta <- Variable("theta", t)
omega <- Variable("omega", t)
tau <- Variable("tau", t, input = TRUE)
angle <- Variable("angle", t, output = TRUE)
g <- Parameter("g", default = 9.81)
len <- Parameter("len", default = 1)
b <- Parameter("b", default = 0.5)
pend <- System(list(D(theta) ~ omega,
                    D(omega) ~ -g / len * sin(theta) - b * omega + tau,
                    angle ~ theta), t, name = "pend")

linearize(sys, inputs, outputs, op = ...) takes the unsimplified model. It compiles the model with the inputs kept as external signals, solves for a consistent operating point and evaluates the Jacobians there. The result holds the four matrices, the names of the states (the order of the unknowns of the simplified system), the simplified system sys and the operating point used, extras.

down <- linearize(pend, list(tau), list(angle), op = list(theta ~ 0, omega ~ 0, tau ~ 0))
down$states
#> [1] "theta(t)" "omega(t)"
down[c("A", "B", "C", "D")]
#> $A
#>       [,1] [,2]
#> [1,]  0.00  1.0
#> [2,] -9.81 -0.5
#> 
#> $B
#>      [,1]
#> [1,]    0
#> [2,]    1
#> 
#> $C
#>      [,1] [,2]
#> [1,]    1    0
#> 
#> $D
#>      [,1]
#> [1,]    0

The operating point is a list of equations variable ~ value; parameters not given take their defaults, and p gives further parameter values. Linearizing at the upright position gives an unstable model: one eigenvalue of AA is positive.

up <- linearize(pend, list(tau), list(angle), op = list(theta ~ pi, omega ~ 0, tau ~ 0))
up$A
#>      [,1] [,2]
#> [1,] 0.00  1.0
#> [2,] 9.81 -0.5
eigen(down$A)$values
#> [1] -0.25+3.122099i -0.25-3.122099i
eigen(up$A)$values
#> [1] -3.392053  2.892053

A different damping is passed with p:

linearize(pend, list(tau), list(angle), op = list(theta ~ 0, omega ~ 0, tau ~ 0),
          p = list(b ~ 2))$A
#>       [,1] [,2]
#> [1,]  0.00    1
#> [2,] -9.81   -2

Symbolic linearization

linearize_symbolic() returns the same matrices as symbolic expressions, along with the Jacobian blocks it is built from. It shows how the linearization depends on the operating point and the parameters.

sym <- linearize_symbolic(pend, list(tau), list(angle))
sym$A
#>      [,1]                 [,2]
#> [1,] 0                    1   
#> [2,] -g*cos(theta(t))/len -b
sym$B
#>      [,1]
#> [1,] 0   
#> [2,] 1

Operating points and repeated linearization

Each call of linearize() compiles the model. When the same model is linearized at many operating points, the compiled form can be reused.

Linearization functions

linearization_function(sys, inputs, outputs, op) (ModelingToolkit’s linearization_function) compiles once and returns lin_fun, a function of the numeric state, parameters and time that returns the Jacobian blocks, and the simplified system sys. linearize(ssys, lin_fun, op = ...) then linearizes without compiling again.

lf <- linearization_function(pend, list(tau), list(angle),
                             op = list(theta ~ 0, omega ~ 0, tau ~ 0))
lf$lin_fun
#> LinearizationFunction of system pend with 1 input(s) and 1 output(s)
linearize(lf$sys, lf$lin_fun, op = list(theta ~ 1))$A
#>           [,1] [,2]
#> [1,]  0.000000  1.0
#> [2,] -5.300366 -0.5

The blocks follow the semi-explicit form x′=f(x,z,u)x' = f(x, z, u), 0=g(x,z,u)0 = g(x, z, u), y=h(x,z,u)y = h(x, z, u), where zz are algebraic unknowns:

unknowns(lf$sys)
#> [[1]]
#> theta(t)
#> 
#> [[2]]
#> omega(t)
parameters(lf$sys)
#> [[1]]
#> len
#> 
#> [[2]]
#> g
#> 
#> [[3]]
#> b
#> 
#> [[4]]
#> tau(t)
blocks <- lf$lin_fun(c(0.5, 0), c(1, 9.81, 0.5, 0), 0)
names(blocks)
#>  [1] "f_x" "f_z" "g_x" "g_z" "f_u" "g_u" "h_x" "h_z" "h_u" "x"   "p"   "t"
blocks$f_x
#>           [,1] [,2]
#> [1,]  0.000000  1.0
#> [2,] -8.609085 -0.5
blocks$f_u
#>      [,1]
#> [1,]    0
#> [2,]    1

The numeric vectors are ordered as unknowns(lf$sys) and parameters(lf$sys); the input τ\tau has become the last parameter of the compiled system.

Linearization problems

LinearizationProblem() wraps a linearization function with a mutable operating point. Values are set with prob[x] <- value, and solve() linearizes at the current point; this mirrors LinearizationProblem and setu/setp in Julia.

lp <- LinearizationProblem(pend, list(tau), list(angle),
                           op = list(theta ~ 0, omega ~ 0, tau ~ 0))
lp
#> LinearizationProblem at time 0 of system pend
angles <- seq(0, pi, length.out = 5)
vapply(angles, function(a) {
  lp[theta] <- a
  solve(lp)$A[2, 1]
}, 0)
#> [1] -9.810000e+00 -6.936718e+00 -6.006893e-16  6.936718e+00  9.810000e+00

The entry A21=−(g/ℓ)cos⁡θA_{21} = -(g / \ell) \cos\theta changes sign at θ=π/2\theta = \pi / 2, where gravity stops pulling the pendulum back.

Operating points along a trajectory

LinearizationOpPoint(sol, t) takes the operating point from a simulation. With several times, linearize() returns one linearization per time, which is how gain scheduled controllers are designed.

sim <- ODEProblem(mtkcompile(pend, inputs = list(tau)),
                  list(theta ~ 2, omega ~ 0, tau ~ 0), c(0, 5))
psol <- solve(sim, Tsit5())
along <- linearize(pend, list(tau), list(angle), op = LinearizationOpPoint(psol, c(0, 2.5, 5)))
vapply(along, function(l) l$A[2, 1], 0)
#> [1]  4.082400 -5.737423 -9.809975
-9.81 * cos(psol(c(0, 2.5, 5))[, 1])
#> [1]  4.082400 -5.737423 -9.809975

Algebraic unknowns

For a model with algebraic equations, the state of ModelingToolkit’s linearization contains all unknowns of the simplified system, (x,z)(x, z), and A=(fxfzGfxGfz)A = \begin{pmatrix} f_x & f_z \\ G f_x & G f_z \end{pmatrix} with G=−gz−1gxG = -g_z^{-1} g_x. With eliminate_algebraic = TRUE the algebraic unknowns are eliminated through the implicit function theorem, z=−gz−1(gxx+guu)z = -g_z^{-1}(g_x x + g_u u), which gives the minimal state. In the example below zz cannot be solved for symbolically, so it stays an unknown after compilation.

w <- Variable("w", t)
z <- Variable("z", t, guess = 1)
v_in <- Variable("v_in", t, input = TRUE)
dae <- System(list(D(w) ~ -w + z + v_in, 0 ~ z^3 + z - w), t, name = "dae")
full <- linearize(dae, list(v_in), list(w), op = list(w ~ 2, v_in ~ 0))
full$states
#> [1] "w(t)" "z(t)"
full$A
#>       [,1] [,2]
#> [1,] -1.00 1.00
#> [2,] -0.25 0.25
reduced <- linearize(dae, list(v_in), list(w), op = list(w ~ 2, v_in ~ 0),
                     eliminate_algebraic = TRUE)
reduced$A
#>       [,1]
#> [1,] -0.75

At w=2w = 2 the constraint gives z=1z = 1, and dz/dw=1/(3z2+1)=1/4dz/dw = 1 / (3 z^2 + 1) = 1/4, so the reduced model has A=−1+1/4=−3/4A = -1 + 1/4 = -3/4.

Using the state space matrices

The matrices are plain R matrices, so base R covers the usual analyses.

Poles and stability

The eigenvalues of AA are the poles of the linear model. The hanging pendulum has a lightly damped pair with natural frequency g/ℓ\sqrt{g / \ell} and damping ratio b/(2g/ℓ)b / (2 \sqrt{g / \ell}):

ev <- eigen(down$A)$values
c(natural_frequency = Mod(ev[1]), damping_ratio = -Re(ev[1]) / Mod(ev[1]))
#> natural_frequency     damping_ratio 
#>        3.13209195        0.07981886
c(sqrt(9.81), 0.5 / (2 * sqrt(9.81)))
#> [1] 3.13209195 0.07981886

Frequency response

The transfer function G(s)=C(sI−A)−1B+DG(s) = C (sI - A)^{-1} B + D is evaluated at s=iωs = i \omega with solve():

freqresp <- function(lin, w) {
  vapply(w, function(wi) {
    as.complex(lin$C %*% solve(1i * wi * diag(nrow(lin$A)) - lin$A, lin$B) + lin$D)
  }, 0i)
}
wgrid <- 10^seq(-1, 2, length.out = 300)
G <- freqresp(down, wgrid)
plot(wgrid, 20 * log10(Mod(G)), log = "x", type = "l", xlab = "frequency (rad/s)",
     ylab = "magnitude (dB)", main = "Pendulum torque to angle")
abline(v = sqrt(9.81), lty = 3)

The resonance peak sits at the natural frequency.

Step response

For a stable AA, the response to a unit step in the input from the operating point is δx(t)=A−1(eAt−I)B\delta x(t) = A^{-1}(e^{At} - I) B. The matrix exponential follows from the eigendecomposition A=VΛV−1A = V \Lambda V^{-1} when AA is diagonalizable:

expm_eigen <- function(A, s) {
  e <- eigen(A)
  Re(e$vectors %*% diag(exp(e$values * s), nrow(A)) %*% solve(e$vectors))
}
step_response <- function(lin, times) {
  vapply(times, function(s) {
    x <- solve(lin$A, (expm_eigen(lin$A, s) - diag(nrow(lin$A))) %*% lin$B)
    as.numeric(lin$C %*% x + lin$D)
  }, 0)
}
tt <- seq(0, 10, length.out = 200)
plot(tt, step_response(down, tt), type = "l", xlab = "t", ylab = "angle",
     main = "Unit torque step")
abline(h = -down$C %*% solve(down$A, down$B), lty = 3)

The response settles at the static gain −CA−1B=ℓ/g-C A^{-1} B = \ell / g.

Changing coordinates

similarity_transform(lin, T) changes the state coordinates with x=Tx2x = T x_2, and reorder_unknowns(lin, old, new) permutes the states. The poles do not change:

Tm <- matrix(c(1, 1, 0, 1), 2)
tr <- similarity_transform(down, Tm)
tr$A
#>        [,1] [,2]
#> [1,]   1.00  1.0
#> [2,] -11.31 -1.5
eigen(tr$A)$values
#> [1] -0.25+3.122099i -0.25-3.122099i
reorder_unknowns(down, down$states, rev(down$states))$A
#>      [,1]  [,2]
#> [1,] -0.5 -9.81
#> [2,]  1.0  0.00

A feedback loop from components

Control systems are usually assembled from blocks connected through signal connectors, as in the ModelingToolkitStandardLibrary. The blocks below are defined with connector() and mtkmodel(), the counterparts of @connector and @mtkmodel: a first order plant P(s)=k/(Ts+1)P(s) = k / (T s + 1), a PI controller C(s)=kp+ki/sC(s) = k_p + k_i / s, and an error block e=r−ye = r - y with the reference rr as a parameter.

RealInput <- connector("RealInput", variables = list(list("u(t)", input = TRUE)))
RealOutput <- connector("RealOutput", variables = list(list("u(t)", output = TRUE)))
SISO <- mtkmodel("SISO", variables = list("u(t)", "y(t)"),
  components = list(input = quote(RealInput()), output = quote(RealOutput())),
  equations = quote(list(u ~ input$u, y ~ output$u)))
FirstOrder <- mtkmodel("FirstOrder", extend = list(siso = quote(SISO())),
  parameters = list("T = 1", "k = 1"), variables = list("x(t) = 0"),
  equations = quote(list(D(x) ~ (k * u - x) / T, y ~ x)))
PI <- mtkmodel("PI", extend = list(siso = quote(SISO())),
  parameters = list("kp = 1", "ki = 1"), variables = list("xi(t) = 0"),
  equations = quote(list(D(xi) ~ u, y ~ kp * u + ki * xi)))
ErrorBlock <- mtkmodel("ErrorBlock", parameters = list("r = 0"),
  components = list(input = quote(RealInput()), output = quote(RealOutput())),
  equations = quote(list(output$u ~ r - input$u)))

Analysis points

An analysis point is a named connection: connect(out, "name", in) creates an AnalysisPoint that behaves like an ordinary connection in simulation, and marks the signal for linear analysis. It is referred to as sys$name on the unsimplified model. Here every connection of the loop is an analysis point: y (the measurement), e (the error) and u (the control signal).

P <- FirstOrder(name = "P", k = 2, T = 0.5)
C <- PI(name = "C", kp = 1, ki = 2)
E <- ErrorBlock(name = "E")
loop <- System(list(connect(P$output, "y", E$input),
                    connect(E$output, "e", C$input),
                    connect(C$output, "u", P$input)),
               t, systems = list(P, C, E), name = "loop")
loop$u
#> AnalysisPoint(loop₊u from loop₊C₊output₊u(t) to loop₊P₊input₊u(t))

The PI zero at −ki/kp=−2-k_i / k_p = -2 cancels the plant pole at −1/T=−2-1 / T = -2, so the loop transfer function is PC=4/sP C = 4 / s and the closed loop from reference to output is 4/(s+4)4 / (s + 4). A simulation of a unit reference step confirms it:

cl_sys <- mtkcompile(loop)
cl_prob <- ODEProblem(cl_sys, list(E$r ~ 1), c(0, 2))
cl_sol <- solve(cl_prob, Tsit5(), reltol = 1e-8, abstol = 1e-8, saveat = 0.5)
rbind(simulated = cl_sol[P$y], exact = 1 - exp(-4 * cl_sol$t))
#>           [,1]      [,2]      [,3]      [,4]      [,5]
#> simulated    0 0.8646647 0.9816844 0.9975212 0.9996645
#> exact        0 0.8646647 0.9816844 0.9975212 0.9996645

Sensitivity functions

The classical loop functions at a point of the loop are

  • the sensitivity function S=1/(1+PC)S = 1 / (1 + PC), the transfer from a disturbance added at the point to the signal there,
  • the complementary sensitivity function T=PC/(1+PC)=1−ST = PC / (1 + PC) = 1 - S,
  • the loop transfer function LL, the response around the opened loop.

get_sensitivity(sys, ap), get_comp_sensitivity(sys, ap) and get_looptransfer(sys, ap) linearize them at the analysis point ap, as in ModelingToolkit; they return the same structure as linearize().

S <- get_sensitivity(loop, loop$u, verbose = FALSE)
Tc <- get_comp_sensitivity(loop, loop$u, verbose = FALSE)
L <- get_looptransfer(loop, loop$u, verbose = FALSE)
S[c("A", "B", "C", "D")]
#> $A
#>      [,1] [,2]
#> [1,]   -6    8
#> [2,]   -1    0
#> 
#> $B
#>      [,1]
#> [1,]    4
#> [2,]    0
#> 
#> $C
#>      [,1] [,2]
#> [1,]   -1    2
#> 
#> $D
#>      [,1]
#> [1,]    1

The state space realizations keep the cancelled plant pole, so the eigenvalues of S$A are −4-4 and −2-2; the transfer functions themselves are S=s/(s+4)S = s / (s + 4), T=4/(s+4)T = 4 / (s + 4) and, with the negative feedback sign that ModelingToolkit includes, L=−4/sL = -4 / s. Evaluating them on the imaginary axis checks this and the identity S+T=1S + T = 1:

s_test <- c(0.5, 2, 10)
rbind(S = freqresp(S, s_test), S_exact = 1i * s_test / (1i * s_test + 4),
      T = freqresp(Tc, s_test), L = freqresp(L, s_test), L_exact = -4 / (1i * s_test))
#>                          [,1]     [,2]                [,3]
#> S       0.01538462+0.1230769i 0.2+0.4i 0.862069+0.3448276i
#> S_exact 0.01538462+0.1230769i 0.2+0.4i 0.862069+0.3448276i
#> T       0.98461538-0.1230769i 0.8-0.4i 0.137931-0.3448276i
#> L       0.00000000+8.0000000i 0.0+2.0i 0.000000+0.4000000i
#> L_exact 0.00000000+8.0000000i 0.0+2.0i 0.000000+0.4000000i
max(Mod(freqresp(S, wgrid) + freqresp(Tc, wgrid) - 1))
#> [1] 0

The peak of |S||S| is a standard robustness measure: its inverse is the distance from the Nyquist curve to the critical point. For this loop |S||S| rises monotonically toward 1 at high frequency and never exceeds it, the best possible value.

Sw <- freqresp(S, wgrid)
Tw <- freqresp(Tc, wgrid)
plot(wgrid, 20 * log10(Mod(Sw)), log = "x", type = "l", ylim = c(-40, 5),
     xlab = "frequency (rad/s)", ylab = "magnitude (dB)")
lines(wgrid, 20 * log10(Mod(Tw)), lty = 2)
legend("bottomright", c("S", "T"), lty = 1:2, bty = "n")

max(Mod(Sw))
#> [1] 0.999201

Several analysis points at once give a multivariable function:

get_sensitivity(loop, list(loop$u, loop$y), verbose = FALSE)$D
#>      [,1] [,2]
#> [1,]    1   -1
#> [2,]    0    1

get_sensitivity_function(), get_comp_sensitivity_function() and get_looptransfer_function() return the linearization function and the simplified system instead, for repeated linearization:

Sf <- get_sensitivity_function(loop, loop$u, verbose = FALSE)
Sf$lin_fun
#> LinearizationFunction of system loop with 1 input(s) and 1 output(s)

Linearizing between analysis points

Analysis points can be used as the inputs and outputs of linearize(). An input point adds a perturbation to its signal, an output point reads the signal. From the error point to the measurement point the transfer is the complementary sensitivity, which is also the reference to output response:

ry <- linearize(loop, loop$e, loop$y, verbose = FALSE)
ry[c("A", "B", "C", "D")]
#> $A
#>      [,1] [,2]
#> [1,]   -6    8
#> [2,]   -1    0
#> 
#> $B
#>      [,1]
#> [1,]    4
#> [2,]    1
#> 
#> $C
#>      [,1] [,2]
#> [1,]    1    0
#> 
#> $D
#>      [,1]
#> [1,]    0
step_response(ry, c(0.25, 0.5, 1))
#> [1] 0.6321206 0.8646647 0.9816844
1 - exp(-4 * c(0.25, 0.5, 1))
#> [1] 0.6321206 0.8646647 0.9816844

Opening loops

loop_openings breaks connections before linearizing. Opening the loop at the measurement and linearizing from the control signal to the measurement gives the plant alone, P(s)=4/(s+2)P(s) = 4 / (s + 2), together with the controller state that no longer affects the output:

plant <- linearize(loop, loop$u, loop$y, loop_openings = list(loop$y), verbose = FALSE)
plant[c("A", "B", "C", "D")]
#> $A
#>      [,1] [,2]
#> [1,]   -2    8
#> [2,]    0    0
#> 
#> $B
#>      [,1]
#> [1,]    4
#> [2,]    0
#> 
#> $C
#>      [,1] [,2]
#> [1,]    1    0
#> 
#> $D
#>      [,1]
#> [1,]    0
freqresp(plant, 1)
#> [1] 1.6-0.8i
4 / (1i + 2)
#> [1] 1.6-0.8i

open_loop(sys, ap) performs the same transformation on the model and returns it, together with du, the new input that now drives the destinations of the point, and u, the variable that drove it:

ol <- open_loop(loop, loop$y)
ol$du
#> [[1]]
#> E₊input₊u(t)
ol$u
#> P₊output₊u(t)
ol_lin <- linearize(ol$sys, ol$du, list(ol$u), verbose = FALSE)
freqresp(ol_lin, 1)
#> [1] 0+4i

The transfer from the measurement input of the error block back to the plant output is −PC=−4/s-PC = -4 / s, the loop transfer function at yy.

linearization_ap_transform() exposes the transformation that linearize() applies to analysis point inputs and outputs, adding the perturbation variables:

tr_ap <- linearization_ap_transform(loop, loop$u, list(), list())
tr_ap$inputs
#> [[1]]
#> d_u(t)

Hierarchical models

Analysis points of subsystems are reached through the hierarchy, and their names are namespaced like any other variable:

plant_room <- System(list(), t, systems = list(loop), name = "room")
room_ap <- plant_room$loop$u
room_ap
#> AnalysisPoint(room₊loop₊u from room₊loop₊C₊output₊u(t) to room₊loop₊P₊input₊u(t))
get_sensitivity(plant_room, room_ap, verbose = FALSE)$D
#>      [,1]
#> [1,]    1

A string name of the point, relative to the model, works as well: get_sensitivity(plant_room, "loop₊u").

Control functions

generate_control_function(sys, inputs) compiles a model with the given inputs and returns the function f(x,u,p,t)f(x, u, p, t) of x′=f(x,u,p,t)x' = f(x, u, p, t), the unknowns dvs and parameters ps that order its arguments, and the compiled input output system io_sys. It is the starting point for model predictive control and state estimation, where the dynamics are evaluated directly.

cf <- generate_control_function(pend, list(tau))
cf$dvs
#> [[1]]
#> theta(t)
#> 
#> [[2]]
#> omega(t)
cf$ps
#> [[1]]
#> len
#> 
#> [[2]]
#> g
#> 
#> [[3]]
#> b
pvals <- unname(c(len = 1, g = 9.81, b = 0.5)[vapply(cf$ps, format, "")])
cf$f(c(0.1, 0), 0.2, pvals, 0)
#> [1]  0.0000000 -0.7793658

calculate_control_jacobian() gives the symbolic Jacobian of the dynamics with respect to the inputs, and unbound_inputs() lists inputs that no equation drives:

calculate_control_jacobian(cf$io_sys)
#> Matrix(2, 1)
#> [0]
#> [1]
unbound_inputs(pend)
#> [[1]]
#> tau(t)

Summary

Task modelcompR ModelingToolkit
linearize at an operating point linearize(sys, inputs, outputs, op = ) linearize
symbolic matrices linearize_symbolic() linearize_symbolic
reuse the compiled model linearization_function(), LinearizationProblem() the same
operating points from a simulation LinearizationOpPoint() the same
analysis points connect(out, "name", in), AnalysisPoint() connect(out, :name, in)
loop functions get_sensitivity(), get_comp_sensitivity(), get_looptransfer() the same
open a loop loop_openings =, open_loop() the same
coordinates similarity_transform(), reorder_unknowns() the same
dynamics for control generate_control_function() the same