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<pre>\n    x = A * pre(x) + B * u\n    y = C * pre(x) + D * u\n</pre>\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<pre>\n        A has the dimension: A(nx,nx),\n        B has the dimension: B(nx,nu),\n        C has the dimension: C(ny,nx),\n        D has the dimension: D(ny,nu)\n</pre>\n<p>\nExample:\n</p>\n<pre>\n     parameter: A = [0.12, 2;3, 1.5]\n     parameter: B = [2, 7;3, 1]\n     parameter: C = [0.1, 2]\n     parameter: 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>\n</html>"),
        Icon(
            coordinateSystem(
                preserveAspectRatio = true,
                extent = {
                    {-100, -100}, 
                    {100, 100}}),
            graphics = {
                Text(
                    extent = {
                        {-90, 15}, 
                        {-15, 90}},
                    textString = "A",
                    lineColor = {0, 0, 127}), 
                Text(
                    extent = {
                        {15, 15}, 
                        {90, 90}},
                    textString = "B",
                    lineColor = {0, 0, 127}), 
                Text(
                    extent = {
                        {-52, 28}, 
                        {54, -20}},
                    textString = "z",
                    lineColor = {0, 0, 127}), 
                Text(
                    extent = {
                        {-90, -15}, 
                        {-15, -90}},
                    textString = "C",
                    lineColor = {0, 0, 127}), 
                Text(
                    extent = {
                        {15, -15}, 
                        {90, -90}},
                    textString = "D",
                    lineColor = {0, 0, 127})}),
        Diagram(
            coordinateSystem(
                preserveAspectRatio = true,
                extent = {
                    {-100, -100}, 
                    {100, 100}}),
            graphics = {
                Rectangle(
                    extent = {
                        {-60, 60}, 
                        {60, -60}},
                    lineColor = {0, 0, 255}), 
                Text(
                    extent = {
                        {-54, 50}, 
                        {52, -10}},
                    textString = "zx=Ax+Bu"), 
                Text(
                    extent = {
                        {-56, 14}, 
                        {54, -50}},
                    textString = "  y=Cx+Du"), 
                Line(
                    points = {
                        {-102, 0}, 
                        {-60, 0}},
                    color = {0, 0, 255}), 
                Line(
                    points = {
                        {60, 0}, 
                        {100, 0}},
                    color = {0, 0, 255})}));
end StateSpace;