@@ -157,9 +157,9 @@ julia> linearize!(linmodel, model, x=[20.0], u=[0.0]); linmodel.A
157157```
158158"""
159159function linearize! (
160- linmodel:: LinModel{NT} , model:: SimModel ;
160+ linmodel:: LinModel , model:: SimModel ;
161161 x= (model. buffer. x.= model. x0.+ model. xop), u= model. uop, d= model. dop
162- ) where NT <: Real
162+ )
163163 nonlinmodel = model
164164 buffer = nonlinmodel. buffer
165165 # --- remove the operating points of the nonlinear model (typically zeros) ---
@@ -172,16 +172,19 @@ function linearize!(
172172 # --- compute the nonlinear model output at operating points ---
173173 x0next, y0 = linmodel. buffer. x, linmodel. buffer. y
174174 h! (y0, nonlinmodel, x0, d0, model. p)
175- y = y0
176- y . = y0 .+ nonlinmodel . yop
175+ y0 .+ = nonlinmodel . yop
176+ y = y0
177177 # --- compute the nonlinear model next state at operating points ---
178178 f! (x0next, k0, nonlinmodel, x0, u0, d0, model. p)
179- xnext = x0next
180- xnext .= x0next .+ nonlinmodel. fop .- nonlinmodel. xop
179+ x0next .+ = nonlinmodel. fop
180+ xnext = x0next # xnext = f(x0,u0,d0) + fop - xop + xop = f(x0,u0,d0) + fop
181+ # --- recompute x since it was modified in buffer.x ---
182+ x0 .+ = nonlinmodel. xop
183+ x = x0
181184 # --- modify the linear model operating points ---
182185 linmodel. uop .= u
183- linmodel. yop .= y
184186 linmodel. dop .= d
187+ linmodel. yop .= y
185188 linmodel. xop .= x
186189 linmodel. fop .= xnext
187190 # --- reset the state of the linear model ---
@@ -190,14 +193,14 @@ function linearize!(
190193end
191194
192195" Call `linfunc!` function to compute the Jacobians of `model` at the linearization point."
193- function linearize_core! (linmodel:: LinModel , model:: SimModel , x0, u0, d0 )
194- x0next, y0 = linmodel. buffer. x, linmodel. buffer. y
196+ function linearize_core! (linmodel:: LinModel , model:: SimModel , x, u, d )
197+ xnext, y = linmodel. buffer. x, linmodel. buffer. y
195198 A, Bu, C, Bd, Dd = linmodel. A, linmodel. Bu, linmodel. C, linmodel. Bd, linmodel. Dd
196- cst_x = Constant (x0 )
197- cst_u = Constant (u0 )
198- cst_d = Constant (d0 )
199+ cst_x = Constant (x )
200+ cst_u = Constant (u )
201+ cst_d = Constant (d )
199202 backend = model. jacobian
200- model. linfunc! (x0next, y0 , A, Bu, C, Bd, Dd, backend, x0, u0, d0 , cst_x, cst_u, cst_d)
203+ model. linfunc! (xnext, y , A, Bu, C, Bd, Dd, backend, x, u, d , cst_x, cst_u, cst_d)
201204 return nothing
202205end
203206
0 commit comments