Skip to content

Commit 29e7f95

Browse files
committed
Numerical improvements in statefbk.py
* `numpy.linalg.solve` instead of matrix inversion. * Better controllability matrix rank check.
1 parent 8285cbf commit 29e7f95

1 file changed

Lines changed: 4 additions & 3 deletions

File tree

control/statefbk.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def acker(A, B, poles):
127127

128128
# Make sure the system is controllable
129129
ct = ctrb(A, B)
130-
if sp.linalg.det(ct) == 0:
130+
if np.linalg.matrix_rank(ct) != a.shape[0]:
131131
raise ValueError("System not reachable; pole placement invalid")
132132

133133
# Compute the desired characteristic polynomial
@@ -138,7 +138,7 @@ def acker(A, B, poles):
138138
pmat = p[n-1]*a**0
139139
for i in np.arange(1,n):
140140
pmat = pmat + p[n-i-1]*a**i
141-
K = sp.linalg.inv(ct) * pmat
141+
K = np.linalg.solve(ct, pmat)
142142

143143
K = K[-1][:] # Extract the last row
144144
return K
@@ -242,7 +242,8 @@ def lqr(*args, **keywords):
242242
X,rcond,w,S,U,A_inv = sb02md(nstates, A_b, G, Q_b, 'C')
243243

244244
# Now compute the return value
245-
K = np.dot(np.linalg.inv(R), (np.dot(B.T, X) + N.T));
245+
# We assume that R is positive definite and, hence, invertible
246+
K = np.linalg.solve(R, np.dot(B.T, X) + N.T);
246247
S = X;
247248
E = w[0:nstates];
248249

0 commit comments

Comments
 (0)