Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix issue with slycot balred change in state
  • Loading branch information
murrayrm authored and bnavigator committed Aug 20, 2022
commit c54097022899eaf51d8feb29637b26e663de88d1
48 changes: 40 additions & 8 deletions control/tests/modelsimp_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,17 +182,33 @@ def testBalredTruncate(self, matarrayin):
B = matarrayin([[2.], [0.], [0.], [0.]])
C = matarrayin([[0.5, 0.6875, 0.7031, 0.5]])
D = matarrayin([[0.]])

sys = StateSpace(A, B, C, D)
orders = 2
rsys = balred(sys, orders, method='truncate')
Ar, Br, Cr, Dr = rsys.A, rsys.B, rsys.C, rsys.D

# Result from MATLAB
Artrue = np.array([[-1.958, -1.194], [-1.194, -0.8344]])
Brtrue = np.array([[0.9057], [0.4068]])
Crtrue = np.array([[0.9057, 0.4068]])
Drtrue = np.array([[0.]])
np.testing.assert_array_almost_equal(rsys.A, Artrue, decimal=2)
np.testing.assert_array_almost_equal(rsys.B, Brtrue, decimal=4)
np.testing.assert_array_almost_equal(rsys.C, Crtrue, decimal=4)
np.testing.assert_array_almost_equal(rsys.D, Drtrue, decimal=4)

# Look for possible changes in state in slycot
T1 = np.array([[1, 0], [0, -1]])
T2 = np.array([[-1, 0], [0, 1]])
T3 = np.array([[0, 1], [1, 0]])
for T in (T1, T2, T3):
if np.allclose(T @ Ar @ T, Artrue, atol=1e-2, rtol=1e-2):
# Apply a similarity transformation
Ar, Br, Cr = T @ Ar @ T, T @ Br, Cr @ T
break

# Make sure we got the correct answer
np.testing.assert_array_almost_equal(Ar, Artrue, decimal=2)
np.testing.assert_array_almost_equal(Br, Brtrue, decimal=4)
np.testing.assert_array_almost_equal(Cr, Crtrue, decimal=4)
np.testing.assert_array_almost_equal(Dr, Drtrue, decimal=4)

@slycotonly
def testBalredMatchDC(self, matarrayin):
Expand All @@ -207,16 +223,32 @@ def testBalredMatchDC(self, matarrayin):
B = matarrayin([[2.], [0.], [0.], [0.]])
C = matarrayin([[0.5, 0.6875, 0.7031, 0.5]])
D = matarrayin([[0.]])

sys = StateSpace(A, B, C, D)
orders = 2
rsys = balred(sys,orders,method='matchdc')
Ar, Br, Cr, Dr = rsys.A, rsys.B, rsys.C, rsys.D

# Result from MATLAB
Artrue = np.array(
[[-4.43094773, -4.55232904],
[-4.55232904, -5.36195206]])
Brtrue = np.array([[1.36235673], [1.03114388]])
Crtrue = np.array([[1.36235673, 1.03114388]])
Drtrue = np.array([[-0.08383902]])
np.testing.assert_array_almost_equal(rsys.A, Artrue, decimal=2)
np.testing.assert_array_almost_equal(rsys.B, Brtrue, decimal=4)
np.testing.assert_array_almost_equal(rsys.C, Crtrue, decimal=4)
np.testing.assert_array_almost_equal(rsys.D, Drtrue, decimal=4)

# Look for possible changes in state in slycot
T1 = np.array([[1, 0], [0, -1]])
T2 = np.array([[-1, 0], [0, 1]])
T3 = np.array([[0, 1], [1, 0]])
for T in (T1, T2, T3):
if np.allclose(T @ Ar @ T, Artrue, atol=1e-2, rtol=1e-2):
# Apply a similarity transformation
Ar, Br, Cr = T @ Ar @ T, T @ Br, Cr @ T
break

# Make sure we got the correct answer
np.testing.assert_array_almost_equal(Ar, Artrue, decimal=2)
np.testing.assert_array_almost_equal(Br, Brtrue, decimal=4)
np.testing.assert_array_almost_equal(Cr, Crtrue, decimal=4)
np.testing.assert_array_almost_equal(Dr, Drtrue, decimal=4)