Why is there no “derivative” block in the PLECS component library?

Problem description: 

I can find an integrator block but not a derivative block in the PLECS component library. What is the reason for this?


In general, numerical derivatives have very bad properties and to be reasonably accurate they need very small time steps. The automatic step-size control of a variable-step solver, however, only works for integrators but not for derivatives. Another practical issue is that a derivative block amplifies noise in the input signal.

It is therefore usually preferable to formulate a problem so that it can be modelled with integrators instead of derivatives.

If you absolutely must use a derivative in conjunction with a variable-step solver, you can implement one with a C-Script with the following content/configuration:


Number of inputs: 1
Number of outputs: 1
Number of disc. states: 2
Sample time: 0

Code declarations:

#define lastTime DiscState(0)
#define lastInput DiscState(1)

Start function:

lastTime = CurrentTime;
Output(0) = 0;

Output function:

if (lastTime != CurrentTime)
   Output(0) = (Input(0) - lastInput)/(CurrentTime - lastTime);

Update function:

lastTime = CurrentTime;
lastInput = Input(0);