Define derivative of function
function foo0 annotation(derivative=foo1); end foo0; function foo1 annotation(derivative(order=2)=foo2); end foo1; function foo2 end foo2;
Derivatives of functions can be declared explicitly using the derivative annotation, whereas a function can be defined as a partial derivative of another function using the der-operator in a short function definition.
A function declaration can have an annotation derivative specifying the derivative function. This can influence simulation time and accuracy and can be applied to both functions written in Modelica and to external functions. A derivative annotation can state that it is only valid under certain restrictions on the input arguments. These restrictions are defined using the following optional attributes: order (only a restriction if order > 1, the default for order is 1), noDerivative, and zeroDerivative. The given derivative-function can only be used to compute the derivative of a function call if these restrictions are satisfied. There may be multiple restrictions on the derivative, in which case they must all be satisfied. The restrictions also imply that some derivatives of some inputs are excluded from the call of the derivative (since they are not necessary). A function may supply multiple derivative functions subject to different restrictions.
The inputs to the derivative function of order 1 are constructed as follows:
Example: Given the declarations
function foo0 ... input Real x; input Boolean linear; input ...; output Real y; ... annotation(derivative=foo1); end foo0; function foo1 ... input Real x; input Boolean linear; input ...; input Real der_x; ... output Real der_y; ... annotation(derivative(order=2)=foo2); end foo1; function foo2 ... input Real x; input Boolean linear; input ...; input Real der_x; ...; input Real der_2_x; ... output Real der_2_y; ...
the equation
(...,y(t),...)=foo0(...,x(t),b,...);
implies that:
(...,d y(t)/dt,...)=foo1(...,x(t),b,..., ...,d x(t)/dt,...); (...,d^2 y(t)/dt^2,...)=foo2(...,x(t),b,...,d x(t)/dt,..., ...,d^2 x(t)/dt^2,...);
An input or output to the function may be any simple type (Real, Boolean, Integer, String and enumeration types) or a record, provided the record does not contain both reals and non-reals predefined types. The function must have at least one input containing reals. The output list of the derivative function may not be empty.
function f "Simple table lookup" input Real x; input Real y[:, 2]; output Real z; annotation(derivative(zeroDerivative=y) = f_der, derivative=f_general_der); algorithm ... end f; function f_der "Derivative of simple table lookup" input Real x; input Real y[:, 2]; input Real x_der; output Real z_der; algorithm ... end f_der; function f_general_der "Derivative of table lookup taking into account varying tables" input Real x; input Real y[:, 2]; input Real x_der; input Real y_der[:, 2]; output Real z_der; algorithm ... end f_general_der;
This is useful if g represents the major computational effort of fg).function fg input Real x; output Real z; algorithm z := f(x, g(x)); end fg; function f input Real x; input Real y; output Real z; annotation(derivative(noDerivative(y = g(x))) = f_der); algorithm ... end f; function f_der input Real x; input Real x_der; input Real y; output Real z_der; algorithm ... end f_der;