From: <kk...@us...> - 2011-02-08 22:12:59
|
Revision: 38 http://python-control.svn.sourceforge.net/python-control/?rev=38&view=rev Author: kkchen Date: 2011-02-08 22:12:53 +0000 (Tue, 08 Feb 2011) Log Message: ----------- Added modelsimp.py with interfaces for hsvd, era, and markov functions. Also, modified TestStatefbk.py so that we import numpy as np, as in other files. Steven Brunton <sbr...@pr...> Modified Paths: -------------- branches/control-0.4a/src/TestStatefbk.py Added Paths: ----------- branches/control-0.4a/src/modelsimp.py Modified: branches/control-0.4a/src/TestStatefbk.py =================================================================== --- branches/control-0.4a/src/TestStatefbk.py 2011-02-08 22:11:21 UTC (rev 37) +++ branches/control-0.4a/src/TestStatefbk.py 2011-02-08 22:12:53 UTC (rev 38) @@ -2,68 +2,68 @@ from statefbk import ctrb, obsv, place, lqr, gram from matlab import * -import numpy as N +import numpy as np import unittest class TestStatefbk(unittest.TestCase): def testCtrbSISO(self): - A = N.matrix("1. 2.; 3. 4.") - B = N.matrix("5.; 7.") - Wctrue = N.matrix("5. 19.; 7. 43.") + A = np.matrix("1. 2.; 3. 4.") + B = np.matrix("5.; 7.") + Wctrue = np.matrix("5. 19.; 7. 43.") Wc = ctrb(A,B) - N.testing.assert_array_almost_equal(Wc, Wctrue) + np.testing.assert_array_almost_equal(Wc, Wctrue) def testCtrbMIMO(self): - A = N.matrix("1. 2.; 3. 4.") - B = N.matrix("5. 6.; 7. 8.") - Wctrue = N.matrix("5. 6. 19. 22.; 7. 8. 43. 50.") + A = np.matrix("1. 2.; 3. 4.") + B = np.matrix("5. 6.; 7. 8.") + Wctrue = np.matrix("5. 6. 19. 22.; 7. 8. 43. 50.") Wc = ctrb(A,B) - N.testing.assert_array_almost_equal(Wc, Wctrue) + np.testing.assert_array_almost_equal(Wc, Wctrue) def testObsvSISO(self): - A = N.matrix("1. 2.; 3. 4.") - C = N.matrix("5. 7.") - Wotrue = N.matrix("5. 7.; 26. 38.") + A = np.matrix("1. 2.; 3. 4.") + C = np.matrix("5. 7.") + Wotrue = np.matrix("5. 7.; 26. 38.") Wo = obsv(A,C) - N.testing.assert_array_almost_equal(Wo, Wotrue) + np.testing.assert_array_almost_equal(Wo, Wotrue) def testObsvMIMO(self): - A = N.matrix("1. 2.; 3. 4.") - C = N.matrix("5. 6.; 7. 8.") - Wotrue = N.matrix("5. 6.; 7. 8.; 23. 34.; 31. 46.") + A = np.matrix("1. 2.; 3. 4.") + C = np.matrix("5. 6.; 7. 8.") + Wotrue = np.matrix("5. 6.; 7. 8.; 23. 34.; 31. 46.") Wo = obsv(A,C) - N.testing.assert_array_almost_equal(Wo, Wotrue) + np.testing.assert_array_almost_equal(Wo, Wotrue) def testCtrbObsvDuality(self): - A = N.matrix("1.2 -2.3; 3.4 -4.5") - B = N.matrix("5.8 6.9; 8. 9.1") + A = np.matrix("1.2 -2.3; 3.4 -4.5") + B = np.matrix("5.8 6.9; 8. 9.1") Wc = ctrb(A,B); - A = N.transpose(A) - C = N.transpose(B) - Wo = N.transpose(obsv(A,C)); - N.testing.assert_array_almost_equal(Wc,Wo) + A = np.transpose(A) + C = np.transpose(B) + Wo = np.transpose(obsv(A,C)); + np.testing.assert_array_almost_equal(Wc,Wo) def testGramWc(self): - A = N.matrix("1. -2.; 3. -4.") - B = N.matrix("5. 6.; 7. 8.") - C = N.matrix("4. 5.; 6. 7.") - D = N.matrix("13. 14.; 15. 16.") + A = np.matrix("1. -2.; 3. -4.") + B = np.matrix("5. 6.; 7. 8.") + C = np.matrix("4. 5.; 6. 7.") + D = np.matrix("13. 14.; 15. 16.") # sys = ss(A, B, C, D) sys = 1. - Wctrue = N.matrix("18.5 24.5; 24.5 32.5") + Wctrue = np.matrix("18.5 24.5; 24.5 32.5") Wc = gram(sys,'c') - N.testing.assert_array_almost_equal(Wc, Wctrue) + np.testing.assert_array_almost_equal(Wc, Wctrue) def testGramWo(self): - A = N.matrix("1. -2.; 3. -4.") - B = N.matrix("5. 6.; 7. 8.") - C = N.matrix("4. 5.; 6. 7.") - D = N.matrix("13. 14.; 15. 16.") + A = np.matrix("1. -2.; 3. -4.") + B = np.matrix("5. 6.; 7. 8.") + C = np.matrix("4. 5.; 6. 7.") + D = np.matrix("13. 14.; 15. 16.") sys = ss(A, B, C, D) sys = 1. - Wotrue = N.matrix("257.5 -94.5; -94.5 56.5") + Wotrue = np.matrix("257.5 -94.5; -94.5 56.5") Wo = gram(sys,'o') - N.testing.assert_array_almost_equal(Wo, Wotrue) + np.testing.assert_array_almost_equal(Wo, Wotrue) if __name__ == '__main__': unittest.main() Added: branches/control-0.4a/src/modelsimp.py =================================================================== --- branches/control-0.4a/src/modelsimp.py (rev 0) +++ branches/control-0.4a/src/modelsimp.py 2011-02-08 22:12:53 UTC (rev 38) @@ -0,0 +1,115 @@ +# modelsimp.py - tools for model simplification +# +# Author: Steve Brunton, Kevin Chen, Lauren Padilla +# Date: 30 Nov 2010 +# +# This file contains routines for obtaining reduced order models +# +# Copyright (c) 2010 by California Institute of Technology +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# 3. Neither the name of the California Institute of Technology nor +# the names of its contributors may be used to endorse or promote +# products derived from this software without specific prior +# written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CALTECH +# OR THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT +# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +# SUCH DAMAGE. +# +# $Id$ + +# External packages and modules +import numpy as np +import ctrlutil +from control.exception import * + +# Hankel Singular Value Decomposition +# The following returns the Hankel singular values, which are singular values of the matrix formed by multiplying the controllability and observability grammians +def hsvd(sys): + """Calculate the Hankel singular values + + Usage + ===== + H = hsvd(sys) + + Inputs + ------ + sys : a state space system + + Outputs + ------- + H : a list of Hankel singular values + + """ + + # Make sure that SLICOT is installed + try: + from slycot import sb01bd + except ImportError: + raise ControlSlycot("can't find slycot module 'sb01bd'") + + H = 1. + # Return the Hankel singular values + return H + +def era(YY,m,n,nin,nout,r): + """Calculate an ERA model of order r based on the impulse-response data YY + + Usage + ===== + sys = era(YY,m,n,nin,nout,r) + + Inputs + ------ + YY : nout x nin dimensional impulse-response data + m : number of rows in Hankel matrix + n : number of columns in Hankel matrix + nin : number of input variables + nout : number of output variables + r : order of model + + Outputs + ------- + sys : a reduced order model sys=ss(Ar,Br,Cr,Dr) + + """ +def markov(Y,U,M): + """Calculate the first M Markov parameters [D CB CAB ...] from input U, output Y + + Usage + ===== + H = markov(Y,U,M) + + Inputs + ------ + Y : output data + U : input data + M : number of Markov parameters to output + + Outputs + ------- + H : first M Markov parameters + + """ + This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |