Skip to content

Commit baf444d

Browse files
author
Kevin Chen
committed
Added TestStatefbk.py, which currently tests obsv and ctrb. Also, obsv and ctrb had their algorithms backwards, so I fixed this.
1 parent 6ee2cb4 commit baf444d

2 files changed

Lines changed: 39 additions & 3 deletions

File tree

‎src/TestStatefbk.py‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env python
2+
3+
from statefbk import ctrb, obsv, place, lqr
4+
import numpy as N
5+
import unittest
6+
7+
class TestStatefbk(unittest.TestCase):
8+
def testCtrbSISO(self):
9+
A = N.matrix("1. 2.; 3. 4.")
10+
B = N.matrix("5.; 7.")
11+
Wctrue = N.matrix("5. 19.; 7. 43.")
12+
Wc = ctrb(A,B)
13+
N.testing.assert_array_almost_equal(Wc, Wctrue)
14+
15+
def testCtrbMIMO(self):
16+
A = N.matrix("1. 2.; 3. 4.")
17+
B = N.matrix("5. 6.; 7. 8.")
18+
Wctrue = N.matrix("5. 6. 19. 22.; 7. 8. 43. 50.")
19+
Wc = ctrb(A,B)
20+
N.testing.assert_array_almost_equal(Wc, Wctrue)
21+
22+
def testObsvSISO(self):
23+
A = N.matrix("1. 2.; 3. 4.")
24+
C = N.matrix("5. 7.")
25+
Wotrue = N.matrix("5. 7.; 26. 38.")
26+
Wo = obsv(A,C)
27+
N.testing.assert_array_almost_equal(Wo, Wotrue)
28+
29+
def testObsvMIMO(self):
30+
A = N.matrix("1. 2.; 3. 4.")
31+
C = N.matrix("5. 6.; 7. 8.")
32+
Wotrue = N.matrix("5. 6.; 7. 8.; 23. 34.; 31. 46.")
33+
Wo = obsv(A,C)
34+
N.testing.assert_array_almost_equal(Wo, Wotrue)
35+
36+
if __name__ == '__main__':
37+
unittest.main()

‎src/statefbk.py‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,11 +205,10 @@ def ctrb(A,B):
205205
amat = np.mat(A)
206206
bmat = np.mat(B)
207207
n = np.shape(amat)[0]
208-
209208
# Construct the controllability matrix
210209
ctrb = bmat
211210
for i in range(1, n):
212-
ctrb = np.vstack((ctrb, amat**i*bmat))
211+
ctrb = np.hstack((ctrb, amat**i*bmat))
213212
return ctrb
214213

215214
def obsv(A, C):
@@ -236,5 +235,5 @@ def obsv(A, C):
236235
# Construct the controllability matrix
237236
obsv = cmat
238237
for i in range(1, n):
239-
obsv = np.hstack((obsv, cmat*amat**i))
238+
obsv = np.vstack((obsv, cmat*amat**i))
240239
return obsv

0 commit comments

Comments
 (0)