From 684fadbc52d24f633f747061dbd32fca1eb07641 Mon Sep 17 00:00:00 2001 From: Hung Pham Date: Tue, 23 Oct 2018 21:54:05 +0800 Subject: [PATCH 1/5] add test case for lft for statespace model --- control/tests/statesp_test.py | 36 +++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/control/tests/statesp_test.py b/control/tests/statesp_test.py index 08a8cdc22..8a59414e3 100644 --- a/control/tests/statesp_test.py +++ b/control/tests/statesp_test.py @@ -448,6 +448,42 @@ def empty(shape): np.testing.assert_array_equal(empty((D.shape[0], 0)), g.C) np.testing.assert_array_equal(D, g.D) + def test_lft(self): + """ compare lft function with matlab implementation""" + # test case + A = [[1, 2, 3], + [1, 4, 5], + [2, 3, 4]] + B = [[0, 2], + [5, 6], + [5, 2]] + C = [[1, 4, 5], + [2, 3, 0]] + D = [[0, 0], + [3, 0]] + P = StateSpace(A, B, C, D) + Ak = [[0, 2, 3], + [2, 3, 5], + [2, 1, 9]] + Bk = [[1, 1], + [2, 3], + [9, 4]] + Ck = [[1, 4, 5], + [2, 3, 6]] + Dk = [[0, 2], + [0, 0]] + K = StateSpace(Ak, Bk, Ck, Dk) + pk = P.lft(K, 2, 1) + + # correct result (from matlab) + Ak = [1, 2, 3, 4, 6, 12, 1, 4, 5, 17, 38, 61, 2, 3, 4, 9, 26, 37, 2, 3, 0, 3, 14, 18, 4, 6, 0, 8, 27, 35, 18, 27, 0, 29, 109, 144] + Bk = [0, 10, 10, 7, 15, 58] + Ck = [1, 4, 5, 0, 0, 0] + Dk = [0] + np.testing.assert_allclose(np.flatten(pk.A), Ak) + np.testing.assert_allclose(np.flatten(pk.B), Bk) + np.testing.assert_allclose(np.flatten(pk.C), Ck) + np.testing.assert_allclose(np.flatten(pk.D), Dk) class TestRss(unittest.TestCase): """These are tests for the proper functionality of statesp.rss.""" From facd0205bd314c99a31f61b55116d22e29712689 Mon Sep 17 00:00:00 2001 From: Hung Pham Date: Wed, 24 Oct 2018 00:38:34 +0800 Subject: [PATCH 2/5] another test case for lft --- control/tests/statesp_test.py | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/control/tests/statesp_test.py b/control/tests/statesp_test.py index 8a59414e3..4ef2f9eb9 100644 --- a/control/tests/statesp_test.py +++ b/control/tests/statesp_test.py @@ -449,7 +449,7 @@ def empty(shape): np.testing.assert_array_equal(D, g.D) def test_lft(self): - """ compare lft function with matlab implementation""" + """ test lft function with result obtained from matlab implementation""" # test case A = [[1, 2, 3], [1, 4, 5], @@ -473,17 +473,28 @@ def test_lft(self): Dk = [[0, 2], [0, 0]] K = StateSpace(Ak, Bk, Ck, Dk) - pk = P.lft(K, 2, 1) - # correct result (from matlab) - Ak = [1, 2, 3, 4, 6, 12, 1, 4, 5, 17, 38, 61, 2, 3, 4, 9, 26, 37, 2, 3, 0, 3, 14, 18, 4, 6, 0, 8, 27, 35, 18, 27, 0, 29, 109, 144] - Bk = [0, 10, 10, 7, 15, 58] - Ck = [1, 4, 5, 0, 0, 0] - Dk = [0] - np.testing.assert_allclose(np.flatten(pk.A), Ak) - np.testing.assert_allclose(np.flatten(pk.B), Bk) - np.testing.assert_allclose(np.flatten(pk.C), Ck) - np.testing.assert_allclose(np.flatten(pk.D), Dk) + # case 1 + pk = P.lft(K, 2, 1) + Amatlab = [1, 2, 3, 4, 6, 12, 1, 4, 5, 17, 38, 61, 2, 3, 4, 9, 26, 37, 2, 3, 0, 3, 14, 18, 4, 6, 0, 8, 27, 35, 18, 27, 0, 29, 109, 144] + Bmatlab = [0, 10, 10, 7, 15, 58] + Cmatlab = [1, 4, 5, 0, 0, 0] + Dmatlab = [0] + np.testing.assert_allclose(np.array(pk.A).reshape(-1), Amatlab) + np.testing.assert_allclose(np.array(pk.B).reshape(-1), Bmatlab) + np.testing.assert_allclose(np.array(pk.C).reshape(-1), Cmatlab) + np.testing.assert_allclose(np.array(pk.D).reshape(-1), Dmatlab) + + # case 2 + pk = P.lft(K) + Amatlab = [1, 2, 3, 4, 6, 12, -3, -2, 5, 11, 14, 31, -2, -3, 4, 3, 2, 7, 0.6, 3.4, 5, -0.6, -0.4, 0, 0.8, 6.2, 10, 0.2, -4.2, -4, 7.4, 33.6, 45, -0.4, -8.6, -3] + Bmatlab = [] + Cmatlab = [] + Dmatlab = [] + np.testing.assert_allclose(np.array(pk.A).reshape(-1), Amatlab) + np.testing.assert_allclose(np.array(pk.B).reshape(-1), Bmatlab) + np.testing.assert_allclose(np.array(pk.C).reshape(-1), Cmatlab) + np.testing.assert_allclose(np.array(pk.D).reshape(-1), Dmatlab) class TestRss(unittest.TestCase): """These are tests for the proper functionality of statesp.rss.""" From e1ab130da0e4307ba9b2902111e197b1f15b05f6 Mon Sep 17 00:00:00 2001 From: Hung Pham Date: Wed, 24 Oct 2018 00:38:47 +0800 Subject: [PATCH 3/5] implement lft system interconnection --- control/statesp.py | 96 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) diff --git a/control/statesp.py b/control/statesp.py index e0f82c132..6fb473d32 100644 --- a/control/statesp.py +++ b/control/statesp.py @@ -607,6 +607,102 @@ def feedback(self, other=1, sign=-1): return StateSpace(A, B, C, D, dt) + def lft(self, other, nu=-1, ny=-1): + """Return the Linear Fractional Transformation. + + See definition here: + https://www.mathworks.com/help/control/ref/lft.html + + Parameters + ---------- + other: LTI + The lower LTI system + ny: int, optional + Dimension of (plant) measurement output. + nu: int, optional + Dimension of (plant) control input. + """ + other = _convertToStateSpace(other) + # maximal values for nu, ny + if ny == -1: + ny = min(other.inputs, self.outputs) + if nu == -1: + nu = min(other.outputs, self.inputs) + # dimension check + # TODO + + # Figure out the sampling time to use + if (self.dt == None and other.dt != None): + dt = other.dt # use dt from second argument + elif (other.dt == None and self.dt != None) or \ + timebaseEqual(self, other): + dt = self.dt # use dt from first argument + else: + raise ValueError("Systems have different sampling times") + + # submatrices + A = self.A + B1 = self.B[:, :self.inputs - nu] + B2 = self.B[:, self.inputs - nu:] + C1 = self.C[:self.outputs - ny, :] + C2 = self.C[self.outputs - ny:, :] + D11 = self.D[:self.outputs - ny, :self.inputs - nu] + D12 = self.D[:self.outputs - ny, self.inputs - nu:] + D21 = self.D[self.outputs - ny:, :self.inputs - nu] + D22 = self.D[self.outputs - ny:, self.inputs - nu:] + + # submatrices + Abar = other.A + Bbar1 = other.B[:, :ny] + Bbar2 = other.B[:, ny:] + Cbar1 = other.C[:nu, :] + Cbar2 = other.C[nu:, :] + Dbar11 = other.D[:nu, :ny] + Dbar12 = other.D[:nu, ny:] + Dbar21 = other.D[nu:, :ny] + Dbar22 = other.D[nu:, ny:] + + # well-posed check + F = np.block([[np.eye(ny), -D22], [-Dbar11, np.eye(nu)]]) + if matrix_rank(F) != ny + nu: + raise ValueError("lft not well-posed to working precision.") + + # solve for the resulting ss by solving for [y, u] using [x, + # xbar] and [w1, w2]. + TH = np.linalg.solve(F, np.block( + [[C2, np.zeros((ny, other.states)), D21, np.zeros((ny, other.inputs - ny))], + [np.zeros((nu, self.states)), Cbar1, np.zeros((nu, self.inputs - nu)), Dbar12]] + )) + T11 = TH[:ny, :self.states] + T12 = TH[:ny, self.states: self.states + other.states] + T21 = TH[ny:, :self.states] + T22 = TH[ny:, self.states: self.states + other.states] + H11 = TH[:ny, self.states + other.states: self.states + other.states + self.inputs - nu] + H12 = TH[:ny, self.states + other.states + self.inputs - nu:] + H21 = TH[ny:, self.states + other.states: self.states + other.states + self.inputs - nu] + H22 = TH[ny:, self.states + other.states + self.inputs - nu:] + + Ares = np.block([ + [A + B2.dot(T21), B2.dot(T22)], + [Bbar1.dot(T11), Abar + Bbar1.dot(T12)] + ]) + + Bres = np.block([ + [B1 + B2.dot(H21), B2.dot(H22)], + [Bbar1.dot(H11), Bbar2 + Bbar1.dot(H12)] + ]) + + Cres = np.block([ + [C1 + D12.dot(T21), D12.dot(T22)], + [Dbar21.dot(T11), Cbar2 + Dbar21.dot(T12)] + ]) + + Dres = np.block([ + [D11 + D12.dot(H21), D12.dot(H22)], + [Dbar21.dot(H11), Dbar22 + Dbar21.dot(H12)] + ]) + return StateSpace(Ares, Bres, Cres, Dres, dt) + def minreal(self, tol=0.0): """Calculate a minimal realization, removes unobservable and uncontrollable states""" From 45638a0794e2ee5c48786935771d18b6104858b7 Mon Sep 17 00:00:00 2001 From: Richard Murray Date: Fri, 4 Jan 2019 20:24:36 +0800 Subject: [PATCH 4/5] Update control/statesp.py Co-Authored-By: hungpham2511 --- control/statesp.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/control/statesp.py b/control/statesp.py index 6fb473d32..e581db3c5 100644 --- a/control/statesp.py +++ b/control/statesp.py @@ -638,7 +638,7 @@ def lft(self, other, nu=-1, ny=-1): timebaseEqual(self, other): dt = self.dt # use dt from first argument else: - raise ValueError("Systems have different sampling times") + raise ValueError("Systems have different time bases") # submatrices A = self.A From 5955fd21f71a98c65b31e734a7b87d6c9544ec4f Mon Sep 17 00:00:00 2001 From: hung Date: Sat, 5 Jan 2019 01:33:35 +0800 Subject: [PATCH 5/5] Update lft definition ref --- control/statesp.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/control/statesp.py b/control/statesp.py index e581db3c5..1152b5a7d 100644 --- a/control/statesp.py +++ b/control/statesp.py @@ -610,7 +610,11 @@ def feedback(self, other=1, sign=-1): def lft(self, other, nu=-1, ny=-1): """Return the Linear Fractional Transformation. - See definition here: + A definition of the LFT operator can be found in Appendix A.7, + page 512 in the 2nd Edition, Multivariable Feedback Control by + Sigurd Skogestad. + + An alternative definition can be found here: https://www.mathworks.com/help/control/ref/lft.html Parameters @@ -621,6 +625,7 @@ def lft(self, other, nu=-1, ny=-1): Dimension of (plant) measurement output. nu: int, optional Dimension of (plant) control input. + """ other = _convertToStateSpace(other) # maximal values for nu, ny