8000 Add observable/unobservable test in canonical_test.py based on Pull #… · python-control/python-control@5f6139c · GitHub
[go: up one dir, main page]

Skip to content

Commit 5f6139c

Browse files
author
Yuichi NAGAYAMA
committed
Add observable/unobservable test in canonical_test.py based on Pull #101(ref Pull #103)
1 parent 32323d4 commit 5f6139c

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

control/tests/canonical_test.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,51 @@ def test_unreachable_system(self):
5252

5353
# Check if an exception is raised
5454
np.testing.assert_raises(ValueError, canonical_form, sys, "reachable")
55+
56+
def test_observable_form(self):
57+
"""Test the observable canonical form"""
58+
59+
# Create a system in the observable canonical form
60+
coeffs = [1.0, 2.0, 3.0, 4.0, 1.0]
61+
A_true = np.polynomial.polynomial.polycompanion(coeffs)
62+
A_true = np.fliplr(np.rot90(A_true)).T
63+
B_true = np.matrix("1.0 1.0 1.0 1.0").T
64+
C_true = np.matrix("1.0 0.0 0.0 0.0")
65+
D_true = 42.0
66+
67+
# Perform a coordinate transform with a random invertible matrix
68+
T_true = np.matrix([[-0.27144004, -0.39933167, 0.75634684, 0.44135471],
69+
[-0.74855725, -0.39136285, -0.18142339, -0.50356997],
70+
[-0.40688007, 0.81416369, 0.38002113, -0.16483334],
71+
[-0.44769516, 0.15654653, -0.50060858, 0.72419146]])
72+
73+
A = np.linalg.solve(T_true, A_true)*T_true
74+
B = np.linalg.solve(T_true, B_true)
75+
C = C_true*T_true
76+
D = D_true
77+
78+
# Create a state space system and convert it to the observable canonical form
79+
sys_check, T_check = canonical_form(ss(A, B, C, D), "observable")
80+
81+
# Check against the true values
82+
np.testing.assert_array_almost_equal(sys_check.A, A_true)
83+
np.testing.assert_array_almost_equal(sys_check.B, B_true)
84+
np.testing.assert_array_almost_equal(sys_check.C, C_true)
85+
np.testing.assert_array_almost_equal(sys_check.D, D_true)
86+
np.testing.assert_array_almost_equal(T_check, T_true)
87+
88+
def test_unobservable_system(self):
89+
"""Test observable canonical form with an unobservable system"""
90+
91+
# Create an observable system
92+
A = np.matrix("1.0 2.0 2.0; 4.0 5.0 5.0; 7.0 8.0 8.0")
93+
B = np.matrix("1.0 1.0 1.0").T
94+
C = np.matrix("1.0 1.0 1.0")
95+
D = 42.0
96+
sys = ss(A, B, C, D)
97+
98+
# Check if an exception is raised
99+
np.testing.assert_raises(ValueError, canonical_form, sys, "observable")
100+
101+
if __name__ == '__main__':
102+
unittest.main()

0 commit comments

Comments
 (0)
0