StateSpace

block StateSpace "Discrete State Space block"
    parameter Real A[:,size(A, 1)] = [1,0; 0,1] "Matrix A of state space model";
    parameter Real B[size(A, 1),:] = [1; 1] "Matrix B of state space model";
    parameter Real C[:,size(A, 1)] = [1,1] "Matrix C of state space model";
    parameter Real D[size(C, 1),size(B, 2)] = zeros(size(C, 1), size(B, 2)) "Matrix D of state space model";

    extends Interfaces.DiscreteMIMO(final nin = size(B, 2), final nout = size(C, 1));

    output Real x[size(A, 1)] "State vector";
equation
    when sampleTrigger then 
        x = A * pre(x) + B * u;
        y = C * pre(x) + D * u;
    end when;

    annotation (
        Documentation(info = "<html>\n<p>\nThe <strong>discrete state space</strong> block defines the relation\nbetween the input u and the output y in state space form:\n</p>\n<blockquote><pre>\nx = A * pre(x) + B * u\ny = C * pre(x) + D * u\n</pre></blockquote>\n<p>\nwhere pre(x) is the value of the discrete state x at\nthe previous sample time instant.\nThe input is a vector of length nu, the output is a vector\nof length ny and nx is the number of states. Accordingly\n</p>\n<blockquote><pre>\nA has the dimension: A(nx,nx),\nB has the dimension: B(nx,nu),\nC has the dimension: C(ny,nx),\nD has the dimension: D(ny,nu)\n</pre></blockquote>\n<p>\nExample:\n</p>\n<blockquote><pre>\nparameter: A = [0.12, 2;3, 1.5]\nparameter: B = [2, 7;3, 1]\nparameter: C = [0.1, 2]\nparameter: D = zeros(ny,nu)\n\nresults in the following equations:\n  [x[1]]   [0.12  2.00] [pre(x[1])]   [2.0  7.0] [u[1]]\n  [    ] = [          ]*[         ] + [        ]*[    ]\n  [x[2]]   [3.00  1.50] [pre(x[2])]   [0.1  2.0] [u[2]]\n                             [pre(x[1])]            [u[1]]\n       y[1]   = [0.1  2.0] * [         ] + [0  0] * [    ]\n                             [pre(x[2])]            [u[2]]\n</pre></blockquote>\n</html>"),
        Icon(
            coordinateSystem(
                preserveAspectRatio = true,
                extent = {
                    {-100, -100}, 
                    {100, 100}}),
            graphics = {
                Text(
                    extent = {
                        {-90, 15}, 
                        {-15, 90}},
                    textString = "A",
                    textColor = {0, 0, 127}), 
                Text(
                    extent = {
                        {15, 15}, 
                        {90, 90}},
                    textString = "B",
                    textColor = {0, 0, 127}), 
                Text(
                    extent = {
                        {-52, 28}, 
                        {54, -20}},
                    textString = "z",
                    textColor = {0, 0, 127}), 
                Text(
                    extent = {
                        {-90, -15}, 
                        {-15, -90}},
                    textString = "C",
                    textColor = {0, 0, 127}), 
                Text(
                    extent = {
                        {15, -15}, 
                        {90, -90}},
                    textString = "D",
                    textColor = {0, 0, 127})}));
end StateSpace;