From 92b2839f8e2df0f5f8754bdb5bbf3dff3cd2c8c6 Mon Sep 17 00:00:00 2001 From: Fredrik Bagge Carlson Date: Fri, 20 Nov 2026 06:33:47 +0000 Subject: [PATCH] nonlinear_pem: pass the sample interval of the data to the UKF The UKF of nonlinear_pem was constructed without Ts, so dynamics and measurement functions that depend on time received t = k instead of t = k*d.Ts. predict passed the sample index as the time to the measurement function; it now passes (k-1)*d.Ts. Co-Authored-By: Claude Opus 5.5 --- ext/ControlSystemIdentificationLSOptExt.jl | 4 ++-- test/test_nonlinear_pem.jl | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/ext/ControlSystemIdentificationLSOptExt.jl b/ext/ControlSystemIdentificationLSOptExt.jl index 4faec722..a05075d2 100644 --- a/ext/ControlSystemIdentificationLSOptExt.jl +++ b/ext/ControlSystemIdentificationLSOptExt.jl @@ -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) @@ -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 diff --git a/test/test_nonlinear_pem.jl b/test/test_nonlinear_pem.jl index f99b5094..254fbcb3 100644 --- a/test/test_nonlinear_pem.jl +++ b/test/test_nonlinear_pem.jl @@ -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