Skip to content

Commit eed89eb

Browse files
authored
Merge pull request #168 from JuliaControl/doc_correction
Minor doc corrections + allow `LinModel` linearization
2 parents 13536cc + b93d7e7 commit eed89eb

File tree

3 files changed

+39
-11
lines changed

3 files changed

+39
-11
lines changed

src/controller/transcription.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ They are defined in the Extended Help section.
343343
\mathbf{E} &= [\begin{smallmatrix}\mathbf{0} & \mathbf{E^†} \end{smallmatrix}] \\
344344
\mathbf{E^†} &= \text{diag}\mathbf{(Ĉ,Ĉ,...,Ĉ)} \\
345345
\mathbf{J} &= \text{diag}\mathbf{(D̂_d,D̂_d,...,D̂_d)} \\
346-
\mathbf{ex̂} &= [\begin{smallmatrix}\mathbf{0} & \mathbf{I}\end{smallmatrix}]
346+
\mathbf{e_x̂} &= [\begin{smallmatrix}\mathbf{0} & \mathbf{I}\end{smallmatrix}]
347347
\end{aligned}
348348
```
349349
"""

src/model/linearization.jl

+27-10
Original file line numberDiff line numberDiff line change
@@ -174,22 +174,15 @@ function linearize!(
174174
linmodel::LinModel{NT}, model::SimModel; x=model.x0+model.xop, u=model.uop, d=model.dop
175175
) where NT<:Real
176176
nonlinmodel = model
177-
buffer, linbuffer = nonlinmodel.buffer, nonlinmodel.linbuffer
177+
buffer = nonlinmodel.buffer
178178
# --- remove the operating points of the nonlinear model (typically zeros) ---
179179
x0, u0, d0 = buffer.x, buffer.u, buffer.d
180+
x0 .= x .- nonlinmodel.xop
180181
u0 .= u .- nonlinmodel.uop
181182
d0 .= d .- nonlinmodel.dop
182-
x0 .= x .- nonlinmodel.xop
183183
# --- compute the Jacobians at linearization points ---
184184
xnext0::Vector{NT}, y0::Vector{NT} = linmodel.buffer.x, linmodel.buffer.y
185-
linbuffer.x .= x0
186-
linbuffer.u .= u0
187-
linbuffer.d .= d0
188-
jacobian!(linmodel.A, linbuffer.buffer_f_at_u_d, xnext0, x0)
189-
jacobian!(linmodel.Bu, linbuffer.buffer_f_at_x_d, xnext0, u0)
190-
jacobian!(linmodel.Bd, linbuffer.buffer_f_at_x_u, xnext0, d0)
191-
jacobian!(linmodel.C, linbuffer.buffer_h_at_d, y0, x0)
192-
jacobian!(linmodel.Dd, linbuffer.buffer_h_at_x, y0, d0)
185+
get_jacobians!(linmodel, xnext0, y0, nonlinmodel, x0, u0, d0)
193186
# --- compute the nonlinear model output at operating points ---
194187
xnext0, y0 = linmodel.buffer.x, linmodel.buffer.y
195188
h!(y0, nonlinmodel, x0, d0, model.p)
@@ -208,4 +201,28 @@ function linearize!(
208201
# --- reset the state of the linear model ---
209202
linmodel.x0 .= 0 # state deviation vector is always x0=0 after a linearization
210203
return linmodel
204+
end
205+
206+
"Compute the 5 Jacobians of `model` at the linearization point and write them in `linmodel`."
207+
function get_jacobians!(linmodel::LinModel, xnext0, y0, model::SimModel, x0, u0, d0)
208+
linbuffer = model.linbuffer # a LinearizationBuffer object
209+
linbuffer.x .= x0
210+
linbuffer.u .= u0
211+
linbuffer.d .= d0
212+
jacobian!(linmodel.A, linbuffer.buffer_f_at_u_d, xnext0, x0)
213+
jacobian!(linmodel.Bu, linbuffer.buffer_f_at_x_d, xnext0, u0)
214+
jacobian!(linmodel.Bd, linbuffer.buffer_f_at_x_u, xnext0, d0)
215+
jacobian!(linmodel.C, linbuffer.buffer_h_at_d, y0, x0)
216+
jacobian!(linmodel.Dd, linbuffer.buffer_h_at_x, y0, d0)
217+
return nothing
218+
end
219+
220+
"Copy the state-space matrices of `model` to `linmodel` if `model` is already linear."
221+
function get_jacobians!(linmodel::LinModel, _ , _ , model::LinModel, _ , _ , _)
222+
linmodel.A .= model.A
223+
linmodel.Bu .= model.Bu
224+
linmodel.C .= model.C
225+
linmodel.Bd .= model.Bd
226+
linmodel.Dd .= model.Dd
227+
return nothing
211228
end

test/1_test_sim_model.jl

+11
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,17 @@ end
118118
@test_throws DimensionMismatch evaloutput(linmodel1, zeros(1))
119119
end
120120

121+
@testitem "LinModel linearization" setup=[SetupMPCtests] begin
122+
using .SetupMPCtests
123+
linmodel1 = LinModel(sys, Ts, i_d=[3])
124+
linmodel2 = linearize(linmodel1, x=[1,2,3,4], u=[5,6], d=[7])
125+
@test linmodel2.A linmodel1.A
126+
@test linmodel2.Bu linmodel1.Bu
127+
@test linmodel2.Bd linmodel1.Bd
128+
@test linmodel2.C linmodel1.C
129+
@test linmodel2.Dd linmodel1.Dd
130+
end
131+
121132
@testitem "LinModel real time simulations" setup=[SetupMPCtests] begin
122133
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
123134
linmodel1 = LinModel(tf(2, [10, 1]), 0.25)

0 commit comments

Comments
 (0)