Skip to content

Commit 49bdd94

Browse files
author
Kevin Chen
committed
Added shell interface for gram along with tests.
Steven Brunton <sbrunton@princeton.edu>
1 parent 007123c commit 49bdd94

2 files changed

Lines changed: 47 additions & 5 deletions

File tree

‎src/TestStatefbk.py‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python
22

3-
from statefbk import ctrb, obsv, place, lqr
3+
from statefbk import ctrb, obsv, place, lqr, gram
4+
from matlab import *
45
import numpy as N
56
import unittest
67

@@ -42,5 +43,27 @@ def testCtrbObsvDuality(self):
4243
Wo = N.transpose(obsv(A,C));
4344
N.testing.assert_array_almost_equal(Wc,Wo)
4445

46+
def testGramWc(self):
47+
A = N.matrix("1. -2.; 3. -4.")
48+
B = N.matrix("5. 6.; 7. 8.")
49+
C = N.matrix("4. 5.; 6. 7.")
50+
D = N.matrix("13. 14.; 15. 16.")
51+
# sys = ss(A, B, C, D)
52+
sys = 1.
53+
Wctrue = N.matrix("18.5 24.5; 24.5 32.5")
54+
Wc = gram(sys,'c')
55+
N.testing.assert_array_almost_equal(Wc, Wctrue)
56+
57+
def testGramWo(self):
58+
A = N.matrix("1. -2.; 3. -4.")
59+
B = N.matrix("5. 6.; 7. 8.")
60+
C = N.matrix("4. 5.; 6. 7.")
61+
D = N.matrix("13. 14.; 15. 16.")
62+
sys = ss(A, B, C, D)
63+
sys = 1.
64+
Wotrue = N.matrix("257.5 -94.5; -94.5 56.5")
65+
Wo = gram(sys,'o')
66+
N.testing.assert_array_almost_equal(Wo, Wotrue)
67+
4568
if __name__ == '__main__':
4669
unittest.main()

‎src/statefbk.py‎

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,15 +190,15 @@ def ctrb(A,B):
190190
191191
Usage
192192
=====
193-
Wc = ctrb(A, B)
193+
C = ctrb(A, B)
194194
195195
Inputs
196196
------
197197
A, B: Dynamics and input matrix of the system
198198
199199
Outputs
200200
-------
201-
Wc: Controllability matrix
201+
C: Controllability matrix
202202
"""
203203

204204
# Convert input parameters to matrices (if they aren't already)
@@ -216,15 +216,15 @@ def obsv(A, C):
216216
217217
Usage
218218
=====
219-
Wc = obsv(A, C)
219+
O = obsv(A, C)
220220
221221
Inputs
222222
------
223223
A, C: Dynamics and output matrix of the system
224224
225225
Outputs
226226
-------
227-
Wc: Observability matrix
227+
O: Observability matrix
228228
"""
229229

230230
# Convert input parameters to matrices (if they aren't already)
@@ -237,3 +237,22 @@ def obsv(A, C):
237237
for i in range(1, n):
238238
obsv = np.vstack((obsv, cmat*amat**i))
239239
return obsv
240+
241+
def gram(sys,type):
242+
"""Gramian
243+
244+
Usage
245+
=====
246+
Wc = gram(sys,'c')
247+
Wo = gram(sys,'o')
248+
"""
249+
250+
if type=='c':
251+
print "controllable"
252+
elif type=='o':
253+
print "observable"
254+
else:
255+
raise ValueError, "Oops, neither observable, nor controllable!"
256+
257+
gram = 0.
258+
return gram

0 commit comments

Comments
 (0)