|
55 | 55 | dot, empty, exp, eye, matrix, ones, pi, poly, poly1d, roots, shape, sin, \
|
56 | 56 | zeros, squeeze
|
57 | 57 | from numpy.random import rand, randn
|
58 |
| -from numpy.linalg import inv, det, solve |
| 58 | +from numpy.linalg import solve, eigvals, matrix_rank |
59 | 59 | from numpy.linalg.linalg import LinAlgError
|
60 | 60 | from scipy.signal import lti, cont2discrete
|
61 | 61 | # from exceptions import Exception
|
@@ -405,7 +405,7 @@ def freqresp(self, omega):
|
405 | 405 | def pole(self):
|
406 | 406 | """Compute the poles of a state space system."""
|
407 | 407 |
|
408 |
| - return roots(poly(self.A)) |
| 408 | + return eigvals(self.A) |
409 | 409 |
|
410 | 410 | def zero(self):
|
411 | 411 | """Compute the zeros of a state space system."""
|
@@ -452,21 +452,24 @@ def feedback(self, other=1, sign=-1):
|
452 | 452 | D2 = other.D
|
453 | 453 |
|
454 | 454 | F = eye(self.inputs) - sign * D2 * D1
|
455 |
| - if abs(det(F)) < 1.e-6: |
| 455 | + if matrix_rank(F) != self.inputs: |
456 | 456 | raise ValueError("I - sign * D2 * D1 is singular.")
|
457 | 457 |
|
458 |
| - E = inv(F) |
459 |
| - T1 = eye(self.outputs) + sign * D1 * E * D2 |
460 |
| - T2 = eye(self.inputs) + sign * E * D2 * D1 |
| 458 | + # Precompute F\D2 and F\C2 (E = inv(F)) |
| 459 | + E_D2 = solve(F, D2) |
| 460 | + E_C2 = solve(F, C2) |
| 461 | + |
| 462 | + T1 = eye(self.outputs) + sign * D1 * E_D2 |
| 463 | + T2 = eye(self.inputs) + sign * E_D2 * D1 |
461 | 464 |
|
462 | 465 | A = concatenate(
|
463 | 466 | (concatenate(
|
464 |
| - (A1 + sign * B1 * E * D2 * C1, sign * B1 * E * C2), axis=1), |
| 467 | + (A1 + sign * B1 * E_D2 * C1, sign * B1 * E_C2), axis=1), |
465 | 468 | concatenate(
|
466 |
| - (B2 * T1 * C1, A2 + sign * B2 * D1 * E * C2), axis=1)), |
| 469 | + (B2 * T1 * C1, A2 + sign * B2 * D1 * E_C2), axis=1)), |
467 | 470 | axis=0)
|
468 | 471 | B = concatenate((B1 * T2, B2 * D1 * T2), axis=0)
|
469 |
| - C = concatenate((T1 * C1, sign * D1 * E * C2), axis=1) |
| 472 | + C = concatenate((T1 * C1, sign * D1 * E_C2), axis=1) |
470 | 473 | D = D1 * T2
|
471 | 474 |
|
472 | 475 | return StateSpace(A, B, C, D, dt)
|
|
0 commit comments