Skip to content

Commit 95ab229

Browse files
committed
improved time to create controllability matrix, fix to obsv function + parameters
1 parent 5692317 commit 95ab229

1 file changed

Lines changed: 20 additions & 9 deletions

File tree

control/statefbk.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -990,13 +990,15 @@ def _control_output(t, states, inputs, params):
990990
return ctrl, closed
991991

992992

993-
def ctrb(A, B):
993+
def ctrb(A, B, t=None):
994994
"""Controllabilty matrix.
995995
996996
Parameters
997997
----------
998998
A, B : array_like or string
999999
Dynamics and input matrix of the system
1000+
t : None or integer
1001+
maximum time horizon of the controllability matrix, max = n
10001002
10011003
Returns
10021004
-------
@@ -1016,11 +1018,17 @@ def ctrb(A, B):
10161018
amat = _ssmatrix(A)
10171019
bmat = _ssmatrix(B)
10181020
n = np.shape(amat)[0]
1021+
m = np.shape(bmat)[1]
1022+
1023+
if t is None or t > n:
1024+
t = n
10191025

10201026
# Construct the controllability matrix
1021-
ctrb = np.hstack(
1022-
[bmat] + [np.linalg.matrix_power(amat, i) @ bmat
1023-
for i in range(1, n)])
1027+
ctrb = np.zeros((n, t * m))
1028+
ctrb[:, :m] = cmat
1029+
for k in range(1, t):
1030+
ctrb[:, k * m:(k + 1) * m] = np.dot(amat, obsv[:, (k - 1) * m:k * m])
1031+
10241032
return _ssmatrix(ctrb)
10251033

10261034

@@ -1031,7 +1039,9 @@ def obsv(A, C, t=None):
10311039
----------
10321040
A, C : array_like or string
10331041
Dynamics and output matrix of the system
1034-
1042+
t : None or integer
1043+
maximum time horizon of the controllability matrix, max = n
1044+
10351045
Returns
10361046
-------
10371047
O : 2D array (or matrix)
@@ -1050,16 +1060,17 @@ def obsv(A, C, t=None):
10501060
amat = _ssmatrix(A)
10511061
cmat = _ssmatrix(C)
10521062
n = np.shape(amat)[0]
1063+
p = np.shape(cmat)[0]
10531064

1054-
if t is None:
1065+
if t is None or t > n:
10551066
t = n
10561067

10571068
# Construct the observability matrix
1058-
obsv = np.zeros((t * ny, n))
1059-
obsv[:ny, :] = c
1069+
obsv = np.zeros((t * p, n))
1070+
obsv[:p, :] = cmat
10601071

10611072
for k in range(1, t):
1062-
obsv[k * ny:(k + 1) * ny, :] = np.dot(obsv[(k - 1) * ny:k * ny, :], a)
1073+
obsv[k * p:(k + 1) * p, :] = np.dot(obsv[(k - 1) * p:k * p, :], amat)
10631074

10641075
return _ssmatrix(obsv)
10651076

0 commit comments

Comments
 (0)