Skip to content

Commit

Permalink
better error messages for failed inversion
Browse files Browse the repository at this point in the history
  • Loading branch information
baggepinnen committed Dec 6, 2023
1 parent b51adf7 commit 313d2b4
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
6 changes: 3 additions & 3 deletions lib/ControlSystemsBase/src/discrete.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ ZoH sampling is exact for linear systems with piece-wise constant inputs (step i
FoH sampling is exact for linear systems with piece-wise linear inputs (ramp invariant), this is a good choice for simulation of systems with smooth continuous inputs.
To approximate the behavior of a continuous-time system well in the frequency domain, the `:tustin` (trapezoidal / bilinear) method may be most appropriate. In this case, the pre-warping argument can be used to ensure that the frequency response of the discrete-time system matches the continuous-time system at a given frequency. The tustin transformation alters the meaning of the state components, while ZoH and FoH preserve the meaning of the state components. The Tustin method is commonly used to discretize a continuous-tiem controller.
To approximate the behavior of a continuous-time system well in the frequency domain, the `:tustin` (trapezoidal / bilinear) method may be most appropriate. In this case, the pre-warping argument can be used to ensure that the frequency response of the discrete-time system matches the continuous-time system at a given frequency. The tustin transformation alters the meaning of the state components, while ZoH and FoH preserve the meaning of the state components. The Tustin method is commonly used to discretize a continuous-time controller.
The forward-Euler method generally requires the sample time to be very small
relative to the time constants of the system, and its use is generally discouraged.
Expand Down Expand Up @@ -99,8 +99,8 @@ function d2c(sys::AbstractStateSpace{<:Discrete}, method::Symbol=:zoh; w_prewarp
ny, nu = size(sys)
nx = nstates(sys)
if method === :zoh
M = log([A B;
zeros(nu, nx) I])./sys.Ts
M = log(Matrix([A B;

Check warning on line 102 in lib/ControlSystemsBase/src/discrete.jl

View check run for this annotation

Codecov / codecov/patch

lib/ControlSystemsBase/src/discrete.jl#L102

Added line #L102 was not covered by tests
zeros(nu, nx) I]))./sys.Ts
Ac = M[1:nx, 1:nx]
Bc = M[1:nx, nx+1:nx+nu]
if eltype(A) <: Real
Expand Down
3 changes: 2 additions & 1 deletion lib/ControlSystemsBase/src/types/StateSpace.jl
Original file line number Diff line number Diff line change
Expand Up @@ -431,10 +431,11 @@ end
function /(n::Number, sys::ST) where ST <: AbstractStateSpace
# Ensure s.D is invertible
A, B, C, D = ssdata(sys)
size(D, 1) == size(D, 2) || error("The inverted system must have the same number of inputs and outputs")

Check warning on line 434 in lib/ControlSystemsBase/src/types/StateSpace.jl

View check run for this annotation

Codecov / codecov/patch

lib/ControlSystemsBase/src/types/StateSpace.jl#L434

Added line #L434 was not covered by tests
Dinv = try
inv(D)
catch
error("D isn't invertible")
error("D isn't invertible. If you are trying to form a quotient between two systems `N(s) / D(s)` where the quotient is proper but the inverse of `D(s)` isn't, consider calling `N / D` instead of `N * inv(D)")

Check warning on line 438 in lib/ControlSystemsBase/src/types/StateSpace.jl

View check run for this annotation

Codecov / codecov/patch

lib/ControlSystemsBase/src/types/StateSpace.jl#L438

Added line #L438 was not covered by tests
end
return basetype(ST)(A - B*Dinv*C, B*Dinv, -n*Dinv*C, n*Dinv, sys.timeevol)
end
Expand Down

0 comments on commit 313d2b4

Please sign in to comment.