## Modelica_LinearSystems2.Examples.TransferFunction

### Package Content

Name | Description
--- | ---
plotPolesAndZeros | Example for plotting poles and zeros of two transfer functions
importFromFile | Example how to read a transfer function from a matlab file
operationsOnTransferFunctions | Demonstrate the usage of the functions of Modelica_LinearSystems2.TransferFunction
plotBode1 | Construct a transfer function and plot the Bode diagram with automatic determination of the frequency range to plot
plotBode2 | Construct 2 transfer functions and plot the Bode diagram with automatic determination of the frequency range to plot
plotBode3 | Construct 2 transfer functions and plot the Bode diagram with automatic determination of the frequency range to plot
plotBodeFilter2 | Show low pass filters of all filter types in one diagram
plotImpulse | Impulse plot example
plotInital | Initial condition plot example
plotStep | Step plot example

## Modelica_LinearSystems2.Examples.TransferFunction.plotPolesAndZeros

Example for plotting poles and zeros of two transfer functions

### Modelica definition

    function plotPolesAndZeros
      "Example for plotting poles and zeros of two transfer functions"

      import Modelica_LinearSystems2;
      import Modelica_LinearSystems2.TransferFunction;
      import Modelica_LinearSystems2.ZerosAndPoles;
      import Modelica_LinearSystems2.Math.Complex;

    protected
      TransferFunction s = TransferFunction.s();
      Complex j = Complex.j();
      TransferFunction tf1 = (s-2)*(s+4)/( (s+1)*(s+3)*TransferFunction({-0.5+2*j, -0.5-2*j}));
      TransferFunction tf2 = (s^3 + 4*s + 1)/(s^4 + 2*s^3 + 3*s^2 + 4*s);

    algorithm
      Modelica_LinearSystems2.TransferFunction.Plot.polesAndZeros(tf=tf1,
        defaultDiagram=Modelica_LinearSystems2.Internal.DefaultDiagramPolesAndZeros(
                           heading="Poles and zeros of " + String(tf1)));
      Modelica_LinearSystems2.TransferFunction.Plot.polesAndZeros(
        tf=tf2,
        defaultDiagram=Modelica_LinearSystems2.Internal.DefaultDiagramPolesAndZeros(
          heading="Poles and zeros of " + String(tf2)),
        device=Modelica_LinearSystems2.Utilities.Plot.Records.Device(xTopLeft=50, yTopLeft=30));

    end plotPolesAndZeros;

## Modelica_LinearSystems2.Examples.TransferFunction.importFromFile

Example how to read a transfer function from a matlab file

### Inputs

Type | Name | Default | Description
--- | --- | --- | ---
system data definition
Boolean | systemOnFile | true | true, if state space system is defined on file
String | fileName | DataDir + "tf_siso_1.mat" | file where numenator n and denominator d are stored
coefficients
Polynomial | n | Polynomial({1,2,3}) |
Polynomial | d | Polynomial({4,5,6}) |

### Outputs

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

### Modelica definition

    function importFromFile
      "Example how to read a transfer function from a matlab file"
      import Modelica_LinearSystems2.TransferFunction;
      import Modelica_LinearSystems2.Math.Polynomial;

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

      input String fileName=DataDir + "tf_siso_1.mat"
        "file where numenator n and denominator d are stored";

      input Polynomial n=Polynomial({1,2,3});
      input Polynomial d=Polynomial({4,5,6});
      output Boolean ok;

    protected
      TransferFunction tf=if systemOnFile then TransferFunction.Import.fromFile(fileName) else TransferFunction(n=n, d=d);

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

    end importFromFile;

## Modelica_LinearSystems2.Examples.TransferFunction.operationsOnTransferFunctions

Demonstrate the usage of the functions of Modelica_LinearSystems2.TransferFunction

### Outputs

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

### Modelica definition

    function operationsOnTransferFunctions
      "Demonstrate the usage of the functions of Modelica_LinearSystems2.TransferFunction"
      import Modelica.Utilities.Streams.print;
      import Modelica_LinearSystems2.Math.Complex;
      import Modelica_LinearSystems2.TransferFunction;
      import Modelica_LinearSystems2.ZerosAndPoles;

      output Boolean ok;

    protected
      Complex j = Modelica_LinearSystems2.Math.Complex.j();
    TransferFunction tf1=TransferFunction(n={1,2}, d={2,3,4});
      TransferFunction tf2=TransferFunction(3.4);
      ZerosAndPoles zp1=ZerosAndPoles({-1+0*j},{1+0*j,2+3*j,2-3*j},  k=4);

      ZerosAndPoles zp2=ZerosAndPoles(
          fill(Complex(0), 0),{0.1+0*j}, k=5);
      TransferFunction tf4=ZerosAndPoles.Conversion.toTransferFunction(zp1);
      TransferFunction tf5=ZerosAndPoles.Conversion.toTransferFunction(zp2);

      TransferFunction tf5a=TransferFunction({1}, {1,1});
      TransferFunction tf6=Modelica_LinearSystems2.TransferFunction.s();
      TransferFunction tf7=-tf1;
      TransferFunction tf8=tf1 + tf4;

      TransferFunction tf9=tf1 - tf4;

      TransferFunction tf10=tf1*tf4;
      TransferFunction tf11=tf1/tf4;
      TransferFunction tf12=tf1^2;
      Boolean same1=tf1 == tf1;
      Boolean same2=tf1 == tf4;
      Complex y1=TransferFunction.Analysis.evaluate(tf1, 2+3*j);
      Complex numZeros[:];
      Complex denZeros[:];
      Real k;
      Real k5;
    algorithm
      (numZeros,denZeros,k) := TransferFunction.Analysis.zerosAndPoles(tf4);
      print("      tf1 = " + String(tf1));
      print("      tf2 = " + String(tf2));
      print("      tf4 = " + String(tf4));
      (numZeros,denZeros,k5) := TransferFunction.Analysis.zerosAndPoles(tf5);
      tf5a := ZerosAndPoles.Conversion.toTransferFunction(ZerosAndPoles(
        numZeros,
        denZeros,
        k));
      print("      tf5 = " + String(tf5a));
      print("      tf6 = " + String(tf6));
      print("     -tf1 = " + String(tf7));
      print("  tf1+tf4 = " + String(tf8));
      print("  tf1-tf4 = " + String(tf9));
      print("  tf1*tf4 = " + String(tf10));
      print("  tf1/tf4 = " + String(tf11));
      print("  tf1^2   = " + String(tf12));
      print(" tf1==tf1 = " + String(same1));
      print(" tf1==tf4 = " + String(same2));
      print("tf1(2+3j) = " + String(y1));
      print("        k = " + String(k) + " // (zeros,poles,k) = zerosAndPoles(tf4)");
      print("numerator   degree of tf1 = " + String(
        TransferFunction.Analysis.numeratorDegree(tf1)));
      print("denominator degree of tf1 = " + String(
        TransferFunction.Analysis.denominatorDegree(tf1)));
      ok := true;
    end operationsOnTransferFunctions;

## Modelica_LinearSystems2.Examples.TransferFunction.plotBode1

Construct a transfer function and plot the Bode diagram with automatic determination of the frequency range to plot

### Outputs

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

### Modelica definition

    function plotBode1
      "Construct a transfer function and plot the Bode diagram with automatic determination of the frequency range to plot"

      import Modelica.Utilities.Streams.print;
      import Modelica_LinearSystems2.Math.Complex;
      import Modelica_LinearSystems2.TransferFunction;

      output Boolean ok;

    protected
     TransferFunction s = Modelica_LinearSystems2.TransferFunction.s();

      TransferFunction tf1= 1/(10*s+1)^3;
      TransferFunction tf2=(s + 2)/(2*s^2 + 3*s +4);
    algorithm
      Modelica_LinearSystems2.TransferFunction.Plot.bode(
        tf=tf1);
      Modelica_LinearSystems2.TransferFunction.Plot.bode(
        tf=tf2);
      ok := true;

    end plotBode1;

## Modelica_LinearSystems2.Examples.TransferFunction.plotBode2

Construct 2 transfer functions and plot the Bode diagram with automatic determination of the frequency range to plot

### Outputs

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

### Modelica definition

    function plotBode2
      "Construct 2 transfer functions and plot the Bode diagram with automatic determination of the frequency range to plot"

      import Modelica.Utilities.Streams.print;
      import Modelica_LinearSystems2.Math.Complex;
      import Modelica_LinearSystems2.TransferFunction;

      output Boolean ok;
    protected
      TransferFunction tf1=TransferFunction({1,2}, {2,3,4});
      TransferFunction tf2=TransferFunction({1,2}, {4,1,4});

    algorithm
      Modelica_LinearSystems2.TransferFunction.Plot.bode(
        tf1,
        autoRange=false,
        f_min=0.01,
        f_max=30);
      Modelica_LinearSystems2.TransferFunction.Plot.bode(
        tf2,
        autoRange=false,
        f_min=0.01,
        f_max=30);
      ok := true;

    end plotBode2;

## Modelica_LinearSystems2.Examples.TransferFunction.plotBode3

Construct 2 transfer functions and plot the Bode diagram with automatic determination of the frequency range to plot

### Outputs

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

### Modelica definition

    function plotBode3
      "Construct 2 transfer functions and plot the Bode diagram with automatic determination of the frequency range to plot"

      import Modelica.Utilities.Streams.print;
      import Modelica_LinearSystems2.Math.Complex;
      import Modelica_LinearSystems2.TransferFunction;

      output Boolean ok;
    protected
      TransferFunction tf=TransferFunction({1}, {1,0,1});

    algorithm
      Modelica_LinearSystems2.TransferFunction.Plot.bode(    tf);
      ok := true;

    end plotBode3;

## Modelica_LinearSystems2.Examples.TransferFunction.plotBodeFilter2

Show low pass filters of all filter types in one diagram

### Inputs

Type | Name | Default | Description
--- | --- | --- | ---
Integer | order | 4 | Order of filter
Frequency | f_cut | 1 | Cut-off frequency [Hz]
Real | A_ripple | 3 | Pass band ripple for Chebyshev filter (otherwise not used) [dB]
Frequency | f_min | 0.1 | Minimum frequency value [Hz]
Frequency | f_max | 10 | Maximum frequency value [Hz]

### Outputs

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

### Modelica definition

    function plotBodeFilter2
      "Show low pass filters of all filter types in one diagram"
      import Modelica;
      import Modelica_LinearSystems2.TransferFunction;
      import AF = Modelica_LinearSystems2.Types.AnalogFilter;

      input Integer order(min=1) = 4 "Order of filter";
      input Modelica.SIunits.Frequency f_cut=1 "Cut-off frequency";
      input Real A_ripple(unit="dB") = 3
        "Pass band ripple for Chebyshev filter (otherwise not used)";
      input Modelica.SIunits.Frequency f_min(min=0) = 0.1 "Minimum frequency value";
      input Modelica.SIunits.Frequency f_max(min=0) = 10 "Maximum frequency value";
      output Boolean ok;
    protected
      TransferFunction tf1=TransferFunction.Design.filter(
          analogFilter=AF.CriticalDamping,
          order=order,
          f_cut=f_cut,
          A_ripple=A_ripple);
      TransferFunction tf2=TransferFunction.Design.filter(
          analogFilter=AF.Bessel,
          order=order,
          f_cut=f_cut,
          A_ripple=A_ripple);
      TransferFunction tf3=TransferFunction.Design.filter(
          analogFilter=AF.Butterworth,
          order=order,
          f_cut=f_cut,
          A_ripple=A_ripple);
      TransferFunction tf4=TransferFunction.Design.filter(
          analogFilter=AF.Chebyshev,
          order=order,
          f_cut=f_cut,
          A_ripple=A_ripple);
    algorithm
      Modelica_LinearSystems2.TransferFunction.Plot.bode(
        tf1,
        autoRange=false,
        f_min=f_min,
        f_max=f_max);
      Modelica_LinearSystems2.TransferFunction.Plot.bode(
        tf2,
        autoRange=false,
        f_min=f_min,
        f_max=f_max);
      Modelica_LinearSystems2.TransferFunction.Plot.bode(
        tf3,
        autoRange=false,
        f_min=f_min,
        f_max=f_max);
      Modelica_LinearSystems2.TransferFunction.Plot.bode(
        tf4,
        autoRange=false,
        f_min=f_min,
        f_max=f_max);

      ok := true;

    end plotBodeFilter2;

## Modelica_LinearSystems2.Examples.TransferFunction.plotImpulse

Impulse plot example

### Information

Computes the impulse response of the system tf =1/s^2 + s + 1.

### Inputs

Type | Name | Default | Description
--- | --- | --- | ---
TransferFunction | tf | TransferFunction(n={1}, d={1... |

### Modelica definition

    function plotImpulse "Impulse plot example"

      import Modelica_LinearSystems2.TransferFunction;

     input TransferFunction tf=TransferFunction(n={1}, d={1,1,1});

    algorithm
      Modelica_LinearSystems2.TransferFunction.Plot.impulse(
        tf=tf,
        dt=0.1,
        tSpan=10);

    end plotImpulse;

## Modelica_LinearSystems2.Examples.TransferFunction.plotInital

Initial condition plot example

### Information

### Inputs

Type | Name | Default | Description
--- | --- | --- | ---
TransferFunction | tf | TransferFunction({1}, {1,1,1}) |

### Modelica definition

    function plotInital "Initial condition plot example"

      import Modelica_LinearSystems2.TransferFunction;

     input TransferFunction tf=TransferFunction({1}, {1,1,1});

    protected
      Real y0=1 "Initial state vector";
    algorithm
      Modelica_LinearSystems2.TransferFunction.Plot.initialResponse(tf=tf, y0=y0, dt=0.1, tSpan=10);

    end plotInital;

## Modelica_LinearSystems2.Examples.TransferFunction.plotStep

Step plot example

### Information

Computes the impulse response of the system tf =1/s^2 + s + 1.

### Inputs

Type | Name | Default | Description
--- | --- | --- | ---
TransferFunction | tf | TransferFunction({1}, {1,1,1}) |

### Modelica definition

    function plotStep "Step plot example"

      import Modelica_LinearSystems2.TransferFunction;

     input TransferFunction tf=TransferFunction({1}, {1,1,1});

    algorithm
    Modelica_LinearSystems2.TransferFunction.Plot.step(
        tf=tf,
        dt=0.1,
        tSpan=10);
    end plotStep;