## Modelica_LinearSystems2.Examples.StateSpace

### Package Content

Name | Description
--- | ---
analysis | Example to check controllability of a state space system
analysisControllability | Example to check controllability of a state space system
analysisObservability | Example to check controllability of a state space system
analysisControllablePoles | Example to check controllability of a state space system and print the controllable poles
analysisTimeResponse | Compute time response of a state space system
analysisInitialResponse | Initial response example
analysisImpulseResponse | Impulse response example
analysisStepResponse | Step response example
analysisPolesAndZeros_SISO | Compute poles and invariant zeros of a SISO state space system by transformation to a minmal system
analysisStairCase | Example to check controllability of a state space system
analysisInvariantZeros | Compute invariant zeros of transfer function
conversionFromZerosAndPoles | Transform a TransferFunction into a StateSpace description
conversionToTransferFunctionMIMO | Example to compute a transfer function matrix of a MIMO system from state space representation
conversionToTransferFunctionSISO | Example to compute a transfer function from SISO state space representation
conversionToZerosAndPolesMIMO | Example to compute a zeros-and-poles representation of a MIMO system from state space representation
conversionToZerosAndPolesSISO | Example to compute a zeros and poles representation from SISO state space representation
designAssignPolesSISO | Example for pole placing using Ackermann's method
designAssignPolesMIMO | Example for pole placing
designKalmanFilter | Description
designLQG | Description
designLQR | Description
plotPolesAndZeros | Example for plotting eigenvalues and invariant zeros of a state space system
plotBodeMIMO | Constructs zeros-and-poles-transfers from state space representation and plots the Bode diagrams with automatic determination of the frequency range to plot
plotBodeSISO | Constructs a zeros-and-poles-transfer function from state space representation and plots the Bode diagram with automatic determination of the frequency range to plot
plotTimeResponse | Time response plot example
plotImpulse | Impulse plot example
plotInital | Initial condition plot example
plotRamp | Plot ramp response
plotStep | Step plot example
plotZeros | case studies of systems with zeros
transformation | Example to demonstrate the transformation to Jordan- observabilitiy- and controllability canonical form
transformationExtract | Example how to extract input/output related subsystems from state space system record
transformationToIrreducibleForm | Example to compute the minimal state space realization of a given SISO state space realization

## Modelica_LinearSystems2.Examples.StateSpace.analysis

Example to check controllability of a state space system

### Information

This example shows the usage of function Modelica_LinearSystems2.StateSpace.Analysis.isControllable which is to check whether a system is controllable or not.

### Inputs

Type | Name | Default | Description
--- | --- | --- | ---
StateSpace | ssi | Modelica_LinearSystems2.Stat... |
AnalyseOptions | analyseOptions | Modelica_LinearSystems2.Inte... |
system data definition
Boolean | systemOnFile | false | true, if state space system is defined on file
String | fileName | "NoName" | file where matrix [A, B; C, D] is stored
String | matrixName | "ABCD" | Name of the state space system matrix

### Outputs

Type | Name | Description
--- | --- | ---
Boolean | ok |

### Modelica definition

    function analysis
      "Example to check controllability of a state space system"
      import Modelica_LinearSystems2;
      import Modelica_LinearSystems2.StateSpace;

      input StateSpace ssi=Modelica_LinearSystems2.StateSpace(
          A=[-3,2,-3,4,5,6; 0,6,7,8,9,4; 0,2,3,0,78,6; 0,1,2,2,3,3; 0,13,34,0,0,1; 0,
            0,0,-17,0,0],
          B=[1,0; 0,1; 1,0; 0,1; 1,0; 0,1],
          C=[0,0,1,0,1,0; 0,1,0,0,1,1],
          D=[0,0; 0,0],
          xNames={"x1","x2","x3","x4","x5","x6"},
          uNames={"u1","u2"}, yNames={"y1","y2"});
      input Internal.AnalyseOptions analyseOptions=
          Modelica_LinearSystems2.Internal.AnalyseOptions(
          plotEigenValues=true,
          plotInvariantZeros=true,
          plotStepResponse=true,
          plotFrequencyResponse=true,
          printEigenValues=true,
          printEigenValueProperties=true,
          printInvariantZeros=true,
          printControllability=true,
          printObservability=true,
          headingEigenValues="Eigenvalues",
          headingInvariantzeros="Invariant zeros",
          headingStepResponse="Step response",
          headingFrequencyResponse="Frequency response");

      input Boolean systemOnFile=false
        "true, if state space system is defined on file";
      input String fileName="NoName" "file where matrix [A, B; C, D] is stored";
      input String matrixName="ABCD" "Name of the state space system matrix";

      output Boolean ok;

    protected
      StateSpace ss=if systemOnFile then Modelica_LinearSystems2.StateSpace.Import.fromFile(fileName) else ssi;

    algorithm
      ok := false;
      StateSpace.Analysis.analysis(ss, fileName="analysis.html", analyseOptions=analyseOptions, description="Description of the system");
      ok := true;
    end analysis;

## Modelica_LinearSystems2.Examples.StateSpace.analysisControllability

Example to check controllability of a state space system

### Information

This example shows the usage of function Modelica_LinearSystems2.StateSpace.Analysis.isControllable which is to check whether a system is controllable or not.

### Inputs

Type | Name | Default | Description
--- | --- | --- | ---
StateSpace | ssi | Modelica_LinearSystems2.Stat... |
system data definition
Boolean | systemOnFile | false | true, if state space system is defined on file
String | fileName | "NoName" | file where matrix [A, B; C, D] is stored
String | matrixName | "ABCD" | Name of the state space system matrix

### Outputs

Type | Name | Description
--- | --- | ---
Boolean | ok |

### Modelica definition

    function analysisControllability
      "Example to check controllability of a state space system"
      import Modelica_LinearSystems2;
      import Modelica_LinearSystems2.StateSpace;
      import Modelica_LinearSystems2.Math.Matrices;
      import Modelica.Utilities.Streams.print;
      import Modelica_LinearSystems2.Types.StaircaseMethod;

      input StateSpace ssi=Modelica_LinearSystems2.StateSpace(
          A=[1,0,0,0,0,0; 1,0,0,0,0,0; 0,2,3,0,78,6; 1,1,2,2,3,3; 10,13,34,0,0,1; 0,
            0,0,2,0,0],
          B=[0,0; 0,0; 0,0; 0,0; 1,0; 0,0],
          C=[1,0,1,0,1,0; 0,1,0,1,0,1; 0,1,0,1,0,1],
         D=[0,0; 0,0; 0,0]);

      input Boolean systemOnFile=false
        "true, if state space system is defined on file";
      input String fileName="NoName" "file where matrix [A, B; C, D] is stored";
      input String matrixName="ABCD" "Name of the state space system matrix";

      output Boolean ok;

    protected
      StateSpace ss=if systemOnFile then Modelica_LinearSystems2.StateSpace.Import.fromFile(
                                                              fileName) else ssi;
      StaircaseMethod method;
      Boolean isControllable;
      Boolean isSISO=StateSpace.Internal.isSISO(ss);

    algorithm
      ok := false;

      method := StaircaseMethod.QR;
      isControllable := StateSpace.Analysis.isControllable(ss, method);

      if isSISO then
        if isControllable then
          Modelica.Utilities.Streams.print("pair (A, B) is controllable");
        else
          Modelica.Utilities.Streams.print("pair (A, B) is not controllable");
        end if;
      else
        if isControllable then
          Modelica.Utilities.Streams.print(
            "pair (A, B) is controllable by QR factorzation");
        else
          Modelica.Utilities.Streams.print(
            "pair (A, B) is not controllable by QR factorzation");
        end if;

        method := StaircaseMethod.SVD;
        isControllable := StateSpace.Analysis.isControllable(ss, method);
        if isControllable then
          Modelica.Utilities.Streams.print("pair (A, B) is controllable by SVD");
        else
          Modelica.Utilities.Streams.print("pair (A, B) is not controllable by SVD");
        end if;
      end if;

      ok := true;
    end analysisControllability;

## Modelica_LinearSystems2.Examples.StateSpace.analysisObservability

Example to check controllability of a state space system

### Information

This example shows the usage of function Modelica_LinearSystems2.StateSpace.Analysis.isObservable which is to check whether a system is observable or not.

### Inputs

Type | Name | Default | Description
--- | --- | --- | ---
StateSpace | ssi | Modelica_LinearSystems2.Stat... |
system data definition
Boolean | systemOnFile | false | true, if state space system is defined on file
String | fileName | "NoName" | file where matrix [A, B; C, D] is stored
String | matrixName | "ABCD" | Name of the state space system matrix

### Outputs

Type | Name | Description
--- | --- | ---
Boolean | ok |

### Modelica definition

    function analysisObservability
      "Example to check controllability of a state space system"
      import Modelica_LinearSystems2;
      import Modelica_LinearSystems2.StateSpace;
      import Modelica_LinearSystems2.Math.Matrices;
      import Modelica.Utilities.Streams.print;

       input StateSpace ssi=Modelica_LinearSystems2.StateSpace(
          A=[1,2,3,4,5,6; 5,6,7,8,9,4; 0,2,3,0,78,6; 1,1,2,2,3,3; 10,13,34,0,0,1; 0,
            0,0,2,0,1],
          B=[1,0; 2,0; 0,1; 0,2; 0,0; 0,1],
          C=[1,0,1,0,1,0; 0,1,0,0,0,0],
         D=[0,0; 0,0]);

      input Boolean systemOnFile=false
        "true, if state space system is defined on file";
      input String fileName="NoName" "file where matrix [A, B; C, D] is stored";
      input String matrixName="ABCD" "Name of the state space system matrix";

      output Boolean ok;

    protected
      StateSpace ss=if systemOnFile then Modelica_LinearSystems2.StateSpace.Import.fromFile(
                                                              fileName) else ssi;

      Boolean isObservable;
    Modelica_LinearSystems2.Types.StaircaseMethod method;
      Boolean isSISO=StateSpace.Internal.isSISO(ss);
    algorithm
      ok := false;

      method:=  Modelica_LinearSystems2.Types.StaircaseMethod.QR;
      isObservable := StateSpace.Analysis.isObservable(ss,method);

     if isSISO then
        if isObservable then
          Modelica.Utilities.Streams.print("pair (A, B) is observable");
        else
          Modelica.Utilities.Streams.print("pair (A, B) is not observable");
        end if;
      else
        if isObservable then
          Modelica.Utilities.Streams.print(
            "pair (A, B) is observable checked by QR factorzation");
        else
          Modelica.Utilities.Streams.print(
            "pair (A, B) is not observable checked by QR factorzation");
        end if;

        method := Modelica_LinearSystems2.Types.StaircaseMethod.SVD;
        isObservable := StateSpace.Analysis.isObservable(ss, method);
        if isObservable then
          Modelica.Utilities.Streams.print("pair (A, B) is observable checked by SVD");
        else
          Modelica.Utilities.Streams.print("pair (A, B) is not observable checked by SVD");
        end if;
      end if;

      ok := true;
    end analysisObservability;

## Modelica_LinearSystems2.Examples.StateSpace.analysisControllablePoles

Example to check controllability of a state space system and print the controllable poles

### Information

This example shows the usage of function Modelica_LinearSystems2.StateSpace.Analysis.isControllable which is to check whether a system is controllable or not.

### Inputs

Type | Name | Default | Description
--- | --- | --- | ---
StateSpace | ssi | Modelica_LinearSystems2.Stat... |
system data definition
Boolean | systemOnFile | false | true, if state space system is defined on file
String | fileName | "NoName" | file where matrix [A, B; C, D] is stored
String | matrixName | "ABCD" | Name of the state space system matrix

### Outputs

Type | Name | Description
--- | --- | ---
Boolean | ok |

### Modelica definition

    function analysisControllablePoles
      "Example to check controllability of a state space system and print the controllable poles"
      import Modelica_LinearSystems2;
      import Modelica_LinearSystems2.StateSpace;
      import Modelica_LinearSystems2.Math.Matrices;
      import Modelica.Utilities.Streams.print;

      input StateSpace ssi=Modelica_LinearSystems2.StateSpace(
          A=[1,0,0,0,0,0; 1,0,0,0,0,0; 0,2,3,0,78,6; 1,1,2,2,3,3; 10,13,34,0,0,1; 0,
            0,0,2,0,0],
          B=[0,0; 0,0; 0,0; 0,0; 1,0; 0,0],
          C=[1,0,1,0,1,0; 0,1,0,1,0,1; 0,1,0,1,0,1],
          D=[0,0; 0,0; 0,0]);

      input Boolean systemOnFile=false
        "true, if state space system is defined on file";
      input String fileName="NoName" "file where matrix [A, B; C, D] is stored";
      input String matrixName="ABCD" "Name of the state space system matrix";

      output Boolean ok;

    protected
      StateSpace ss=if systemOnFile then
          Modelica_LinearSystems2.StateSpace.Import.fromFile(fileName) else ssi;
      Real cPoles[:,2] "controllable poles";
      Real ncPoles[:,2] "uncontrollable poles";

    algorithm
      (cPoles,ncPoles) := StateSpace.Internal.controllablePoles(ss);
      if size(ncPoles, 1) == 0 then
        print("\nThe system is controllable\nThe poles are" +
          Modelica_LinearSystems2.Math.Matrices.printMatrix(cPoles,6,""));
      else
        print("\nThe system is not controllable\nThe uncontrollable poles are" +
          Modelica_LinearSystems2.Math.Matrices.printMatrix(ncPoles,6,""));
      end if;

    ok := true;
    end analysisControllablePoles;

## Modelica_LinearSystems2.Examples.StateSpace.analysisTimeResponse

Compute time response of a state space system

### Information

Computes the time response of the system StateSpace sc = StateSpace(A=[-1,1;0,-2],B=[1, 0;0, 1],C=[1,0; 0,1],D=[0, 0; 0, 0]), sampled at Ts=0.01 with inititial state x0=[0;0] subject to the system input u = ones(samples,2), (samples is set to 30).

### Inputs

Type | Name | Default | Description
--- | --- | --- | ---
Real | u[:, 2] | ones(300, 2) |

### Modelica definition

    function analysisTimeResponse
      "Compute time response of a state space system"
      import Modelica;
      import Modelica_LinearSystems2;

     input Real u[:,2]=ones(300, 2);
    protected
      Modelica_LinearSystems2.StateSpace sc=Modelica_LinearSystems2.StateSpace(
          A=[-1,1; 0,-2],
          B=[1,0; 0,1],
          C=[1,0; 0,1],
          D=[0,0; 0,0]);
      Real Ts=0.01;

      Modelica_LinearSystems2.DiscreteStateSpace sd=
          Modelica_LinearSystems2.DiscreteStateSpace(sc, Ts);
      Integer samples=size(u,1);
      Real x0[2]={0, 0};

      output Real y[samples,2]
        "System response (dimension: (input samples) x (number of outputs))";
      output Real t[samples] "Time vector used for simulation";
      output Real x[samples,2]
        "State trajectories (dimension: (input samples) x (number of states)";
    algorithm
       t := 0:sd.Ts:(samples*sd.Ts - sd.Ts);
      (y,x) := Modelica_LinearSystems2.DiscreteStateSpace.timeResponse(
        sd,
        u,
        x0);

     Modelica_LinearSystems2.Utilities.Plot.diagram(
           Modelica_LinearSystems2.Utilities.Plot.Records.Diagram(
                     curve={Modelica_LinearSystems2.Utilities.Plot.Records.Curve(
                              x=t,
                              y=y[:,1],
                              legend="y1"),
                              Modelica_LinearSystems2.Utilities.Plot.Records.Curve(
                              x=t,
                              y=y[:,2],
                              legend="y2")},
                     heading="Step response to synchronous step of u1 and u2",
                     xLabel="time [s]",
                     yLabel="y1, y2"));
    end analysisTimeResponse;

## Modelica_LinearSystems2.Examples.StateSpace.analysisInitialResponse

Initial response example

### Information

Computes and plots the step response

### Outputs

Type | Name | Description
--- | --- | ---
Real | y[:, size(sc.C, 1), 1] | Output response: (number of samples) x (number of outputs) x 1

### Modelica definition

    function analysisInitialResponse "Initial response example"

      import Modelica_LinearSystems2.StateSpace;

    protected
      Modelica_LinearSystems2.StateSpace sc=Modelica_LinearSystems2.StateSpace(
          A=[-1,1; 0,-2],
          B=[1,0; 0,1],
          C=[1,0; 0,1],
          D=[0,0; 0,0]);

      Real t[:] "Time vector: (number of samples)";
     Real x_continuous[:,2,1]
        "State trajectories: (number of samples) x (number of states) x 1";
     Real x0[2]=ones(2) "Initial state vector";

    public
    output Real y[:,size(sc.C, 1),1]
        "Output response: (number of samples) x (number of outputs) x 1";
    // output Real y[:,size(sc.C, 1),size(sc.B, 2)]
    //     "Output response: (number of samples) x (number of outputs) x (number of inuputs)";

    algorithm
     (y,t,x_continuous) := Modelica_LinearSystems2.StateSpace.Analysis.initialResponse(x0=x0,sc=sc,dt=0.1,tSpan=5);

    //  Modelica_LinearSystems2.Utilities.Plot.diagramVector({
    //        Modelica_LinearSystems2.Utilities.Plot.Records.Diagram(
    //                  curve={Modelica_LinearSystems2.Utilities.Plot.Records.Curve(
    //                           x=t,
    //                           y=y[:,1,1],
    //                           legend="y1"),
    //                           Modelica_LinearSystems2.Utilities.Plot.Records.Curve(
    //                           x=t,
    //                           y=y[:,1,2],
    //                           legend="y2")},
    //                  heading="Initial response to u1",
    //                  xLabel="time [s]",
    //                  yLabel="y1, y2"),
    //        Modelica_LinearSystems2.Utilities.Plot.Records.Diagram(
    //                  curve={Modelica_LinearSystems2.Utilities.Plot.Records.Curve(
    //                           x=t,
    //                           y=y[:,2,1],
    //                           legend="y1"),
    //                           Modelica_LinearSystems2.Utilities.Plot.Records.Curve(
    //                           x=t,
    //                           y=y[:,2,2],
    //                           legend="y2")},
    //                  heading="Initial response to u2",
    //                  xLabel="time [s]",
    //                  yLabel="y1, y2")});

      Modelica_LinearSystems2.Utilities.Plot.diagramVector({
            Modelica_LinearSystems2.Utilities.Plot.Records.Diagram(
                      curve={Modelica_LinearSystems2.Utilities.Plot.Records.Curve(
                               x=t,
                               y=y[:,1,1],
                               legend="y1")},
                      heading="Initial response y1",
                      xLabel="time [s]"),
            Modelica_LinearSystems2.Utilities.Plot.Records.Diagram(
                      curve={Modelica_LinearSystems2.Utilities.Plot.Records.Curve(
                               x=t,
                               y=y[:,2,1],
                               legend="y2")},
                      heading="Initial response y2",
                      xLabel="time [s]")});

    end analysisInitialResponse;

## Modelica_LinearSystems2.Examples.StateSpace.analysisImpulseResponse

Impulse response example

### Information

Computes and plots the step response

### Outputs

Type | Name | Description
--- | --- | ---
Real | y[:, size(sc.C, 1), size(sc.B, 2)] | Output response: (number of samples) x (number of outputs) x (number of inuputs)

### Modelica definition

    function analysisImpulseResponse "Impulse response example"

      import Modelica_LinearSystems2.StateSpace;

    protected
      Modelica_LinearSystems2.StateSpace sc=Modelica_LinearSystems2.StateSpace(
          A=[-1,1; 0,-2],
          B=[4,0; 0,2],
          C=[2,0; 0,3],
          D=[0,0; 0,0]);

      Real t[:] "Time vector: (number of samples)";
     Real x_continuous[:,size(sc.A, 1),size(sc.B, 2)]
        "State trajectories: (number of samples) x (number of states) x (number of inputs)";

    public
    output Real y[:,size(sc.C, 1),size(sc.B, 2)]
        "Output response: (number of samples) x (number of outputs) x (number of inuputs)";

    algorithm
     (y,t,x_continuous) := Modelica_LinearSystems2.StateSpace.Analysis.impulseResponse(sc=sc,dt=0.1,tSpan=5);

     Modelica_LinearSystems2.Utilities.Plot.diagramVector({
           Modelica_LinearSystems2.Utilities.Plot.Records.Diagram(
                     curve={Modelica_LinearSystems2.Utilities.Plot.Records.Curve(
                              x=t,
                              y=y[:,1,1],
                              legend="y1"),
                              Modelica_LinearSystems2.Utilities.Plot.Records.Curve(
                              x=t,
                              y=y[:,2,1],
                              legend="y2")},
                     heading="Impulse response to u1",
                     xLabel="time [s]",
                     yLabel="y1, y2"),
           Modelica_LinearSystems2.Utilities.Plot.Records.Diagram(
                     curve={Modelica_LinearSystems2.Utilities.Plot.Records.Curve(
                              x=t,
                              y=y[:,1,2],
                              legend="y1"),
                              Modelica_LinearSystems2.Utilities.Plot.Records.Curve(
                              x=t,
                              y=y[:,2,2],
                              legend="y2")},
                     heading="Impulse response to u2",
                     xLabel="time [s]",
                     yLabel="y1, y2")});
    end analysisImpulseResponse;

## Modelica_LinearSystems2.Examples.StateSpace.analysisStepResponse

Step response example

### Information

Computes and plots the step response

### Outputs

Type | Name | Description
--- | --- | ---
Real | y[:, size(sc.C, 1), size(sc.B, 2)] | Output response: (number of samples) x (number of outputs) x (number of inuputs)

### Modelica definition

    function analysisStepResponse "Step response example"

      import Modelica_LinearSystems2.StateSpace;

    protected
      Modelica_LinearSystems2.StateSpace sc=Modelica_LinearSystems2.StateSpace(
          A=[-1,1; 0,-2],
          B=[1,0; 0,1],
          C=[1,0; 0,1],
          D=[0,0; 0,0]);

      Real t[:] "Time vector: (number of samples)";
     Real x_continuous[:,size(sc.A, 1),size(sc.B, 2)]
        "State trajectories: (number of samples) x (number of states) x (number of inputs)";

    public
    output Real y[:,size(sc.C, 1),size(sc.B, 2)]
        "Output response: (number of samples) x (number of outputs) x (number of inuputs)";

    algorithm
      (y,t,x_continuous) :=
        Modelica_LinearSystems2.StateSpace.Analysis.stepResponse(
        sc=sc,
        dt=0.1,
        tSpan=5);

     Modelica_LinearSystems2.Utilities.Plot.diagramVector({
           Modelica_LinearSystems2.Utilities.Plot.Records.Diagram(
                     curve={Modelica_LinearSystems2.Utilities.Plot.Records.Curve(
                              x=t,
                              y=y[:,1,1],
                              legend="y1"),
                              Modelica_LinearSystems2.Utilities.Plot.Records.Curve(
                              x=t,
                              y=y[:,2,1],
                              legend="y2")},
                     heading="Step response to u1",
                     xLabel="time [s]",
                     yLabel="y1, y2"),
           Modelica_LinearSystems2.Utilities.Plot.Records.Diagram(
                     curve={Modelica_LinearSystems2.Utilities.Plot.Records.Curve(
                              x=t,
                              y=y[:,1,2],
                              legend="y1"),
                              Modelica_LinearSystems2.Utilities.Plot.Records.Curve(
                              x=t,
                              y=y[:,2,2],
                              legend="y2")},
                     heading="Step response to u2",
                     xLabel="time [s]",
                     yLabel="y1, y2")});
    end analysisStepResponse;

## Modelica_LinearSystems2.Examples.StateSpace.analysisPolesAndZeros_SISO

Compute poles and invariant zeros of a SISO state space system by transformation to a minmal system

### Inputs

Type | Name | Default | Description
--- | --- | --- | ---
system data definition
String | fileName | "NoName" | file where matrix [A, B; C, D] is stored
String | matrixName | "ABCD" | Name of the state space system matrix
system matrices
Real | A[:, :] | [-1.0, 0.0, 0.0; 0.0, -2.0, ... |
Real | B[:, :] | [1.0; 1.0; 0.0] |
Real | C[:, :] | [1.0, 1.0, 1.0] |
Real | D[:, :] | [0.0] |

### Outputs

Type | Name | Description
--- | --- | ---
Boolean | ok |

### Modelica definition

    function analysisPolesAndZeros_SISO
      "Compute poles and invariant zeros of a SISO state space system by transformation to a minmal system"
      import Modelica_LinearSystems2.StateSpace;
      import Modelica_LinearSystems2.Math.Complex;

      input String fileName="NoName" "file where matrix [A, B; C, D] is stored";
      input String matrixName="ABCD" "Name of the state space system matrix";

      input Real A[:,:]=[-1.0,0.0,0.0; 0.0,-2.0,0.0; 0.0,0.0,-3.0];
      input Real B[:,:]=[1.0; 1.0; 0.0];
      input Real C[:,:]=[1.0,1.0,1.0];
      input Real D[:,:]=[0.0];

      output Boolean ok;

    protected
      Boolean systemOnFile=fileName <> "NoName";

      Modelica_LinearSystems2.StateSpace ss=if systemOnFile then
          Modelica_LinearSystems2.StateSpace.Import.fromFile(  fileName, matrixName) else
          Modelica_LinearSystems2.StateSpace(
          A=A,
          B=B,
          C=C,
          D=D);
      StateSpace ssm=Modelica_LinearSystems2.StateSpace.Transformation.toIrreducibleForm(
                                                                     ss);
      Complex poles[:];
      Complex zeros[:];

    algorithm
      poles := Complex.eigenValues(ssm.A);

      for i in 1:size(poles, 1) loop

        Modelica.Utilities.Streams.print("pole_" + String(i) + " = " + String(poles[i]));
      end for;

      zeros := Modelica_LinearSystems2.StateSpace.Analysis.invariantZeros(
                                          ssm);

      for i in 1:size(zeros, 1) loop
         Modelica.Utilities.Streams.print("zero_" + String(i) + " = " + String(zeros[i]));
      end for;

      ok := true;

    end analysisPolesAndZeros_SISO;

## Modelica_LinearSystems2.Examples.StateSpace.analysisStairCase

Example to check controllability of a state space system

### Information

This example shows the usage of function Modelica_LinearSystems2.StateSpace.Design.assignPolesMI which is to design pole assigment controllers for state space systems with multiple input.

### Inputs

Type | Name | Default | Description
--- | --- | --- | ---
StateSpace | ssi | Modelica_LinearSystems2.Stat... |
system data definition
Boolean | systemOnFile | false | true, if state space system is defined on file
String | fileName | "NoName" | file where matrix [A, B; C, D] is stored
String | matrixName | "ABCD" | Name of the state space system matrix

### Outputs

Type | Name | Description
--- | --- | ---
Boolean | ok |

### Modelica definition

    function analysisStairCase
      "Example to check controllability of a state space system"
      import Modelica_LinearSystems2;
      import Modelica_LinearSystems2.StateSpace;
      import Modelica_LinearSystems2.Math.Matrices;
      import Modelica.Utilities.Streams.print;

      input Boolean systemOnFile=false
        "true, if state space system is defined on file";
      input String fileName="NoName" "file where matrix [A, B; C, D] is stored";
      input String matrixName="ABCD" "Name of the state space system matrix";

      input StateSpace ssi=Modelica_LinearSystems2.StateSpace(
          A=[1,2,3,4,5,6; 5,6,7,8,9,4; 0,2,3,0,78,6; 1,1,2,2,3,3; 10,13,34,0,0,1; 0,
            0,0,2,0,1],
          B=[1; 2; 0; 0; 0; 0],
          C=[1,0,1,0,1,0],
          D=[0]);

      output Boolean ok;

    protected
      StateSpace ss=if systemOnFile then
      Modelica_LinearSystems2.StateSpace.Import.fromFile(fileName) else ssi;
      StateSpace ss2=StateSpace.Internal.transposeStateSpace(ss);
      StateSpace ss3=ss;
      Boolean isControllable;
      Boolean isObservable;
      Modelica_LinearSystems2.Internal.StateSpaceR ssR;

    algorithm
      ok := false;

      (isControllable,ssR) := StateSpace.Internal.staircaseSVD(ss);
      ss3.A:=ssR.A;
      ss3.B:=ssR.B;
      ss3.C:=ssR.C;
      ss3.D:=ssR.D;
      if isControllable then
        Modelica.Utilities.Streams.print("pair (A, B) is controllable");
      else
        Modelica.Utilities.Streams.print("pair (A, B) is not controllable");
      end if;

     Modelica.Utilities.Streams.print("Transformed system is"+String(ss3));

     (isObservable,ssR) := StateSpace.Internal.staircaseSVD(ss2);
      ss3.A:=ssR.A;
      ss3.B:=ssR.B;
      ss3.C:=ssR.C;
      ss3.D:=ssR.D;
      if isObservable then
        Modelica.Utilities.Streams.print("pair (A, C) is observable");
      else
        Modelica.Utilities.Streams.print("pair (A, C) is not observable");
      end if;
      Modelica.Utilities.Streams.print("\nTransformed dual system is:\n"+String(ss3));

      ok := true;
    end analysisStairCase;

## Modelica_LinearSystems2.Examples.StateSpace.analysisInvariantZeros

Compute invariant zeros of transfer function

### Inputs

Type | Name | Default | Description
--- | --- | --- | ---
Complex | z[:] | {-2 + 0*j,-3 + 4*j,-3 - 4*j} | Zeros (Complex vector of numerator zeros)
Complex | p[:] | {-0.5 + 0*j,-5 + 2*j,-5 - 2*j} | Poles (Complex vector of denominator zeros)
Real | k | 1.0 | Constant multiplied with transfer function

### Modelica definition

    function analysisInvariantZeros
      "Compute invariant zeros of transfer function"
      import Modelica;
      import Modelica.Utilities.Streams.print;
      import Modelica_LinearSystems2.Math.Complex;
      import Modelica_LinearSystems2.TransferFunction;
      import Modelica_LinearSystems2.StateSpace;
      import Modelica_LinearSystems2.ZerosAndPoles;

      input Complex z[:]={-2 + 0*j,-3 + 4*j,-3 - 4*j}
        "Zeros (Complex vector of numerator zeros)";
      input Complex p[:]={-0.5 + 0*j,-5 + 2*j,-5 - 2*j}
        "Poles (Complex vector of denominator zeros)";
      input Real k=1.0 "Constant multiplied with transfer function";

    protected
      input Complex j=Modelica_LinearSystems2.Math.Complex.j();

      ZerosAndPoles zp=ZerosAndPoles(
          z=z,
          p=p,
          k=k);

      StateSpace ss=StateSpace(zp);
      Complex Zeros[:];
      Boolean ok;
    algorithm
      Zeros := Modelica_LinearSystems2.StateSpace.Analysis.invariantZeros(ss);

      if size(Zeros, 1) == 0 then
        print("\nSystem\n  "+String(zp)+"\nhas no invariant zeros\n");
      else
        print("\nSystem\n  zp = "+String(zp)+"\n has " + String(size(Zeros, 1)) + " invariant zeros:");
        for i in 1:size(Zeros, 1) loop
          print("   " + String(Zeros[i]));
        end for;
      end if;
      ok := true;
    end analysisInvariantZeros;

## Modelica_LinearSystems2.Examples.StateSpace.conversionFromZerosAndPoles

Transform a TransferFunction into a StateSpace description

### Inputs

Type | Name | Default | Description
--- | --- | --- | ---
ZerosAndPoles | zp | Modelica_LinearSystems2.Zero... |

### Outputs

Type | Name | Description
--- | --- | ---
Boolean | ok |

### Modelica definition

    function conversionFromZerosAndPoles
      "  Transform a TransferFunction into a StateSpace description"
      import Modelica_LinearSystems2.StateSpace;
      import Modelica_LinearSystems2.ZerosAndPoles;
      import Modelica_LinearSystems2.Math.Complex;

      input ZerosAndPoles zp= Modelica_LinearSystems2.ZerosAndPoles({2+0*j}, {1+0*j,2+3*j,2-3*j}, 4);
    protected
     input Complex j = Modelica_LinearSystems2.Math.Complex.j();
    public
      output Boolean ok;

    protected
      StateSpace ss=StateSpace(zp);

      ZerosAndPoles zp2;

    algorithm
       Modelica.Utilities.Streams.print("zp = " + String(zp));
       Modelica.Utilities.Streams.print("ss = " + String(ss));
       ok := true;

    end conversionFromZerosAndPoles;

## Modelica_LinearSystems2.Examples.StateSpace.conversionToTransferFunctionMIMO

Example to compute a transfer function matrix of a MIMO system from state space representation

### Inputs

Type | Name | Default | Description
--- | --- | --- | ---
system data definition
Boolean | systemOnFile | false | true, if state space system is defined on file
String | fileName | "NoName" | file where matrix [A, B; C, D] is stored
String | matrixName | "ABCD" | Name of the state space system matrix
system matrices
Real | A[:, size(A, 1)] | [-1.0, 0.0, 0.0; 0.0, -2.0, ... |
Real | B[size(A, 2), :] | [0.0, 1.0; 1.0, 1.0; -1.0, 0... |
Real | C[:, size(A, 1)] | [0.0, 1.0, 1.0; 1.0, 1.0, 1.0] |
Real | D[size(C, 1), size(B, 2)] | [1.0, 0.0; 0.0, 1.0] |

### Outputs

Type | Name | Description
--- | --- | ---
Boolean | ok |

### Modelica definition

    function conversionToTransferFunctionMIMO
      "Example to compute a transfer function matrix of a MIMO system from state space representation"
      import Modelica;
      import Modelica_LinearSystems2;
      import Modelica_LinearSystems2.TransferFunction;
      import Modelica_LinearSystems2.StateSpace;

      input Boolean systemOnFile=false
        "true, if state space system is defined on file";

      input String fileName="NoName" "file where matrix [A, B; C, D] is stored";
      input String matrixName="ABCD" "Name of the state space system matrix";

      input Real A[:,size(A, 1)]=[-1.0,0.0,0.0; 0.0,-2.0,0.0; 0.0,0.0,-3.0];
      input Real B[size(A, 2),:]=[0.0,1.0; 1.0,1.0; -1.0,0.0];
      input Real C[:,size(A, 1)]=[0.0,1.0,1.0; 1.0,1.0,1.0];
      input Real D[size(C, 1),size(B, 2)]=[1.0,0.0; 0.0,1.0];

      output Boolean ok;

    protected
      Modelica_LinearSystems2.StateSpace ss=if systemOnFile then
          Modelica_LinearSystems2.StateSpace.Import.fromFile(  fileName, matrixName) else
          Modelica_LinearSystems2.StateSpace(
          A=A,
          B=B,
          C=C,
          D=D);

      Modelica_LinearSystems2.TransferFunction tf[:,:];

    algorithm
      tf := StateSpace.Conversion.toTransferFunctionMIMO(ss);
      for i1 in 1:size(ss.C, 1) loop
        for i2 in 1:size(ss.B, 2) loop

          Modelica.Utilities.Streams.print("TransferFunction[" + String(i1) + "," +
            String(i2) + "] = " + String(tf[i1, i2]));
        end for;
      end for;
      ok := true;

    end conversionToTransferFunctionMIMO;

## Modelica_LinearSystems2.Examples.StateSpace.conversionToTransferFunctionSISO

Example to compute a transfer function from SISO state space representation

### Inputs

Type | Name | Default | Description
--- | --- | --- | ---
system data definition
Boolean | systemOnFile | false | true, if state space system is defined on file
String | fileName | "NoName" | file where matrix [A, B; C, D] is stored
String | matrixName | "ABCD" | Name of the state space system matrix
system matrices
Real | A[:, :] | [-1.0, 0.0, 0.0; 0.0, -2.0, ... |
Real | B[:, :] | [1.0; 1.0; 0.0] |
Real | C[:, :] | [1.0, 1.0, 1.0] |
Real | D[:, :] | [0.0] |

### Outputs

Type | Name | Description
--- | --- | ---
Boolean | ok |

### Modelica definition

    function conversionToTransferFunctionSISO
      "Example to compute a transfer function from SISO state space representation"
      import Modelica_LinearSystems2.TransferFunction;
      import Modelica_LinearSystems2.StateSpace;

      input Boolean systemOnFile=false
        "true, if state space system is defined on file";

      input String fileName="NoName" "file where matrix [A, B; C, D] is stored";
      input String matrixName="ABCD" "Name of the state space system matrix";

      input Real A[:,:]=[-1.0,0.0,0.0; 0.0,-2.0,0.0; 0.0,0.0,-3.0];
      input Real B[:,:]=[1.0; 1.0; 0.0];
      input Real C[:,:]=[1.0,1.0,1.0];
      input Real D[:,:]=[0.0];
      output Boolean ok;

    protected
      StateSpace ss=if systemOnFile then
          Modelica_LinearSystems2.StateSpace.Import.fromFile( fileName) else
          Modelica_LinearSystems2.StateSpace(
          A=A,
          B=B,
          C=C,
          D=D);
      TransferFunction tf;

    algorithm
      tf := StateSpace.Conversion.toTransferFunction(ss);
      Modelica.Utilities.Streams.print("StateSpace = " + String(ss)+"\n");
      Modelica.Utilities.Streams.print("TransferFunction = " + String(tf));
      ok := true;

    end conversionToTransferFunctionSISO;

## Modelica_LinearSystems2.Examples.StateSpace.conversionToZerosAndPolesMIMO

Example to compute a zeros-and-poles representation of a MIMO system from state space representation

### Inputs

Type | Name | Default | Description
--- | --- | --- | ---
system data definition
Boolean | systemOnFile | false | true, if state space system is defined on file
String | fileName | "NoName" | file where matrix [A, B; C, D] is stored
String | matrixName | "ABCD" | Name of the state space system matrix
system matrices
Real | A[:, size(A, 1)] | [-1.0, 0.0, 0.0; 0.0, -2.0, ... |
Real | B[size(A, 2), :] | [0.0, 1.0; 1.0, 1.0; -1.0, 0... |
Real | C[:, size(A, 1)] | [0.0, 1.0, 1.0; 1.0, 1.0, 1.0] |
Real | D[size(C, 1), size(B, 2)] | [1.0, 0.0; 0.0, 1.0] |

### Outputs

Type | Name | Description
--- | --- | ---
Boolean | ok |

### Modelica definition

    function conversionToZerosAndPolesMIMO
      "Example to compute a zeros-and-poles representation of a MIMO system from state space representation"
      import Modelica_LinearSystems2.ZerosAndPoles;
      import Modelica_LinearSystems2.StateSpace.*;

      input Boolean systemOnFile=false
        "true, if state space system is defined on file";

      input String fileName="NoName" "file where matrix [A, B; C, D] is stored";
      input String matrixName="ABCD" "Name of the state space system matrix";

      input Real A[:,size(A, 1)]=[-1.0,0.0,0.0; 0.0,-2.0,0.0; 0.0,0.0,-3.0];
      input Real B[size(A, 2),:]=[0.0,1.0; 1.0,1.0; -1.0,0.0];
      input Real C[:,size(A, 1)]=[0.0,1.0,1.0; 1.0,1.0,1.0];
      input Real D[size(C, 1),size(B, 2)]=[1.0,0.0; 0.0,1.0];

      output Boolean ok;

    protected
      Modelica_LinearSystems2.StateSpace ss=if systemOnFile then Modelica_LinearSystems2.StateSpace.Import.fromFile( fileName, matrixName) else
          Modelica_LinearSystems2.StateSpace(
          A=A,
          B=B,
          C=C,
          D=D);

    protected
      Modelica_LinearSystems2.ZerosAndPoles zp[:,:];

    algorithm
      zp := Modelica_LinearSystems2.StateSpace.Conversion.toZerosAndPolesMIMO(ss);
      for i1 in 1:size(ss.C, 1) loop
        for i2 in 1:size(ss.B, 2) loop
           Modelica.Utilities.Streams.print("ZerosAndPoles-TransferFunction[" + String(i1) + "," +
            String(i2) + "] = " + String(zp[i1, i2]));

        end for;
      end for;
      ok := true;

    end conversionToZerosAndPolesMIMO;

## Modelica_LinearSystems2.Examples.StateSpace.conversionToZerosAndPolesSISO

Example to compute a zeros and poles representation from SISO state space representation

### Inputs

Type | Name | Default | Description
--- | --- | --- | ---
system data definition
Boolean | systemOnFile | false | true, if state space system is defined on file
String | fileName | "NoName" | file where matrix [A, B; C, D] is stored
String | matrixName | "ABCD" | Name of the state space system matrix
system matrices
Real | A[:, :] | [-1.0, 0.0, 0.0; 0.0, -2.0, ... |
Real | B[:, :] | [1.0; 1.0; 0.0] |
Real | C[:, :] | [1.0, 1.0, 1.0] |
Real | D[:, :] | [0.0] |

### Outputs

Type | Name | Description
--- | --- | ---
Boolean | ok |

### Modelica definition

    function conversionToZerosAndPolesSISO
      "Example to compute a zeros and poles representation from SISO state space representation"
      import Modelica_LinearSystems2.StateSpace;
      import Modelica_LinearSystems2.ZerosAndPoles;

      input Boolean systemOnFile=false
        "true, if state space system is defined on file";

      input String fileName="NoName" "file where matrix [A, B; C, D] is stored";
      input String matrixName="ABCD" "Name of the state space system matrix";

      input Real A[:,:]=[-1.0,0.0,0.0; 0.0,-2.0,0.0; 0.0,0.0,-3.0];
      input Real B[:,:]=[1.0; 1.0; 0.0];
      input Real C[:,:]=[1.0,1.0,1.0];
      input Real D[:,:]=[0.0];
      output Boolean ok;

    protected
      StateSpace ss=if systemOnFile then Modelica_LinearSystems2.StateSpace.Import.fromFile(
                                                              fileName, matrixName) else
          Modelica_LinearSystems2.StateSpace(
          A=A,
          B=B,
          C=C,
          D=D);
      Modelica_LinearSystems2.ZerosAndPoles zp;

    algorithm
      zp := StateSpace.Conversion.toZerosAndPoles(ss);
      Modelica.Utilities.Streams.print(String(zp));
      Modelica.Utilities.Streams.print("ZerosAndPoles-TransferFunction = " + String(
        zp));
      ok := true;

    end conversionToZerosAndPolesSISO;

## Modelica_LinearSystems2.Examples.StateSpace.designAssignPolesSISO

Example for pole placing using Ackermann's method

### Information

Computes the gain vector k for the state space system

    sc = StateSpace(A=[-1,1;0,-2],B=[0, 1],C=[1,0; 0, 1],D=[0; 0])

such that for the state feedback

    u = -k*y = -k*x

the closed-loop poles are placed at

    p = {-3,-4}.

Extends from Modelica.Icons.Function (Icon for a function).

### Inputs

Type | Name | Default | Description
--- | --- | --- | ---
StateSpace | sc | Modelica_LinearSystems2.Stat... |

### Outputs

Type | Name | Description
--- | --- | ---
Real | k[2] | Gain vector

### Modelica definition

    function designAssignPolesSISO
      "Example for pole placing using Ackermann's method"
      extends Modelica.Icons.Function;
      output Real k[2] "Gain vector";

      input Modelica_LinearSystems2.StateSpace sc=Modelica_LinearSystems2.StateSpace(
          A=[-1,1; 0,-2],
          B=[0; 1],
          C=[1,0; 0,1],
          D=[0; 0]);
    protected
          Modelica_LinearSystems2.Math.Complex p[2]={
          Modelica_LinearSystems2.Math.Complex(-3,0),
          Modelica_LinearSystems2.Math.Complex(-4,0)};
    algorithm
      k := Modelica_LinearSystems2.StateSpace.Design.assignPolesSI(sc, p);
    end designAssignPolesSISO;

## Modelica_LinearSystems2.Examples.StateSpace.designAssignPolesMIMO

Example for pole placing

### Information

Computes the gain vector k for the state space system

    ss = StateSpace(A=[-1,1;0,-2],B=[0, 1],C=[1,0; 0, 1],D=[0; 0])

such that for the state feedback

    u = -k*y = -k*x

the closed-loop poles are placed at

    p = {-3,-4}.

Extends from Modelica.Icons.Function (Icon for a function).

### Inputs

Type | Name | Default | Description
--- | --- | --- | ---
StateSpace | ss | Modelica_LinearSystems2.Stat... |
Complex | p[:] | {Modelica_LinearSystems2.Mat... |

### Outputs

Type | Name | Description
--- | --- | ---
Real | K[:, :] | Gain vector

### Modelica definition

    function designAssignPolesMIMO "Example for pole placing"
      extends Modelica.Icons.Function;
      output Real K[:,:] "Gain vector";

      input Modelica_LinearSystems2.StateSpace ss=Modelica_LinearSystems2.StateSpace(
          A=[-1,1; 0,-2],
          B=[0; 1],
          C=[1,0; 0,1],
          D=[0; 0]);
          input Modelica_LinearSystems2.Math.Complex p[:]={
          Modelica_LinearSystems2.Math.Complex(-3,0),
          Modelica_LinearSystems2.Math.Complex(-4,0)};

    protected
          Modelica_LinearSystems2.Math.Complex newPoles[:];
          Real S[:,:];

    algorithm
      (K, S, newPoles) := Modelica_LinearSystems2.StateSpace.Design.assignPolesMI(ss, p);
      Modelica_LinearSystems2.Math.Complex.Vectors.print("newPoles", newPoles);
      newPoles := Modelica_LinearSystems2.Math.Complex.eigenValues(ss.A-ss.B*K);
      Modelica_LinearSystems2.Math.Complex.Vectors.print("newPoles", newPoles);

    end designAssignPolesMIMO;

## Modelica_LinearSystems2.Examples.StateSpace.designKalmanFilter

Description

### Information

Extends from Modelica.Icons.Function (Icon for a function).

### Inputs

Type | Name | Default | Description
--- | --- | --- | ---
StateSpace | ss | Modelica_LinearSystems2.Stat... |

### Outputs

Type | Name | Description
--- | --- | ---
Real | L[:, :] |
StateSpace | kss |

### Modelica definition

    function designKalmanFilter "Description"
      import Modelica_LinearSystems2.Math.Complex;
      import Modelica_LinearSystems2.StateSpace;
      import Modelica_LinearSystems2.TransferFunction;
     extends Modelica.Icons.Function;

     input StateSpace ss=Modelica_LinearSystems2.StateSpace(TransferFunction(n={1}, d={1,2,3,4}));
    protected
     Real Q[:,:]=identity(3);
     Real R[:,:]=[1];

    public
     output Real L[:,:];
     output StateSpace kss(
       redeclare Real A[size(ss.A, 1),size(ss.A, 1)],
       redeclare Real B[size(ss.B, 1),size(ss.B, 2) + size(ss.C, 1)],
       redeclare Real C[size(ss.A, 1),size(ss.A, 2)],
       redeclare Real D[size(ss.A, 1),size(ss.B, 2) + size(ss.C,1)]);

    algorithm
     (L,kss) := StateSpace.Design.kalmanFilter(
       ss,
       Q,
       R);

    end designKalmanFilter;

## Modelica_LinearSystems2.Examples.StateSpace.designLQG

Description

### Information

Extends from Modelica.Icons.Function (Icon for a function).

### Inputs

Type | Name | Default | Description
--- | --- | --- | ---
StateSpace | ssi | Modelica_LinearSystems2.Stat... |
system data definition
Boolean | systemOnFile | false | true, if state space system is defined on file
String | fileName | "NoName" | file where matrix [A, B; C, D] is stored
String | matrixName | "ABCD" | Name of the state space system matrix

### Outputs

Type | Name | Description
--- | --- | ---
Boolean | ok |

### Modelica definition

    function designLQG "Description"

      import Modelica_LinearSystems2.StateSpace;

     extends Modelica.Icons.Function;

    input StateSpace ssi=Modelica_LinearSystems2.StateSpace(
          A=[-0.02, 0.005, 2.4,  -32; -0.14,  0.44,  -1.3,  -30; 0,  0.018,  -1.6,  1.2; 0, 0, 1, 0],
          B=[0.14,  -0.12; 0.36, -8.6; 0.35, 0.009; 0, 0],
          C=[0, 1, 0, 0; 0, 0, 0, 57.3],
          D=[0,0; 0,0]);

      input Boolean systemOnFile=false
        "true, if state space system is defined on file";
      input String fileName="NoName" "file where matrix [A, B; C, D] is stored";
      input String matrixName="ABCD" "Name of the state space system matrix";

      output Boolean ok;

    protected
     StateSpace ss=if systemOnFile then Modelica_LinearSystems2.StateSpace.Import.fromFile(
                                                             fileName) else ssi;
     Real Q[:,:] = transpose(ss.C)*ss.C " state weighting matrix";
     Real R[:,:] = identity(2) " input weighting matrix";
     Real V[:,:] = identity(2) " covariance output noise matrix";
     Real W[:,:] = ss.B*transpose(ss.B) " covariance input noise matrix";
     Real Kc[size(ss.B, 2),size(ss.A, 1)] "Controller feedback gain matrix";
     Real Kf[size(ss.A, 1),size(ss.C, 1)] "Kalman feedback gain matrix";
     StateSpace sslgq(
        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)]);

    algorithm
     (Kc, Kf, sslgq) := StateSpace.Design.lqg(ss, Q, R, V, W);

     Modelica_LinearSystems2.Math.Matrices.printMatrix(Kc,6,"Kc");
     Modelica_LinearSystems2.Math.Matrices.printMatrix(Kf,6,"Kf");
     Modelica.Utilities.Streams.print(String(sslgq));
     ok := true;

    end designLQG;

## Modelica_LinearSystems2.Examples.StateSpace.designLQR

Description

### Information

Extends from Modelica.Icons.Function (Icon for a function).

### Inputs

Type | Name | Default | Description
--- | --- | --- | ---
StateSpace | ssi | Modelica_LinearSystems2.Stat... |
system data definition
Boolean | systemOnFile | false | true, if state space system is defined on file
String | fileName | "NoName" | file where matrix [A, B; C, D] is stored
String | matrixName | "ABCD" | Name of the state space system matrix

### Outputs

Type | Name | Description
--- | --- | ---
Boolean | ok |

### Modelica definition

    function designLQR "Description"

      import Modelica_LinearSystems2.StateSpace;

     extends Modelica.Icons.Function;

    input StateSpace ssi=Modelica_LinearSystems2.StateSpace(
          A=[0, 1, 0, 0; 0, 0, 39.2, 0; 0, 0, 0, 1; 0, 0, 49, 0],
          B=[0; 1; 0; 1],
          C=[1, 0, 0, 0],
          D=[0]);

      input Boolean systemOnFile=false
        "true, if state space system is defined on file";
      input String fileName="NoName" "file where matrix [A, B; C, D] is stored";
      input String matrixName="ABCD" "Name of the state space system matrix";

      output Boolean ok;

    protected
      StateSpace ss=if systemOnFile then Modelica_LinearSystems2.StateSpace.Import.fromFile(
                                                              fileName) else ssi;
      Real Q[:,:]=identity(4) " state weighting matrix";
      Real R[:,:]=identity(1) " input weighting matrix";
      Real K[size(ss.B, 2),size(ss.A, 1)] "Controller feedback gain matrix";

    algorithm
      K := StateSpace.Design.lqr(ss, Q, R);

      Modelica_LinearSystems2.Math.Matrices.printMatrix(K, 6, "K");

      ok := true;

    end designLQR;

## Modelica_LinearSystems2.Examples.StateSpace.plotPolesAndZeros

Example for plotting eigenvalues and invariant zeros of a state space system

### Information

Extends from Modelica_LinearSystems2.StateSpace.Plot.polesAndZeros (Plot poles (i.e. eigenvalues) and/or invariant zeros of a state space system).

### Inputs

Type | Name | Default | Description
--- | --- | --- | ---
StateSpace | ss | Modelica_LinearSystems2.Stat... | Linear system in state space form
Boolean | poles | true | = true, to plot the poles (i.e. the eigenvalues) of ss
Boolean | zeros | true | = true, to plot the (invariant) zeros of ss
Diagram | defaultDiagram | Modelica_LinearSystems2.Inte... | Default diagram layout
Device | device | Modelica_LinearSystems2.Util... | Properties of device where figure is shown

### Modelica definition

    function plotPolesAndZeros
      "Example for plotting eigenvalues and invariant zeros of a state space system"

      import Modelica_LinearSystems2;
      extends Modelica_LinearSystems2.StateSpace.Plot.polesAndZeros(ss=
        Modelica_LinearSystems2.StateSpace(
          A=[-3,2,-3,4,5,6; 0,6,7,8,9,4; 0,2,3,0,78,6; 0,1,2,2,3,3; 0,13,34,0,0,1; 0,0,0,-17,0,0],
          B=[1,0; 0,1; 1,0; 0,1; 1,0; 0,1],
          C=[0,0,1,0,1,0; 0,1,0,0,1,1],
          D=[0,0; 0,0]));
    end plotPolesAndZeros;

## Modelica_LinearSystems2.Examples.StateSpace.plotBodeMIMO

Constructs zeros-and-poles-transfers from state space representation and plots the Bode diagrams with automatic determination of the frequency range to plot

### Inputs

Type | Name | Default | Description
--- | --- | --- | ---
system data definition
Boolean | systemOnFile | false | true, if state space system is defined on file
String | fileName | "NoName" | file where matrix [A, B; C, D] is stored
system matrices
Real | A[:, size(A, 1)] | [-1.0, 0.0, 0.0; 0.0, -2.0, ... |
Real | B[size(A, 2), :] | [0.0, 1.0; 1.0, 1.0; -1.0, 0... |
Real | C[:, size(A, 1)] | [0.0, 1.0, 1.0; 1.0, 1.0, 1.0] |
Real | D[size(C, 1), size(B, 2)] | [1.0, 0.0; 0.0, 1.0] |

### Outputs

Type | Name | Description
--- | --- | ---
Boolean | ok |

### Modelica definition

    function plotBodeMIMO
      "Constructs zeros-and-poles-transfers from state space representation and plots the Bode diagrams with automatic determination of the frequency range to plot "

      import Modelica;
      import Modelica_LinearSystems2.StateSpace;
      import Modelica_LinearSystems2.TransferFunction;

      input Boolean systemOnFile=false
        "true, if state space system is defined on file";

      input String fileName="NoName" "file where matrix [A, B; C, D] is stored";

      input Real A[:,size(A, 1)]=[-1.0,0.0,0.0; 0.0,-2.0,0.0; 0.0,0.0,-3.0];
      input Real B[size(A, 2),:]=[0.0,1.0; 1.0,1.0; -1.0,0.0];
      input Real C[:,size(A, 1)]=[0.0,1.0,1.0; 1.0,1.0,1.0];
      input Real D[size(C, 1),size(B, 2)]=[1.0,0.0; 0.0,1.0];

      output Boolean ok;

    protected
      StateSpace ss=if systemOnFile then
          Modelica_LinearSystems2.StateSpace.Import.fromFile( fileName) else StateSpace(
          A=A,
          B=B,
          C=C,
          D=D);

    algorithm
      Modelica_LinearSystems2.StateSpace.Plot.bodeMIMO(    ss);
      ok := true;

    end plotBodeMIMO;

## Modelica_LinearSystems2.Examples.StateSpace.plotBodeSISO

Constructs a zeros-and-poles-transfer function from state space representation and plots the Bode diagram with automatic determination of the frequency range to plot

### Inputs

Type | Name | Default | Description
--- | --- | --- | ---
Boolean | systemOnFile | false | true, if state space system is defined on file
String | fileName | "NoName" | file where matrix [A, B; C, D] is stored
Real | A[:, size(A, 1)] | [-1.0, 0.0, 0.0; 0.0, -2.0, ... |
Real | B[size(A, 2), :] | [0.0, 1.0; 1.0, 1.0; -1.0, 0... |
Real | C[:, size(A, 1)] | [0.0, 1.0, 1.0; 1.0, 1.0, 1.0] |
Real | D[size(C, 1), size(B, 2)] | [1.0, 0.0; 0.0, 1.0] |
Integer | iu | 1 | index of inout
Integer | iy | 1 | index of output

### Outputs

Type | Name | Description
--- | --- | ---
Boolean | ok |

### Modelica definition

    function plotBodeSISO
      "Constructs a zeros-and-poles-transfer function from state space representation and plots the Bode diagram with automatic determination of the frequency range to plot "

      import Modelica;
      import Modelica_LinearSystems2.StateSpace;
      import Modelica_LinearSystems2.TransferFunction;

      input Boolean systemOnFile=false
        "true, if state space system is defined on file";
      input String fileName="NoName" "file where matrix [A, B; C, D] is stored";

      input Real A[:,size(A, 1)]=[-1.0,0.0,0.0; 0.0,-2.0,0.0; 0.0,0.0,-3.0];
      input Real B[size(A, 2),:]=[0.0,1.0; 1.0,1.0; -1.0,0.0];
      input Real C[:,size(A, 1)]=[0.0,1.0,1.0; 1.0,1.0,1.0];
      input Real D[size(C, 1),size(B, 2)]=[1.0,0.0; 0.0,1.0];

      input Integer iu=1 "index of inout";
      input Integer iy=1 "index of output";
      output Boolean ok;

    protected
      StateSpace ss=if systemOnFile then Modelica_LinearSystems2.StateSpace.Import.fromFile(
                                                              fileName) else
          StateSpace(
          A=A,
          B=B,
          C=C,
          D=D);

    algorithm
      assert(iu <= size(ss.B, 2) and iu > 0, "index for input is " + String(iu) +
        " which is not in [1, " + String(size(ss.B, 2)) + "].");
      assert(iy <= size(ss.C, 1) and iy > 0, "index for output is " + String(iy) +
        " which is not in [1, " + String(size(ss.C, 1)) + "].");

      Modelica_LinearSystems2.StateSpace.Plot.bodeSISO(
        ss,
        iu,
        iy);
      ok := true;

    end plotBodeSISO;

## Modelica_LinearSystems2.Examples.StateSpace.plotTimeResponse

Time response plot example

### Information

Computes the impulse response of the system StateSpace sc = StateSpace(A=[-1,1;0,-2],B=[1, 0;0, 1],C=[1,0; 0,1],D=[0, 0; 0, 0]).

### Inputs

Type | Name | Default | Description
--- | --- | --- | ---
TimeResponse | response | Modelica_LinearSystems2.Type... | type of time response
StateSpace | ss | Modelica_LinearSystems2.Stat... |

### Modelica definition

    function plotTimeResponse "Time response plot example"

        input Modelica_LinearSystems2.Types.TimeResponse response=
            Modelica_LinearSystems2.Types.TimeResponse.Step "type of time response";

        input Modelica_LinearSystems2.StateSpace ss=Modelica_LinearSystems2.StateSpace(
          A=[-1,1; 0,-2],
          B=[4,0; 0,2],
          C=[2,0; 0,3; -1, 2],
          D=[0,0; 0,0; 1, 0],
          yNames={"phi1", "w", "a"},
          uNames={"tau1", "f1"});

    algorithm
      Modelica_LinearSystems2.StateSpace.Plot.timeResponse(    ss=ss);

    end plotTimeResponse;

## Modelica_LinearSystems2.Examples.StateSpace.plotImpulse

Impulse plot example

### Information

Computes the impulse response of the system StateSpace sc = StateSpace(A=[-1,1;0,-2],B=[1, 0;0, 1],C=[1,0; 0,1],D=[0, 0; 0, 0]).

### Inputs

Type | Name | Default | Description
--- | --- | --- | ---
StateSpace | ss | Modelica_LinearSystems2.Stat... |

### Modelica definition

    function plotImpulse "Impulse plot example"

      input Modelica_LinearSystems2.StateSpace ss=Modelica_LinearSystems2.StateSpace(
          A=[-1,1; 0,-2],
          B=[4,0; 0,2],
          C=[2,0; 0,3],
          D=[0,0; 0,0]);

    algorithm
      Modelica_LinearSystems2.StateSpace.Plot.impulse(    ss=ss);

    end plotImpulse;

## Modelica_LinearSystems2.Examples.StateSpace.plotInital

Initial condition plot example

### Information

Computes the initial condition response of the system StateSpace sc = StateSpace(A=[-1,1;0,-2],B=[1, 0;0, 1],C=[1,0; 0,1],D=[0, 0; 0, 0]) to the initial condition x0=[1;1].

### Inputs

Type | Name | Default | Description
--- | --- | --- | ---
StateSpace | ss | Modelica_LinearSystems2.Stat... |

### Modelica definition

    function plotInital "Initial condition plot example"

      input Modelica_LinearSystems2.StateSpace ss=Modelica_LinearSystems2.StateSpace(
          A=[-1,1; 0,-2],
          B=[1,0; 0,1],
          C=[1,0; 0,1],
          D=[0,0; 0,0]);
    protected
      Real x0[size(ss.A, 1)]=ones(size(ss.A, 1)) "Initial state vector";
    algorithm
      Modelica_LinearSystems2.StateSpace.Plot.initialResponse(
         ss=ss, x0=x0);

    end plotInital;

## Modelica_LinearSystems2.Examples.StateSpace.plotRamp

Plot ramp response

### Information

Computes the ramp response of the system StateSpace sc = StateSpace(A=[-1,1;0,-2],B=[1, 0;0, 1],C=[1,0; 0,1],D=[0, 0; 0, 0]).

### Inputs

Type | Name | Default | Description
--- | --- | --- | ---
StateSpace | ss | Modelica_LinearSystems2.Stat... |

### Modelica definition

    function plotRamp "Plot ramp response"

      input Modelica_LinearSystems2.StateSpace ss=
          Modelica_LinearSystems2.StateSpace(
          A=[-1,1; 0,-2],
          B=[1,0; 0,1],
          C=[1,0; 0,1],
          D=[0,0; 0,0]);

    algorithm
      Modelica_LinearSystems2.StateSpace.Plot.ramp(ss=ss);

    end plotRamp;

## Modelica_LinearSystems2.Examples.StateSpace.plotStep

Step plot example

### Information

Computes and plots the step response

### Inputs

Type | Name | Default | Description
--- | --- | --- | ---
StateSpace | ss | Modelica_LinearSystems2.Stat... |

### Modelica definition

    function plotStep "Step plot example"

      import Modelica_LinearSystems2.StateSpace;

      input Modelica_LinearSystems2.StateSpace ss=Modelica_LinearSystems2.StateSpace(
          A=[-1,1; 0,-2],
          B=[1,0; 0,1],
          C=[1,0; 0,1],
          D=[0,0; 0,0]);

    algorithm
     Modelica_LinearSystems2.StateSpace.Plot.step(    ss=ss);
    end plotStep;

## Modelica_LinearSystems2.Examples.StateSpace.plotZeros

case studies of systems with zeros

### Information

Computes the initial condition response of the system StateSpace sc = StateSpace(A=[-1,1;0,-2],B=[1, 0;0, 1],C=[1,0; 0,1],D=[0, 0; 0, 0]) to the initial condition x0=[1;1]. This example plots the output y and the states (x1, x2, x3) of a system with the input

>        u(t) = uk*exp(zk*t)

where zk is an invariant zero of the system. Assuming appropriate initial conditions, the output of the system is forced to zero. It is demonstrated that the output can also be forced to zero by applying a transient unstable input. Although the output is zero, the states show transient and unstable behavior. In comparison, the outputs as an reaction of inputs with half or double frequency are not equal to zero.

### Modelica definition

    function plotZeros "case studies of systems with zeros"

      import Modelica;
      import Modelica_LinearSystems2.TransferFunction;
      import Modelica_LinearSystems2;
      import Modelica_LinearSystems2.StateSpace;
      import Modelica_LinearSystems2.Math.Complex;

    protected
      parameter Real sampleT=0.001;
      TransferFunction tf=TransferFunction({1,-0.1,1.0025}, {1,3,3,1});
      StateSpace ss=StateSpace(tf);
      Complex invZeros[:]=Modelica_LinearSystems2.StateSpace.Analysis.invariantZeros( ss);
      Complex invZero1;
      Real invMat[2*size(ss.A, 1),2*size(ss.A, 1)];
      Real t[:]=0:sampleT:20 "Time vector: (number of samples)";
      Real u[size(t, 1),1];
      Real u2[size(t, 1),1];
      Real y[:,1];
      Real y2[:,1];
      Integer windowID[size(ss.C, 1)] "ID of the used plot window";
      Real x0[size(ss.A, 1)]=zeros(size(ss.A, 1)) "Initial state vector";
      Real x[size(t,1),size(ss.A,1)];
      Real Ts=sampleT;
      Modelica_LinearSystems2.DiscreteStateSpace sd=Modelica_LinearSystems2.DiscreteStateSpace(ss, Ts, method=Modelica_LinearSystems2.Types.Method.StepExact);
      Boolean ok;

    algorithm
      Modelica.Utilities.Streams.print(String(ss));
      Modelica.Utilities.Streams.print(String(tf));
      Modelica_LinearSystems2.Math.Complex.Vectors.print("invariant zeros", invZeros);

      invZero1 := invZeros[1];
      invMat := Modelica.Math.Matrices.inv([invZero1.re*identity(size(ss.A, 1)) -
        ss.A,invZero1.im*identity(size(ss.A, 1)); -invZero1.im*identity(size(ss.A,
        1)),invZero1.re*identity(size(ss.A, 1)) - ss.A]);
      x0 := vector(2*(invMat[1:size(ss.A, 1), 1:size(ss.A, 1)])*ss.B);
      Modelica_LinearSystems2.Math.Vectors.printVector(
        x0,
        6,
        "x0");

      u[:, 1] := 2*exp(invZero1.re*t).*vector(cos(t));
      (y,x) := Modelica_LinearSystems2.DiscreteStateSpace.timeResponse(
        sd,
        u,
        x0);

        Modelica_LinearSystems2.Utilities.Plot.diagram(
            Modelica_LinearSystems2.Utilities.Plot.Records.Diagram(
                      curve={Modelica_LinearSystems2.Utilities.Plot.Records.Curve(
                               x=t,
                               y=y[:,1],
                               legend="y"),
                             Modelica_LinearSystems2.Utilities.Plot.Records.Curve(
                               x=t,
                               y=u[:,1],
                               legend="u")},
                      heading="y and u",
                      xLabel="t [s]",
                      yLabel="y and u"));

     Modelica_LinearSystems2.Utilities.Plot.diagram(
           Modelica_LinearSystems2.Utilities.Plot.Records.Diagram(
                     curve={Modelica_LinearSystems2.Utilities.Plot.Records.Curve(
                              x=t,
                              y=x[:,1],
                              legend="x1"),
                            Modelica_LinearSystems2.Utilities.Plot.Records.Curve(
                              x=t,
                              y=x[:,2],
                              legend="x2"),
                            Modelica_LinearSystems2.Utilities.Plot.Records.Curve(
                              x=t,
                              y=x[:,3],
                              legend="x3")},
                     heading="x",
                     xLabel="t [s]",
                     yLabel="x"));

      u[:, 1] := 2*exp(invZero1.re*t).*vector(cos(2*t));

      y := Modelica_LinearSystems2.DiscreteStateSpace.timeResponse(
        sd,
        u,
        x0);

      Modelica_LinearSystems2.Utilities.Plot.diagram(
           Modelica_LinearSystems2.Utilities.Plot.Records.Diagram(
                     curve={Modelica_LinearSystems2.Utilities.Plot.Records.Curve(
                              x=t,
                              y=y[:,1],
                              legend="y"),
                            Modelica_LinearSystems2.Utilities.Plot.Records.Curve(
                              x=t,
                              y=u[:,1],
                              legend="u")},
                     heading="y and u",
                     xLabel="t [s]",
                     yLabel="y, u"));

     u[:, 1] := 2*exp(invZero1.re*t).*vector(cos(0.5*t));

      y := Modelica_LinearSystems2.DiscreteStateSpace.timeResponse(
        sd,
        u,
        x0);

       Modelica_LinearSystems2.Utilities.Plot.diagram(
           Modelica_LinearSystems2.Utilities.Plot.Records.Diagram(
                     curve={Modelica_LinearSystems2.Utilities.Plot.Records.Curve(
                              x=t,
                              y=y[:,1],
                              legend="y"),
                            Modelica_LinearSystems2.Utilities.Plot.Records.Curve(
                              x=t,
                              y=u[:,1],
                              legend="u")},
                     heading="y and u",
                     xLabel="t [s]",
                     yLabel="y, u"));

      ok := true;

    end plotZeros;

## Modelica_LinearSystems2.Examples.StateSpace.transformation

Example to demonstrate the transformation to Jordan- observabilitiy- and controllability canonical form

### Inputs

Type | Name | Default | Description
--- | --- | --- | ---
system data definition
String | fileName | "NoName" | file where matrix [A, B; C, D] is stored
String | matrixName | "ABCD" | Name of the state space system matrix
system matrices
Real | A[:, :] | [-1.0, 0.0, 0.0; 0.0, -2.0, ... |
Real | B[:, :] | [1.0; 1.0; 2.0] |
Real | C[:, :] | [1.0, 1.0, 1.0] |
Real | D[:, :] | [0.0] |

### Outputs

Type | Name | Description
--- | --- | ---
Boolean | ok |

### Modelica definition

    function transformation
      "Example to demonstrate the transformation to Jordan- observabilitiy- and controllability canonical form"
      import Modelica_LinearSystems2.StateSpace;

      input String fileName="NoName" "file where matrix [A, B; C, D] is stored";
      input String matrixName="ABCD" "Name of the state space system matrix";

      input Real A[:,:]=[-1.0,0.0,0.0; 0.0,-2.0,0.0; 0.0,0.0,-3.0];
      input Real B[:,:]=[1.0; 1.0; 2.0];
      input Real C[:,:]=[1.0,1.0,1.0];
      input Real D[:,:]=[0.0];

    protected
      Boolean systemOnFile=fileName <> "NoName";
      StateSpace ss=if systemOnFile then Modelica_LinearSystems2.StateSpace.Import.fromFile(
                                                              fileName, matrixName) else
                StateSpace(
          A=A,
          B=B,
          C=C,
          D=D);
      StateSpace ssj=StateSpace(
          A=A,
          B=B,
          C=C,
          D=D);

      StateSpace sso=StateSpace(
          A=A,
          B=B,
          C=C,
          D=D);

      StateSpace ssc=StateSpace(
          A=A,
          B=B,
          C=C,
          D=D);

      Real ctrb[:,:]=StateSpace.Analysis.controllabilityMatrix(ss);

    public
      output Boolean ok;

    algorithm
      ssj := Modelica_LinearSystems2.StateSpace.Transformation.toDiagonalForm(
                                                          ss);
      sso := Modelica_LinearSystems2.StateSpace.Transformation.toObservabilityForm(
                                                         ss);
      ssc := Modelica_LinearSystems2.StateSpace.Transformation.toControllabilityForm(
                                                        ss);
      Modelica.Utilities.Streams.print("\n\noriginal sate space system:\n");
      Modelica.Utilities.Streams.print(String(
        ss,
        6,
        "ss"));
      Modelica_LinearSystems2.Math.Matrices.printMatrix(
        ctrb,
        6,
        "controllability matrix");

      Modelica.Utilities.Streams.print("\n\nJordan form:\n");
      Modelica.Utilities.Streams.print(String(
        ssj,
        6,
        "ssy"));

      Modelica.Utilities.Streams.print("\n\nobservability canonical form:\n");
      Modelica.Utilities.Streams.print(String(
        sso,
        6,
        "sso"));

      Modelica.Utilities.Streams.print("\n\ncontrollability canonical form:\n");
      Modelica.Utilities.Streams.print(String(
        ssc,
        6,
        "ssc"));

      ok := true;

    end transformation;

## Modelica_LinearSystems2.Examples.StateSpace.transformationExtract

Example how to extract input/output related subsystems from state space system record

### Modelica definition

    function transformationExtract
      "Example how to extract input/output related subsystems from state space system record"

    protected
      Modelica_LinearSystems2.StateSpace ss=Modelica_LinearSystems2.StateSpace(
          A=[-1,1; 0,-2],
          B=[1,0; 0,1],
          C=[1,0; 0,1],
          D=[0,0; 0,0]);
    Integer i1[1]={1};
    Integer i2[1]={1};

    Modelica_LinearSystems2.StateSpace subSys(
        redeclare Real A[size(ss.A, 1),size(ss.A, 2)],
        redeclare Real B[size(ss.B, 1),size(i1,1)],
        redeclare Real C[size(i2,1),size(ss.C, 2)],
        redeclare Real D[size(i2, 1),size(i1, 1)]) "Subsystem state space record";
    algorithm
    subSys :=  Modelica_LinearSystems2.StateSpace.Transformation.extract(
        ss,
        i1,
        i2);
     Modelica.Utilities.Streams.print("Subsystem is " +String(subSys));
    end transformationExtract;

## Modelica_LinearSystems2.Examples.StateSpace.transformationToIrreducibleForm

Example to compute the minimal state space realization of a given SISO state space realization

### Information

This example shows the usage of function Modelica_LinearSystems2.StateSpace.reduceSystem which compute a controllable and observable state space realization of a given state space realization.

### Inputs

Type | Name | Default | Description
--- | --- | --- | ---
Real | A[:, :] | fill(0, 0, 0) |
Real | B[:, :] | fill(0, 0, 1) |
Real | C[:, :] | fill(0, 1, 0) |
Real | D[:, :] | fill(0, 1, 1) |
system data definition
String | fileName | DataDir + "abcd_siso2.mat" | file where matrix [A, B; C, D] is stored
String | matrixName | "ABCD" | Name of the state space system matrix

### Outputs

Type | Name | Description
--- | --- | ---
Boolean | ok |

### Modelica definition

    function transformationToIrreducibleForm
      "Example to compute the minimal state space realization of a given SISO state space realization"
      import Modelica_LinearSystems2.TransferFunction;
      import Modelica_LinearSystems2.StateSpace;

      input String fileName=DataDir + "abcd_siso2.mat"
        "file where matrix [A, B; C, D] is stored";
      input String matrixName="ABCD" "Name of the state space system matrix";

      input Real A[:,:]=fill(
          0,
          0,
          0);
      input Real B[:,:]=fill(
          0,
          0,
          1);
      input Real C[:,:]=fill(
          0,
          1,
          0);
      input Real D[:,:]=fill(
          0,
          1,
          1);

      output Boolean ok;

    protected
      Boolean systemOnFile=fileName <> "NoName";
      StateSpace ss=if systemOnFile then
          Modelica_LinearSystems2.StateSpace.Import.fromFile(  fileName) else
          Modelica_LinearSystems2.StateSpace(
          A=A,
          B=B,
          C=C,
          D=D);
      StateSpace sso=Modelica_LinearSystems2.StateSpace.Transformation.toIrreducibleForm(
                                                                     ss);

    algorithm
      Modelica.Utilities.Streams.print("original system:\n" + String(ss));
      Modelica.Utilities.Streams.print("\n\nminimal system:\n" + String(sso));
      ok := true;
    end transformationToIrreducibleForm;