ssBalanced = StateSpace.Transformation.toBalancedForm(ss);
Balancing a linear dynamic system in state space form ss means to find a state transformation x_new = T*x = diagonal(scale)*x so that the transformed system is better suited for numerical algorithms. In more detail:
This function performs a similarity transformation with T=diagonal(scale) such that S_scale
| inv(T)*A*T inv(T)*B | S_scale = | | | C*T 0 |
has a better condition then the system matrix S
| A B | S = | | | C 0 |
that is, conditionNumber(S_scale) ≤ conditionNumber(S). The elements of vector scale are multiples of 2 which means that this function does not introduce round-off errors.
import Modelica.Math.Matrices.norm; ss = Modelica_LinearSystems2.StateSpace(A=[1, -10, 1000; 0.01, 0, 10; 0.005, -0.01, 10], B=[100, 10; 1,0; -0.003, 1], C=[-0.5, 1, 100], D=[0,0]); sb = Modelica_LinearSystems2.StateSpace.Transformation.toBalancedForm(ss); // norm(ss.A) = 1000.15, norm(ss.B) = 100.504, norm(ss.C) = 100.006 // norm(sb.A) = 10.8738, norm(sb.B) = 16.0136, norm(sb.C) = 10.2011
The algorithm is taken from
which is based on the balance
function from EISPACK.
encapsulated function toBalancedForm import Modelica; import Modelica_LinearSystems2; import Modelica_LinearSystems2.StateSpace; input StateSpace ss "State space system"; output StateSpace ssBalanced(redeclare Real A[size(ss.A, 1), size(ss.A, 2)], redeclare Real B[size(ss.B, 1), size(ss.B, 2)], redeclare Real C[size(ss.C, 1), size(ss.C, 2)], redeclare Real D[size(ss.D, 1), size(ss.D, 2)]) "Balanced state space system"; end toBalancedForm;