Skip to content

Commit c62931e

Browse files
committed
Add era example
1 parent 28744f8 commit c62931e

1 file changed

Lines changed: 64 additions & 0 deletions

File tree

examples/era_mkd.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# mkd_era.py
2+
# Johannes Kaisinger, 4 July 2024
3+
#
4+
# Demonstrate estimation of markov parameters.
5+
# SISO, SIMO, MISO, MIMO case
6+
7+
8+
import numpy as np
9+
import matplotlib.pyplot as plt
10+
import os
11+
12+
13+
import control as ct
14+
15+
16+
# set up a mass spring damper system (2dof, MIMO case)
17+
# m q_dd + c q_d + k q = u
18+
m1, k1, c1 = 1., 1., .1
19+
m2, k2, c2 = 2., .5, .1
20+
k3, c3 = .5, .1
21+
22+
A = np.array([
23+
[0., 0., 1., 0.],
24+
[0., 0., 0., 1.],
25+
[-(k1+k2)/m1, (k2)/m1, -(c1+c2)/m1, c2/m1],
26+
[(k2)/m2, -(k2+k3)/m2, c2/m2, -(c2+c3)/m2]
27+
])
28+
B = np.array([[0.,0.],[0.,0.],[1/m1,0.],[0.,1/m2]])
29+
C = np.array([[1.0, 0.0, 0.0, 0.0],[0.0, 1.0, 0.0, 0.0]])
30+
D = np.zeros((2,2))
31+
32+
xixo_list = ["SISO","SIMO","MISO","MIMO"]
33+
xixo = xixo_list[3] # choose a system for estimation
34+
match xixo:
35+
case "SISO":
36+
sys = ct.StateSpace(A, B[:,0], C[0,:], D[0,0])
37+
case "SIMO":
38+
sys = ct.StateSpace(A, B[:,:1], C, D[:,:1])
39+
case "MISO":
40+
sys = ct.StateSpace(A, B, C[:1,:], D[:1,:])
41+
case "MIMO":
42+
sys = ct.StateSpace(A, B, C, D)
43+
44+
45+
dt = 0.5
46+
sysd = sys.sample(dt, method='zoh')
47+
response = ct.impulse_response(sysd)
48+
response.plot()
49+
plt.show()
50+
51+
sysd_est, _ = ct.era(response,r=4,dt=dt)
52+
53+
step_true = ct.step_response(sysd)
54+
step_est = ct.step_response(sysd_est)
55+
56+
step_true.plot(title=xixo)
57+
step_est.plot(color='orange',linestyle='dashed')
58+
59+
plt.show()
60+
61+
62+
if 'PYCONTROL_TEST_EXAMPLES' not in os.environ:
63+
64+
plt.show()

0 commit comments

Comments
 (0)