Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions ext/ControlSystemIdentificationLSOptExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ function _inner_pem(
function get_ukf(px0::Vector{T}) where {T}
pᵢ = px0[1:length(p0)]
x0i = optimize_x0 ? px0[x0inds] : x0
UnscentedKalmanFilter(discrete_dynamics, measurement, R1, R2, SimpleMvNormal(T.(x0i), T.(R1mut)); ny, nu, p=pᵢ)
UnscentedKalmanFilter(discrete_dynamics, measurement, R1, R2, SimpleMvNormal(T.(x0i), T.(R1mut)); ny, nu, p=pᵢ, Ts)
end

function residuals!(ϵ, px0)
Expand Down Expand Up @@ -207,7 +207,7 @@ function ControlSystemIdentification.predict(model::NonlinearPredictionErrorMode
h == 1 || h == 0 || throw(ArgumentError("Only h=1 is supported at the moment"))
reset!(model.ukf; x0)
sol = forward_trajectory(model.ukf, d, p)
yh = model.ukf.measurement.(h == 1 ? sol.x : sol.xt, eachcol(d.u), Ref(p), 1:length(d))
yh = model.ukf.measurement.(h == 1 ? sol.x : sol.xt, eachcol(d.u), Ref(p), range(0, step = d.Ts, length = length(d)))
reduce(hcat, yh)
end

Expand Down
22 changes: 22 additions & 0 deletions test/test_nonlinear_pem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,25 @@ modelγ = ControlSystemIdentification.nonlinear_pem(d, discrete_dynamics, measur
@test 0.6 < mean_est_3[1] / emp_var_3[1] < 1.7
@test 0.6 < mean_est_3[2] / emp_var_3[2] < 1.7
end

@testset "nonlinear_pem with time-dependent dynamics and Ts ≠ 1" begin
# The dynamics depend on t through a known forcing sin(t); the estimate is correct only if the
# filter receives the sample interval of the data.
Tsv = 0.1
ctd(x, u, p, t) = SA[-p[1]*x[1] + p[2]*u[1] + sin(t)]
dyn = SeeToDee.Rk4(ctd, Tsv)
meas(x, u, p, t) = SA[x[1]]
pv = [0.8, 1.5]
uv = [SA[sign(sin(0.3k))] for k in 0:599]
tv = range(0, step = Tsv, length = length(uv))
xv = [SA[0.0]]
for k in 1:length(uv)-1
push!(xv, dyn(xv[end], uv[k], pv, tv[k]))
end
yv = meas.(xv, uv, Ref(pv), tv)
dv = iddata(reduce(hcat, yv), reduce(hcat, uv), Tsv)
modelv = ControlSystemIdentification.nonlinear_pem(dv, dyn, meas, [0.5, 1.0], [0.0], 1e-6I(1), 1e-4I(1), 1)
@test modelv.ukf.Ts == Tsv
@test modelv.p ≈ pv rtol = 1e-3
@test ControlSystemIdentification.predict(modelv, dv) ≈ reduce(hcat, yv) atol = 1e-2
end
Loading