Skip to content

Commit efa3f39

Browse files
committed
Add markov example
1 parent 14ce769 commit efa3f39

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

examples/markov.py

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

0 commit comments

Comments
 (0)