(scale,As,Bs,Cs) = Matrices.balanceABC(A,B,C); (scale,As,Bs) = Matrices.balanceABC(A,B); (scale,As,,Cs) = Matrices.balanceABC(A,C=C);
This function returns a vector scale, such that with T = diagonal(scale) system matrix S_scale
| inv(T)*A*T inv(T)*B | S_scale = | | | C*T 0 |
has a better condition then 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.
Balancing a linear dynamic system in state space form
der(x) = A*x + B*u y = C*x + D*u
means to find a state transformation x_new = T*x = diagonal(scale)*x so that the transformed system is better suited for numerical algorithms.
import Modelica.Math.Matrices; 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]; (scale, As, Bs, Cs) := Matrices.balanceABC(A,B,C); T = diagonal(scale); Diff = [Matrices.inv(T)*A*T, Matrices.inv(T)*B; C*T, zeros(1,2)] - [As, Bs; Cs, zeros(1,2)]; err = Matrices.norm(Diff); // scale = {16, 1, 0.0625} // norm(A) = 1000.15, norm(B) = 100.504, norm(C) = 100.006 // norm(As) = 10.8738, norm(Bs) = 16.0136, norm(Cs) = 10.2011 // err = 0
The algorithm is taken from
which is based on the balance
function from EISPACK.
function balanceABC extends Modelica.Icons.Function; input Real A[:, size(A, 1)] "System matrix A"; input Real B[size(A, 1), :] = fill(0.0, size(A, 1), 0) "System matrix B (need not be present)"; input Real C[:, size(A, 1)] = fill(0.0, 0, size(A, 1)) "System matrix C (need not be present)"; output Real scale[size(A, 1)] "diagonal(scale)=T is such that [inv(T)*A*T, inv(T)*B; C*T, 0] has smaller condition as [A,B;C,0]"; output Real As[size(A, 1), size(A, 1)] "Balanced matrix A (= inv(T)*A*T )"; output Real Bs[size(A, 1), size(B, 2)] "Balanced matrix B (= inv(T)*B )"; output Real Cs[size(C, 1), size(A, 1)] "Balanced matrix C (= C*T )"; end balanceABC;