Skip to content

Commit 5f86a30

Browse files
committed
debug linearization
1 parent 2ed9d57 commit 5f86a30

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

src/model/linearization.jl

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,13 @@ julia> linmodel.A
3939
"""
4040
function linearize(model::NonLinModel; x=model.x, u=model.uop, d=model.dop)
4141
u0, d0 = u - model.uop, d - model.dop
42-
y = model.h(x, d0) + model.yop
43-
A = ForwardDiff.jacobian(x -> model.f(x, u0, d0), x)
44-
Bu = ForwardDiff.jacobian(u0 -> model.f(x, u0, d0), u0)
45-
Bd = ForwardDiff.jacobian(d0 -> model.f(x, u0, d0), d0)
46-
C = ForwardDiff.jacobian(x -> model.h(x, d0), x)
47-
Dd = ForwardDiff.jacobian(d0 -> model.h(x, d0), d0)
42+
xnext, y = similar(x), similar(model.yop)
43+
y = model.h!(y, x, d0) .+ model.yop
44+
A = ForwardDiff.jacobian((xnext, x) -> model.f!(xnext, x, u0, d0), xnext, x)
45+
Bu = ForwardDiff.jacobian((xnext, u0) -> model.f!(xnext, x, u0, d0), xnext, u0)
46+
Bd = ForwardDiff.jacobian((xnext, d0) -> model.f!(xnext, x, u0, d0), xnext, d0)
47+
C = ForwardDiff.jacobian((y, x) -> model.h!(y, x, d0), y, x)
48+
Dd = ForwardDiff.jacobian((y, d0) -> model.h!(y, x, d0), y, d0)
4849
linmodel = LinModel(A, Bu, C, Bd, Dd, model.Ts)
4950
setop!(linmodel, uop=u, yop=y, dop=d)
5051
return linmodel

test/test_sim_model.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ end
112112
@test nonlinmodel1.nu == 2
113113
@test nonlinmodel1.nd == 0
114114
@test nonlinmodel1.ny == 2
115-
@test nonlinmodel1.f([0,0],[0,0],[1]) zeros(2,)
116-
@test nonlinmodel1.h([0,0],[1]) zeros(2,)
115+
@test nonlinmodel1.f!([0,0],[0,0],[0,0],[1]) zeros(2,)
116+
@test nonlinmodel1.h!([0,0],[0,0],[1]) zeros(2,)
117117

118118
linmodel2 = LinModel(sys,Ts,i_d=[3])
119119
f2(x,u,d) = linmodel2.A*x + linmodel2.Bu*u + linmodel2.Bd*d
@@ -124,8 +124,8 @@ end
124124
@test nonlinmodel2.nu == 2
125125
@test nonlinmodel2.nd == 1
126126
@test nonlinmodel2.ny == 2
127-
@test nonlinmodel2.f([0,0,0,0],[0,0],[0]) zeros(4,)
128-
@test nonlinmodel2.h([0,0,0,0],[0]) zeros(2,)
127+
@test nonlinmodel2.f!([0,0,0,0],[0,0,0,0],[0,0],[0]) zeros(4,)
128+
@test nonlinmodel2.h!([0,0],[0,0,0,0],[0]) zeros(2,)
129129

130130
nonlinemodel3 = NonLinModel{Float32}(f2,h2,Ts,2,4,2,1)
131131
@test isa(nonlinemodel3, NonLinModel{Float32})

0 commit comments

Comments
 (0)