# Checking derivatives by using the plotting features

This post looks at a simple way to check that derivatives of a function are correct and in the process explores some of the different plotting features of Dymola.

## When should derivatives be checked?

Most of the time it is best to let Dymola calculate derivatives (see automatically-getting-derivatives-of-a-function-in-dymola). However Dymola cannot always calculate the derivative of a function, for example derivatives of external functions have to be supplied as described in applying-derivatives-to-functions-in-dymola. These supplied derivative functions should be checked.

## A simple example of where derivatives are supplied

Below is a simple example where the derivative of a function is supplied.

function sin_2x
input Real x;
output Real y;
algorithm
y :=sin(2*x);
annotation(Inline=false, derivative = sin_2x_der);
end sin_2x;

function sin_2x_der “derivative of sin_2x function”
input Real x;
input Real x_der;
output Real y_der;
algorithm
y_der :=2*x_der*cos(x) “this is incorrect on purpose”;
end sin_2x_der;

This simple example is just to show how checking of derivatives can be done and is used throughout this post.

## Example of checking the derivative of a function

The derivative of the sin_2x() function is checked.

### A simple test model

A simple model is created that calculates the derivative of the function as below:

model testModel
Real y=sin_2x(time);
Real y_der=der(y);
annotation (experiment(
Interval=0.01,
__Dymola_fixedstepsize=0.01,
__Dymola_Algorithm=”Euler”));
end testModel;

### Simulating the test model

The sin_2x function is simulated with a fixed step solver and the results are stored at a fixed interval, of 0.01[s]. Make sure that Store variables at events is not selected in Simulation>Setup on the Output tab as in Figure 1.

Store variables at events in Figure 1 has to be deselected to ensure that the data is only stored at the fixed interval.

### Using the difference signal operator

Simulate and plot the output of the function. Then right click on the signal select Signal Operators>Difference as in Figure 2.

The difference signal operator calculates the difference between consecutive grid points of the signal and plots the difference signal on the right axis. There are a number of other useful Signal Operators as seen in Figure 2.

The original signal y can now be deleted and the derivative signal y_der can be plotted on the same graph.

However, the signals are scaled differently which makes comparing these signals in Figure 3 difficult.

### Scaling y_der

The reason for the different scaling is that the difference calculation is:
diff_y[i] = x[i] – x[i-1]
and the derivative can be approximated using the forward difference method:
y_der[i] ≈ (x[i] – x[i-1])/h
where h is the time step between grid points.

So if the derivative signal is scaled by the time step, h, then the results should be comparable. This can be done by right clicking on the derivative signal, selecting Plot Expression and scaling the derivative signal by h as in Figure 4. In this case the time step, h, is 0.01[s].

The Plot Expression option can be used to plot expressions of signals as in Figure 4. These expressions can contain Modelica library functions.
For the final comparison the y_der signal is deleted and the diff(y) signal is put onto the left axis by right clicking on the diff(y) signal, selecting Setup… and setting Vertical axis to Left.

In the final comparison in Figure 5 it is clear that there is an error in the derivative signal calculation. This is because the correct derivative equation is:

y_der :=2*x_der*cos(2*x);

## Alternatively generate the checking plot using functions

The above comparison is done using the Dymola GUI, this comparison can also be done using the plotting functions stored in DymolaCommands.Plot, some of the plotting functions are in Figure 6.

The createPlot, plotExpression and plotSignalDifference functions will be used from the DymolaCommands in Figure 6. To find out more information about the plot functions view them in Dymola.

The following commands can be used to generate the same derivative checking plot as in Figure 5:

DymolaCommands.Plot.createPlot();
DymolaCommands.Plot.plotSignalDifference(“y”, axis=0, id=0);
DymolaCommands.Plot.plotExpression(apply(testModel[end].y_der*0.01), false, “y_der*0.01”);

The above functions must be called after the experiment has been simulated; in this example the model is called testModel.

## Conclusion

There are a number of useful plotting features in Dymola that can be used directly from the GUI or by using the DymolaCommands functions. These features are used to check the derivative calculation of a function.

Written by: Garron Fish – Chief Engineer

Please get in touch if you have any questions or have got a topic in mind that you would like us to write about. You can submit your questions / topics via: Tech Blog Questions / Topic Suggestion