Skip to content

Conversation

baggepinnen
Copy link
Owner

@baggepinnen baggepinnen commented Jun 26, 2025

Usage (experimental)

using ControlSystemIdentification, Optim, ControlSystemsBase
import LowLevelParticleFilters: SimpleMvNormal
using MonteCarloMeasurements
using Random, Test, LinearAlgebra

constructor = function (p)
    Mα, Mq, Mδe, Zα, Zq, Zδe = p
    V = 30.2
    A = [
        Mq Mα
        (1+Zq / V) Zα/V
    ]
    B = [Mδe; Zδe / V]
    return ss(A, B, I, 0)
end

# True Parameters
Mα_true = -83.17457223750834
Mq_true = -4.0485957994781785
Mδe_true = -80.71377329163104
Zα_true = -115.91677356408941
Zq_true = -0.573560626095269
Zδe_true = 32.13261325199936

Ts = 0.01
t = 0:Ts:10

p_true = [Mα_true, Mq_true, Mδe_true, Zα_true, Zq_true, Zδe_true]
p0 = [Mα_true, Mq_true, Mδe_true, Zα_true, Zq_true, Zδe_true] .* 1.2

P_true = constructor(p_true)
u = sign.(sin.((1 / 3 * t)')) .+ sign.(sin.((1 / 2 * t)'))
res_true = lsim(c2d(P_true, Ts, :tustin), u, t)
d = iddata(res_true)

nx = 2
function regularizer(p, _)
    sum(abs2, p.K)
end

d2 = deepcopy(d)
d2.y .+= randn.()

res = ControlSystemIdentification.structured_pem(d2, nx; focus=:simulation, p0, constructor, regularizer, show_trace = false, show_every = 1, iterations=300, method=:tustin)

H = res.Λ() # Inverse covariance matrix
Σ = inv(H) # Covariance matrix
dp = SimpleMvNormal(res.res.minimizer, Σ) # Parameter posterior distribution
pars = Particles(rand!(Xoshiro(), dp, zeros(length(dp), 100))') # Sample uncertain parameters
sysu, Ku, x0u = res.constructor(pars) # Construct uncertain system

w = exp10.(LinRange(-3, 2, 200))
unsafe_comparisons(true)
bodeplot(sysu, w, plotphase=false)
bodeplot!(res.sys, w, plotphase=false, legend=false)

image

@codecov
Copy link

codecov bot commented Jun 26, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 88.36%. Comparing base (b30bfbe) to head (592d394).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #188      +/-   ##
==========================================
- Coverage   88.68%   88.36%   -0.32%     
==========================================
  Files          13       13              
  Lines        2404     2416      +12     
==========================================
+ Hits         2132     2135       +3     
- Misses        272      281       +9     
Flag Coverage Δ
unittests 88.36% <100.00%> (-0.32%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant