Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update prescale function #465

Closed
wants to merge 11 commits into from
37 changes: 23 additions & 14 deletions src/matrix_comps.jl
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ end
syst = similarity_transform(sys, T)
Perform a similarity transform `T : Tx̃ = x` on `sys` such that
```
= T⁻¹AT
à = T⁻¹AT
B̃ = T⁻¹ B
C̃ = CT
D̃ = D
Expand All @@ -585,24 +585,33 @@ end

"""
syst, S = prescale(sys)
Perform a eigendecomposition on system state-transition matrix `sys.A`.

Balances the metasystem matrix
```
à = S⁻¹AS
B̃ = S⁻¹ B
C̃ = CS
D̃ = D
M = [A B;
C 0]
```
Such that `Ã` is diagonal.
such that their column and row norms are closer.
This results in a system with overall better numerical conditioning.

Returns a new scaled state-space object and the associated transformation
matrix.
"""
function prescale(sys::StateSpace)
d, S = eigen(sys.A)
A = Diagonal(d)
B = S\sys.B
C = sys.C*S
normalized_sys = iscontinuous(sys) ? ss(A, B, C, sys.D) : ss(A, B, C, sys.D, sys.Ts)
return normalized_sys, S
function prescale(sys::StateSpace) where ST <: AbstractStateSpace
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
function prescale(sys::StateSpace) where ST <: AbstractStateSpace
function prescale(sys::ST) where ST <: AbstractStateSpace

n, m = size(sys.B)
p = size(sys.C, 1)

# create an augmented system to balance the whole system at once
metasys = [sys.A sys.B zeros(n, n-m); sys.C zeros(p, n)]
S, P, B = balance(metasys)
T = S*P # permutation matrix to be returned later

# reconstruct A, B and C by taking slices of the balanced metasystem
bal_metasys = P\B*P # undo permutation
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to use perm=false to avoid having to undo the permutation?

A = bal_metasys[1:n, 1:n]
B = bal_metasys[1:n, n+1:n+m]
C = bal_metasys[n+1:end, 1:n]
return ST(A, B, C, sys.D, sys.timeevol), T
end

"""
Expand Down