Why is there no Derivative block in the component library?

I can find an Integrator but no Derivative. What is the reason for this?

1 Like

PLECS does not provide a Derivative block because numerical derivatives have very bad numerical properties. To be reasonably accurate, they need 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 amplifies noise in the system.

It is therefore preferable to formulate a problem in such a way that it can be modeled with integrators rather than derivatives.

If you absolutely must use a derivative in conjunction with a variable-step solver, you can implement one with a C-Script as follows:

Setup

Number of inputs: 1Number of outputs: 1Number of disc. states: 2Sample time: 0Code 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);

See the attached PLECS Standalone demo model for a working implementation. Notice the visible difference between the differentiated sine signal and the cosine signal. This difference will become smaller if you reduce the maximum step size in the solver settings.

Derivative.plecs (11.1 KB)