Skip to content

Commit 42e6549

Browse files
committed
doc: internal details on the defect computation
1 parent 5fa54d2 commit 42e6549

File tree

2 files changed

+48
-22
lines changed

2 files changed

+48
-22
lines changed

src/controller/transcription.jl

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,7 +1149,7 @@ end
11491149
Compute vectors if `model` is a [`NonLinModel`](@ref) and for [`SingleShooting`](@ref).
11501150
11511151
The method mutates `Ŷ0`, `x̂0end`, `X̂0`, `Û0` and `K0` arguments. The augmented model of
1152-
[`f̂!`](@ref) and [`ĥ!`](@ref) is called recursively in a `for` loop.
1152+
[`f̂!`](@ref) and [`ĥ!`](@ref) is called recursively in a `for` loop:
11531153
"""
11541154
function predict!(
11551155
Ŷ0, x̂0end, X̂0, Û0, K0,
@@ -1302,12 +1302,14 @@ end
13021302
13031303
Nonlinear equality constrains for [`NonLinModel`](@ref) and [`MultipleShooting`](@ref).
13041304
1305-
The method mutates the `geq`, `X̂0`, `Û0` and `K0` vectors in argument. The defects are
1306-
updated with:
1305+
The method mutates the `geq`, `X̂0`, `Û0` and `K0` vectors in argument. The nonlinear
1306+
equality constraints `geq` only includes the state defects, computed with:
13071307
```math
13081308
\mathbf{ŝ}(k+1) = \mathbf{f̂}\Big(\mathbf{x̂_0}(k), \mathbf{u_0}(k), \mathbf{d_0}(k)\Big)
1309-
+ \text{TODO}
1309+
- \mathbf{x̂_0^†}(k+1)
13101310
```
1311+
in which ``\mathbf{x̂_0^†}`` is the augmented state extracted from the decision variables
1312+
`Z̃`, and ``\mathbf{f̂}``, the augmented state function defined in [`f̂!`](@ref).
13111313
"""
13121314
function con_nonlinprogeq!(
13131315
geq, X̂0, Û0, K0,
@@ -1346,21 +1348,33 @@ end
13461348
13471349
Nonlinear equality constrains for [`NonLinModel`](@ref) and [`TrapezoidalCollocation`](@ref).
13481350
1349-
The method mutates the `geq`, `X̂0`, `Û0` and `K0` vectors in argument. The deterministic
1350-
and stochastic states are handled separately since collocation methods require
1351-
continuous-time state-space models, and the stochastic model of the unmeasured disturbances
1352-
is discrete-time. Also note that operating points in `model` are typically zeros for
1353-
[`NonLinModel`](@ref), but they are handled rigorously here if it's not the case. It should
1354-
be noted that linearization of continuous-time dynamics at non-equilibrium points leads to:
1351+
The method mutates the `geq`, `X̂0`, `Û0` and `K0` vectors in argument.
1352+
1353+
The nonlinear equality constraints `geq` only includes the state defects. The deterministic
1354+
and stochastic states are handled separately since collocation methods require continuous-
1355+
time state-space models, and the stochastic model of the unmeasured disturbances
1356+
is discrete-time. The deterministic and stochastic defects are respectively computed with:
13551357
```math
1356-
\mathbf{ẋ_0}(t) ≈ \mathbf{A x_0}(t) + \mathbf{B_u u_0}(t) + \mathbf{B_d d_0}(t)
1358+
\begin{aligned}
1359+
\mathbf{s_d}(k+1) &= \mathbf{x_0}(k) - \mathbf{x_0^†}(k+1)
1360+
+ 0.5 T_s (\mathbf{k}_1 + \mathbf{k}_2) \\
1361+
\mathbf{s_s}(k+1) &= \mathbf{A_s x_s}(k) - \mathbf{x_s^†}(k+1)
1362+
\end{aligned}
13571363
```
1358-
as opposed to, for discrete-time models:
1364+
in which ``\mathbf{x_0^†}`` and ``\mathbf{x_s^†}`` are the deterministic and stochastic
1365+
states extracted from the decision variables `Z̃`. The ``\mathbf{k}`` coefficients are
1366+
evaluated from the continuous-time function `model.f!` and:
13591367
```math
1360-
\mathbf{x_0}(k+1) ≈ \mathbf{A x_0}(k) + \mathbf{B_u u_0}(k) + \mathbf{B_d d_0}(k)
1361-
+ \mathbf{f_{op}} - \mathbf{x_{op}}
1368+
\begin{aligned}
1369+
\mathbf{k}_1 &= \mathbf{f}\Big(\mathbf{x_0}(k), \mathbf{û_0}(k), \mathbf{d_0}(k) \Big) \\
1370+
\mathbf{k}_2 &= \mathbf{f}\Big(\mathbf{x_0^†}(k+1), \mathbf{û_0}(k), \mathbf{d_0}(k+1)\Big)
1371+
\end{aligned}
13621372
```
1363-
hence no need to add `model.fop` and subtract `model.xop` in the collocation equations.
1373+
and the input of the augmented model is:
1374+
```math
1375+
\mathbf{û_0}(k) = \mathbf{u_0}(k) + \mathbf{C_{s_u} x_s}(k)
1376+
```
1377+
the ``\mathbf{A_s, C_{s_u}}`` matrices are defined in [`init_estimstoch`](@ref) doc.
13641378
"""
13651379
function con_nonlinprogeq!(
13661380
geq, X̂0, Û0, K0,
@@ -1391,19 +1405,18 @@ function con_nonlinprogeq!(
13911405
sdnext, ssnext = @views ŝnext[1:nx], ŝnext[nx+1:end]
13921406
mul!(û0, Cs_u, xs) # ys_u = Cs_u*xs
13931407
û0 .+= u0 # û0 = u0 + ys_u
1394-
ẋ0, ẋ0next = @views k0[1:nx], k0[nx+1:2*nx]
1395-
# no need to handle model.fop and model.xop, see docstring:
1396-
model.f!(ẋ0, xd, û0, d0, p)
1397-
model.f!(ẋ0next, xdnext_Z̃, û0, d0next, p) # assuming ZOH on manipulated inputs u
1408+
k1, k2 = @views k0[1:nx], k0[nx+1:2*nx]
1409+
model.f!(k1, xd, û0, d0, p)
1410+
model.f!(k2, xdnext_Z̃, û0, d0next, p) # assuming ZOH on manipulated inputs u
13981411
xsnext = @views x̂0next[nx+1:end]
13991412
mul!(xsnext, As, xs)
1400-
sdnext .= @. xd - xdnext_Z̃ + (Ts/2)*(ẋ0 + ẋ0next)
1413+
sdnext .= @. xd - xdnext_Z̃ + (Ts/2)*(k1 + k2)
14011414
ssnext .= @. xsnext - xsnext_Z̃
14021415
x̂0 = x̂0next_Z̃ # using states in Z̃ for next iteration (allow parallel for)
14031416
d0 = d0next
14041417
end
14051418
return geq
14061419
end
14071420

1408-
"No equality constraints for other cases e.g. [`SingleShooting`](@ref), returns `geq` unchanged."
1421+
"No eq. constraints for other cases e.g. [`SingleShooting`](@ref), returns `geq` unchanged."
14091422
con_nonlinprogeq!(geq,_,_,_,::PredictiveController,::SimModel,::TranscriptionMethod,_,_)=geq

src/estimator/execute.jl

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,23 @@ function f̂!(x̂0next, û0, k0, estim::StateEstimator, model::SimModel, x̂0,
6565
return f̂!(x̂0next, û0, k0, model, estim.As, estim.Cs_u, estim.f̂op, estim.x̂op, x̂0, u0, d0)
6666
end
6767

68-
"""
68+
@doc raw"""
6969
f̂!(x̂0next, _ , _ , estim::StateEstimator, model::LinModel, x̂0, u0, d0) -> nothing
7070
7171
Use the augmented model matrices and operating points if `model` is a [`LinModel`](@ref).
72+
73+
# Extended Help
74+
!!! details "Extended Help"
75+
76+
This method computes:
77+
```math
78+
\begin{aligned}
79+
\mathbf{x̂_0}(k+1) &= \mathbf{Â x̂_0}(k) + \mathbf{B̂_u u_0}(k) + \mathbf{B̂_d d_0}(k)
80+
+ \mathbf{f̂_{op}} - \mathbf{x̂_{op}} \\
81+
\mathbf{ŷ_0}(k) &= \mathbf{Ĉ x̂_0}(k) + \mathbf{D̂_d d_0}(k)
82+
\end{aligned}
83+
```
84+
with the augmented matrices constructed by [`augment_model`](@ref).
7285
"""
7386
function f̂!(x̂0next, _ , _ , estim::StateEstimator, ::LinModel, x̂0, u0, d0)
7487
mul!(x̂0next, estim.Â, x̂0)

0 commit comments

Comments
 (0)