Derivative

block Derivative "Approximated derivative block"
    import Modelica.Blocks.Types.Init;

    parameter Real k(unit = "1") = 1 "Gains";
    parameter SIunits.Time T(min = Modelica.Constants.small) = 0.01 "Time constants (T>0 required; T=0 is ideal derivative block)";
    parameter Modelica.Blocks.Types.Init initType = Modelica.Blocks.Types.Init.NoInit "Type of initialization (1: no init, 2: steady state, 3: initial state, 4: initial output)"
        annotation (
            Evaluate = true,
            Dialog(group = "Initialization"));
    parameter Real x_start = 0 "Initial or guess value of state"
        annotation (Dialog(group = "Initialization"));
    parameter Real y_start = 0 "Initial value of output (= state)"
        annotation (Dialog(
            enable = initType == Init.InitialOutput,
            group = "Initialization"));

    extends Interfaces.SISO;

    output Real x(start = x_start) "State of block";
protected
    parameter Boolean zeroGain = abs(k) < Modelica.Constants.eps;
initial equation
    if initType == Init.SteadyState then 
        der(x) = 0;
    elseif initType == Init.InitialState then 
        x = x_start;
    elseif initType == Init.InitialOutput then 
        if zeroGain then 
            x = u;
        else 
            y = y_start;
        end if;
    end if;
equation
    y = if zeroGain then 0 else k / T * (u - x);
    der(x) = if zeroGain then 0 else (u - x) / T;

    annotation (
        Documentation(info = "<html>\n<p>\nThis blocks defines the transfer function between the\ninput u and the output y\nas <em>approximated derivative</em>:\n</p>\n<pre>\n             k * s\n     y = ------------ * u\n            T * s + 1\n</pre>\n<p>\nIf you would like to be able to change easily between different\ntransfer functions (FirstOrder, SecondOrder, ... ) by changing\nparameters, use the general block <strong>TransferFunction</strong> instead\nand model a derivative block with parameters<br>\nb = {k,0}, a = {T, 1}.\n</p>\n\n<p>\nIf k=0, the block reduces to y=0.\n</p>\n</html>"),
        Icon(
            coordinateSystem(
                preserveAspectRatio = true,
                extent = {
                    {-100, -100}, 
                    {100, 100}}),
            graphics = {
                Line(
                    points = {
                        {-80, 78}, 
                        {-80, -90}},
                    color = {192, 192, 192}), 
                Polygon(
                    lineColor = {192, 192, 192},
                    fillColor = {192, 192, 192},
                    fillPattern = FillPattern.Solid,
                    points = {
                        {-80, 90}, 
                        {-88, 68}, 
                        {-72, 68}, 
                        {-80, 90}}), 
                Line(
                    points = {
                        {-90, -80}, 
                        {82, -80}},
                    color = {192, 192, 192}), 
                Polygon(
                    lineColor = {192, 192, 192},
                    fillColor = {192, 192, 192},
                    fillPattern = FillPattern.Solid,
                    points = {
                        {90, -80}, 
                        {68, -72}, 
                        {68, -88}, 
                        {90, -80}}), 
                Line(
                    origin = {-24.667, -27.333},
                    points = {
                        {-55.333, 87.333}, 
                        {-19.333, -40.667}, 
                        {86.667, -52.667}},
                    color = {0, 0, 127},
                    smooth = Smooth.Bezier), 
                Text(
                    lineColor = {192, 192, 192},
                    extent = {
                        {-30, 14}, 
                        {86, 60}},
                    textString = "DT1"), 
                Text(
                    extent = {
                        {-150, -150}, 
                        {150, -110}},
                    textString = "k=%k")}),
        Diagram(
            coordinateSystem(
                preserveAspectRatio = true,
                extent = {
                    {-100, -100}, 
                    {100, 100}}),
            graphics = {
                Text(
                    extent = {
                        {-54, 52}, 
                        {50, 10}},
                    textString = "k s"), 
                Text(
                    extent = {
                        {-54, -6}, 
                        {52, -52}},
                    textString = "T s + 1"), 
                Line(points = {
                    {-50, 0}, 
                    {50, 0}}), 
                Rectangle(
                    extent = {
                        {-60, 60}, 
                        {60, -60}},
                    lineColor = {0, 0, 255}), 
                Line(
                    points = {
                        {-100, 0}, 
                        {-60, 0}},
                    color = {0, 0, 255}), 
                Line(
                    points = {
                        {60, 0}, 
                        {100, 0}},
                    color = {0, 0, 255})}));
end Derivative;