@@ -1669,6 +1669,7 @@ def test_conversion():
1669
1669
C1 = Matrix ([[1 , 2 ]])
1670
1670
D1 = Matrix ([0 ])
1671
1671
H1 = StateSpace (A1 , B1 , C1 , D1 )
1672
+ H3 = StateSpace (Matrix ([[a0 , a1 ], [a2 , a3 ]]), B = Matrix ([[b1 ], [b2 ]]), C = Matrix ([[c1 , c2 ]]))
1672
1673
tm1 = H1 .rewrite (TransferFunction )
1673
1674
tm2 = (- H1 ).rewrite (TransferFunction )
1674
1675
@@ -1691,7 +1692,8 @@ def test_conversion():
1691
1692
assert tm3 [0 ][0 ] == TransferFunction (2.0 * s ** 3 + 1.0 * s ** 2 - 10.5 * s + 4.5 , 1.0 * s ** 3 + 0.5 * s ** 2 - 6.5 * s - 2.5 , s )
1692
1693
assert tm3 [0 ][1 ] == TransferFunction (2.0 * s ** 3 + 2.0 * s ** 2 - 10.5 * s - 3.5 , 1.0 * s ** 3 + 0.5 * s ** 2 - 6.5 * s - 2.5 , s )
1693
1694
assert tm3 [0 ][2 ] == TransferFunction (2.0 * s ** 2 + 5.0 * s - 0.5 , 1.0 * s ** 3 + 0.5 * s ** 2 - 6.5 * s - 2.5 , s )
1694
-
1695
+ assert H3 .rewrite (TransferFunction ) == [[TransferFunction (- c1 * (a1 * b2 - a3 * b1 + b1 * s ) - c2 * (- a0 * b2 + a2 * b1 + b2 * s ),
1696
+ - a0 * a3 + a0 * s + a1 * a2 + a3 * s - s ** 2 , s )]]
1695
1697
# TransferFunction to StateSpace
1696
1698
SS = TF1 .rewrite (StateSpace )
1697
1699
assert SS == \
@@ -1717,6 +1719,7 @@ def test_StateSpace_dsolve():
1717
1719
I1 = Matrix ([[1 ], [2 ]])
1718
1720
t = symbols ('t' )
1719
1721
ss1 = StateSpace (A1 , B1 , C1 , D1 )
1722
+
1720
1723
# Zero input and Zero initial conditions
1721
1724
assert ss1 .dsolve () == Matrix ([[0 ]])
1722
1725
assert ss1 .dsolve (initial_conditions = I1 ) == Matrix ([[8 * exp (- t ) - 9 * exp (- 2 * t )]])
@@ -1736,6 +1739,7 @@ def test_StateSpace_dsolve():
1736
1739
op = ss3 .dsolve (input_vector = U3 , var = t )
1737
1740
assert str (op .simplify ().expand ().evalf ()[0 ]) == str (5.0 + 20.7880460155075 * exp (- 5 * t / 2 )* sin (sqrt (7 )* t / 2 )
1738
1741
- 5.0 * exp (- 5 * t / 2 )* cos (sqrt (7 )* t / 2 ))
1742
+
1739
1743
# Test with Heaviside as input
1740
1744
A4 = Matrix ([[- 1 , 1 ], [- 4 , - 4 ]])
1741
1745
B4 = Matrix ([[0 ], [4 ]])
@@ -1745,6 +1749,7 @@ def test_StateSpace_dsolve():
1745
1749
op4 = str (ss4 .dsolve (var = t , input_vector = U4 )[0 ].simplify ().expand ().evalf ())
1746
1750
assert op4 == str (5.0 * Heaviside (t ) + 20.7880460155075 * exp (- 5 * t / 2 )* sin (sqrt (7 )* t / 2 )* Heaviside (t )
1747
1751
- 5.0 * exp (- 5 * t / 2 )* cos (sqrt (7 )* t / 2 )* Heaviside (t ))
1752
+
1748
1753
# Test with Symbolic Matrices
1749
1754
m , a , x0 = symbols ('m a x_0' )
1750
1755
A5 = Matrix ([[0 , 1 ], [0 , 0 ]])
@@ -1776,6 +1781,7 @@ def test_StateSpace_functions():
1776
1781
SS1 = StateSpace (A_mat , B_mat , C_mat , D_mat )
1777
1782
SS2 = StateSpace (Matrix ([[1 , 1 ], [4 , - 2 ]]),Matrix ([[0 , 1 ], [0 , 2 ]]),Matrix ([[- 1 , 1 ], [1 , - 1 ]]))
1778
1783
SS3 = StateSpace (Matrix ([[1 , 1 ], [4 , - 2 ]]),Matrix ([[1 , - 1 ], [1 , - 1 ]]))
1784
+ SS4 = StateSpace (Matrix ([[a0 , a1 ], [a2 , a3 ]]), Matrix ([[b1 ], [b2 ]]), Matrix ([[c1 , c2 ]]))
1779
1785
1780
1786
# Observability
1781
1787
assert SS1 .is_observable () == True
@@ -1784,6 +1790,8 @@ def test_StateSpace_functions():
1784
1790
assert SS2 .observability_matrix () == Matrix ([[- 1 , 1 ], [ 1 , - 1 ], [ 3 , - 3 ], [- 3 , 3 ]])
1785
1791
assert SS1 .observable_subspace () == [Matrix ([[0 ], [1 ]]), Matrix ([[1 ], [0 ]])]
1786
1792
assert SS2 .observable_subspace () == [Matrix ([[- 1 ], [ 1 ], [ 3 ], [- 3 ]])]
1793
+ Qo = SS4 .observability_matrix ().subs ([(a0 , 0 ), (a1 , - 6 ), (a2 , 1 ), (a3 , - 5 ), (c1 , 0 ), (c2 , 1 )])
1794
+ assert Qo == Matrix ([[0 , 1 ], [1 , - 5 ]])
1787
1795
1788
1796
# Controllability
1789
1797
assert SS1 .is_controllable () == True
@@ -1792,6 +1800,13 @@ def test_StateSpace_functions():
1792
1800
assert SS3 .controllability_matrix () == Matrix ([[1 , - 1 , 2 , - 2 ], [1 , - 1 , 2 , - 2 ]])
1793
1801
assert SS1 .controllable_subspace () == [Matrix ([[0.5 ], [ 0 ]]), Matrix ([[- 0.75 ], [ 0.5 ]])]
1794
1802
assert SS3 .controllable_subspace () == [Matrix ([[1 ], [1 ]])]
1803
+ assert SS4 .controllable_subspace () == [Matrix ([
1804
+ [b1 ],
1805
+ [b2 ]]), Matrix ([
1806
+ [a0 * b1 + a1 * b2 ],
1807
+ [a2 * b1 + a3 * b2 ]])]
1808
+ Qc = SS4 .controllability_matrix ().subs ([(a0 , 0 ), (a1 , 1 ), (a2 , - 6 ), (a3 , - 5 ), (b1 , 0 ), (b2 , 1 )])
1809
+ assert Qc == Matrix ([[0 , 1 ], [1 , - 5 ]])
1795
1810
1796
1811
# Append
1797
1812
A1 = Matrix ([[0 , 1 ], [1 , 0 ]])
@@ -1801,6 +1816,7 @@ def test_StateSpace_functions():
1801
1816
ss1 = StateSpace (A1 , B1 , C1 , D1 )
1802
1817
ss2 = StateSpace (Matrix ([[1 , 0 ], [0 , 1 ]]), Matrix ([[1 ], [0 ]]), Matrix ([[1 , 0 ]]), Matrix ([[1 ]]))
1803
1818
ss3 = ss1 .append (ss2 )
1819
+ ss4 = SS4 .append (ss1 )
1804
1820
1805
1821
assert ss3 .num_states == ss1 .num_states + ss2 .num_states
1806
1822
assert ss3 .num_inputs == ss1 .num_inputs + ss2 .num_inputs
@@ -1810,6 +1826,15 @@ def test_StateSpace_functions():
1810
1826
assert ss3 .output_matrix == Matrix ([[0 , 1 , 0 , 0 ], [0 , 0 , 1 , 0 ]])
1811
1827
assert ss3 .feedforward_matrix == Matrix ([[0 , 0 ], [0 , 1 ]])
1812
1828
1829
+ # Using symbolic matrices
1830
+ assert ss4 .num_states == SS4 .num_states + ss1 .num_states
1831
+ assert ss4 .num_inputs == SS4 .num_inputs + ss1 .num_inputs
1832
+ assert ss4 .num_outputs == SS4 .num_outputs + ss1 .num_outputs
1833
+ assert ss4 .state_matrix == Matrix ([[a0 , a1 , 0 , 0 ], [a2 , a3 , 0 , 0 ], [0 , 0 , 0 , 1 ], [0 , 0 , 1 , 0 ]])
1834
+ assert ss4 .input_matrix == Matrix ([[b1 , 0 ], [b2 , 0 ], [0 , 0 ], [0 , 1 ]])
1835
+ assert ss4 .output_matrix == Matrix ([[c1 , c2 , 0 , 0 ], [0 , 0 , 0 , 1 ]])
1836
+ assert ss4 .feedforward_matrix == Matrix ([[0 , 0 ], [0 , 0 ]])
1837
+
1813
1838
1814
1839
def test_StateSpace_series ():
1815
1840
# For SISO Systems
@@ -1855,6 +1880,7 @@ def test_StateSpace_series():
1855
1880
ser2 = Series (ss1 )
1856
1881
ser3 = Series (ser2 , ss2 )
1857
1882
assert ser3 .doit () == ser1 .doit ()
1883
+
1858
1884
# TransferFunction interconnection with StateSpace
1859
1885
ser_tf = Series (tf1 , ss1 )
1860
1886
assert ser_tf == Series (TransferFunction (s , s + 1 , s ), StateSpace (Matrix ([
@@ -1874,6 +1900,7 @@ def test_StateSpace_series():
1874
1900
Matrix ([[0 , 0 , 1 ]]),
1875
1901
Matrix ([[0 ]]))
1876
1902
assert ser_tf .rewrite (TransferFunction ) == TransferFunction (s ** 2 , s ** 3 + s ** 2 - s - 1 , s )
1903
+
1877
1904
# For MIMO Systems
1878
1905
a3 = Matrix ([[4 , 1 ], [2 , - 3 ]])
1879
1906
b3 = Matrix ([[5 , 2 ], [- 3 , - 3 ]])
@@ -1976,6 +2003,7 @@ def test_StateSpace_parallel():
1976
2003
Matrix ([[0 , 1 , 1 , 0 ]]),
1977
2004
Matrix ([[1 ]]))
1978
2005
assert p1 .rewrite (TransferFunction ) == TransferFunction (s * (s + 2 ), s ** 2 - 1 , s )
2006
+
1979
2007
# Connecting StateSpace with TransferFunction
1980
2008
tf1 = TransferFunction (s , s + 1 , s )
1981
2009
p2 = Parallel (ss1 , tf1 )
@@ -2046,6 +2074,7 @@ def test_StateSpace_parallel():
2046
2074
Matrix ([
2047
2075
[1 , 6 ],
2048
2076
[1 , 0 ]]))
2077
+
2049
2078
# Using StateSpace with MIMOParallel.
2050
2079
tf2 = TransferFunction (1 , s , s )
2051
2080
tf3 = TransferFunction (1 , s + 1 , s )
0 commit comments