matlab-deriv-operation
is a MATLAB module for performing auto differentiation base on OOP(object-oriented programming). Currently, matlab-deriv-operation
is on develop and supports only the basic features.
The most important component in this module is DerivVariable
class. An object of DerivVariable
can store not only the value of a variable but also values of its first, second, ... order derivative with respect to the specified independent variable. The syntax for defining an object of DerivVariable
class is
derivVar = DerivVariable(value, value_of_first_order_deriv, value_of_second_order_deriv, ..., value_of_the_highest_order_deriv);
The number of input arguments to the constructor of DerivVariable
depends on the highest order of the derivative for which we want to track. For example, assume that the value and time derivatives of variable DerivVariable
can be defined as
x = DerivVariable([1; 0], [-1; 1], [1; -1]);
Denoting the value of x
(0th order derivative) as x_0
and its first and second order time derivatives as x_1
, x_2
respectively, each value can be get using deriv
method as the following:
x_0 = x.deriv(0); % x_0 = [1; 0]
x_1 = x.deriv(1); % x_1 = [-1; 1]
x_2 = x.deriv(2); % x_2 = [1; -1]
Now let's define a new variable z_1
and z_2
respectively, each value can be simply get as the following:
z = norm(x); % same as z = x.norm()
z_1 = z.deriv(1); % z_1 = -1
z_2 = z.deriv(2); % z_2 = 2
The independent variable with which the differentiation is performed can be explicitly defined using IndepVariable
class. The syntax for defining an object of IndepVariable
class is
indepVar = IndepVariable(value, the_highest_order_to_track)
value
represents the current value of the independent variable and the_highest_order_to_track
represents the highest order of the derivative to be tracked. Using the defined independent variable, any other variable can be defined and derivatives of the new variable can be easily get just by calling deriv
method of the variable. For example, assume that a rotation matrix
t = IndepVariable(1, 2);
R = [1, 0, 0;
0, cos(0.1*pi*t), sin(0.1*pi*t);
0, -sin(0.1*pi*t), cos(0.1*pi*t)];
R_1 = R.deriv(1); % R_1 = [0, 0, 0; 0, -0.0971, 0.2988; 0, -0.2988, -0.0971]
R_2 = R.deriv(2); % R_2 = [0, 0, 0; 0, -0.0939, -0.0305; 0, 0.0305, -0.0939]
where R_1
and R_2
denotes the first and second derivatives, respectively.