Skip to content

Commit 84b114c

Browse files
authored
Merge pull request sympy#26758 from abhiphile/gsoc2.2
Added tests using symbolic matrices in StateSpace.
2 parents 362b3fe + 09a00c6 commit 84b114c

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

sympy/physics/control/tests/test_lti.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1669,6 +1669,7 @@ def test_conversion():
16691669
C1 = Matrix([[1, 2]])
16701670
D1 = Matrix([0])
16711671
H1 = StateSpace(A1, B1, C1, D1)
1672+
H3 = StateSpace(Matrix([[a0, a1], [a2, a3]]), B = Matrix([[b1], [b2]]), C = Matrix([[c1, c2]]))
16721673
tm1 = H1.rewrite(TransferFunction)
16731674
tm2 = (-H1).rewrite(TransferFunction)
16741675

@@ -1691,7 +1692,8 @@ def test_conversion():
16911692
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)
16921693
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)
16931694
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)]]
16951697
# TransferFunction to StateSpace
16961698
SS = TF1.rewrite(StateSpace)
16971699
assert SS == \
@@ -1717,6 +1719,7 @@ def test_StateSpace_dsolve():
17171719
I1 = Matrix([[1], [2]])
17181720
t = symbols('t')
17191721
ss1 = StateSpace(A1, B1, C1, D1)
1722+
17201723
# Zero input and Zero initial conditions
17211724
assert ss1.dsolve() == Matrix([[0]])
17221725
assert ss1.dsolve(initial_conditions=I1) == Matrix([[8*exp(-t) - 9*exp(-2*t)]])
@@ -1736,6 +1739,7 @@ def test_StateSpace_dsolve():
17361739
op = ss3.dsolve(input_vector=U3, var=t)
17371740
assert str(op.simplify().expand().evalf()[0]) == str(5.0 + 20.7880460155075*exp(-5*t/2)*sin(sqrt(7)*t/2)
17381741
- 5.0*exp(-5*t/2)*cos(sqrt(7)*t/2))
1742+
17391743
# Test with Heaviside as input
17401744
A4 = Matrix([[-1, 1], [-4, -4]])
17411745
B4 = Matrix([[0], [4]])
@@ -1745,6 +1749,7 @@ def test_StateSpace_dsolve():
17451749
op4 = str(ss4.dsolve(var=t, input_vector=U4)[0].simplify().expand().evalf())
17461750
assert op4 == str(5.0*Heaviside(t) + 20.7880460155075*exp(-5*t/2)*sin(sqrt(7)*t/2)*Heaviside(t)
17471751
- 5.0*exp(-5*t/2)*cos(sqrt(7)*t/2)*Heaviside(t))
1752+
17481753
# Test with Symbolic Matrices
17491754
m, a, x0 = symbols('m a x_0')
17501755
A5 = Matrix([[0, 1], [0, 0]])
@@ -1776,6 +1781,7 @@ def test_StateSpace_functions():
17761781
SS1 = StateSpace(A_mat, B_mat, C_mat, D_mat)
17771782
SS2 = StateSpace(Matrix([[1, 1], [4, -2]]),Matrix([[0, 1], [0, 2]]),Matrix([[-1, 1], [1, -1]]))
17781783
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]]))
17791785

17801786
# Observability
17811787
assert SS1.is_observable() == True
@@ -1784,6 +1790,8 @@ def test_StateSpace_functions():
17841790
assert SS2.observability_matrix() == Matrix([[-1, 1], [ 1, -1], [ 3, -3], [-3, 3]])
17851791
assert SS1.observable_subspace() == [Matrix([[0], [1]]), Matrix([[1], [0]])]
17861792
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]])
17871795

17881796
# Controllability
17891797
assert SS1.is_controllable() == True
@@ -1792,6 +1800,13 @@ def test_StateSpace_functions():
17921800
assert SS3.controllability_matrix() == Matrix([[1, -1, 2, -2], [1, -1, 2, -2]])
17931801
assert SS1.controllable_subspace() == [Matrix([[0.5], [ 0]]), Matrix([[-0.75], [ 0.5]])]
17941802
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]])
17951810

17961811
# Append
17971812
A1 = Matrix([[0, 1], [1, 0]])
@@ -1801,6 +1816,7 @@ def test_StateSpace_functions():
18011816
ss1 = StateSpace(A1, B1, C1, D1)
18021817
ss2 = StateSpace(Matrix([[1, 0], [0, 1]]), Matrix([[1], [0]]), Matrix([[1, 0]]), Matrix([[1]]))
18031818
ss3 = ss1.append(ss2)
1819+
ss4 = SS4.append(ss1)
18041820

18051821
assert ss3.num_states == ss1.num_states + ss2.num_states
18061822
assert ss3.num_inputs == ss1.num_inputs + ss2.num_inputs
@@ -1810,6 +1826,15 @@ def test_StateSpace_functions():
18101826
assert ss3.output_matrix == Matrix([[0, 1, 0, 0], [0, 0, 1, 0]])
18111827
assert ss3.feedforward_matrix == Matrix([[0, 0], [0, 1]])
18121828

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+
18131838

18141839
def test_StateSpace_series():
18151840
# For SISO Systems
@@ -1855,6 +1880,7 @@ def test_StateSpace_series():
18551880
ser2 = Series(ss1)
18561881
ser3 = Series(ser2, ss2)
18571882
assert ser3.doit() == ser1.doit()
1883+
18581884
# TransferFunction interconnection with StateSpace
18591885
ser_tf = Series(tf1, ss1)
18601886
assert ser_tf == Series(TransferFunction(s, s + 1, s), StateSpace(Matrix([
@@ -1874,6 +1900,7 @@ def test_StateSpace_series():
18741900
Matrix([[0, 0, 1]]),
18751901
Matrix([[0]]))
18761902
assert ser_tf.rewrite(TransferFunction) == TransferFunction(s**2, s**3 + s**2 - s - 1, s)
1903+
18771904
# For MIMO Systems
18781905
a3 = Matrix([[4, 1], [2, -3]])
18791906
b3 = Matrix([[5, 2], [-3, -3]])
@@ -1976,6 +2003,7 @@ def test_StateSpace_parallel():
19762003
Matrix([[0, 1, 1, 0]]),
19772004
Matrix([[1]]))
19782005
assert p1.rewrite(TransferFunction) == TransferFunction(s*(s + 2), s**2 - 1, s)
2006+
19792007
# Connecting StateSpace with TransferFunction
19802008
tf1 = TransferFunction(s, s+1, s)
19812009
p2 = Parallel(ss1, tf1)
@@ -2046,6 +2074,7 @@ def test_StateSpace_parallel():
20462074
Matrix([
20472075
[1, 6],
20482076
[1, 0]]))
2077+
20492078
# Using StateSpace with MIMOParallel.
20502079
tf2 = TransferFunction(1, s, s)
20512080
tf3 = TransferFunction(1, s + 1, s)

0 commit comments

Comments
 (0)