Symbolic derivatives

Computation of symbolic derivatives without the Symbolic Math Toolbox.
540 téléchargements
Mise à jour 5 oct. 2016

Afficher la licence

Entire functionality is accessible via symbderiv() function.
Compatibility:
--------------
Only Matlab BASE is needed, additional toolboxes are not required.
The codes have been tested on M2010b, M2012b, M2016b, M2011A (Mac version)
Installation:
-------------
Main folder containing symbderiv() must be added to the Matlab search path.
Intended use:
--------------
symbderiv('x^2','x') computes the derivative of 'x^2' with respect
to a variable named 'x'. In this case the function returns the expected
result '2*x'. Computation of partial derivatives is straight-forward in that
all variables other than the selected variable are treated as constant.
Therefore, e.g. symbderiv('x*y*z','x') returns y*z.
The input function has to be a string, and the output derivative is also
returned as strings. Standard Matlab function eval() can then be used to
evaluate all computed derivatives numerically at given points.
The symbderiv() function is not suited for the computation of
Jacobians and Hessians directly. One can, however, make use of repeated
calls to symbderiv().

Example :
equations = {'exp(x^2)+y';'sin(x)'};% The system of 2 equations
varlist = {'x','y'}; % List of variables
jac = cell(2,2); % Container for Jacobian
for i = 1:length(equations)
jac(i,:) = symbderiv(equations{i},varlist); % Each Jacobian row as a gradient call
end

Example :
equation = '(x+y)^2';
symbderiv( symbderiv(equation,'x') ,'y') % dF/dxdy -> symbderiv() is simply called twice

Example :
equation = 'x^2';
analderiv = symbderiv(equation,'x');
x = 5; % -> we plan to evaluate the derivative at this point
numderiv = eval(analderiv);

User customization
------------------
The file func_list.m contains definitions of derivatives
for commonly used elementary functions, such as log(), sin(), etc.
This file can be freely expanded with other functions (potentially
user-defined) and their derivatives.

Note
----
The computed symbolic derivatives may look weird at first sight.
E.g. symbderiv('(x+y)^2','x') yields '((2)*(x+y)^(2-1)*(((1))))'
instead of '2*(x+y)'. Simplification rules on top of the computed
result are not applied in order not to slow down the computations.
The consequent numerical evaluation of the computed derivatives
using eval() tends to be rather fast in Matlab even if the underlying
expression contains many nested parentheses and redundant algebraic
operations.

Citation pour cette source

Jakub Rysanek (2024). Symbolic derivatives (https://www.mathworks.com/matlabcentral/fileexchange/59438-symbolic-derivatives), MATLAB Central File Exchange. Récupéré le .

Compatibilité avec les versions de MATLAB
Créé avec R2016b
Compatible avec toutes les versions
Plateformes compatibles
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Publié le Notes de version
1.1.0.0

...3.1415 constant in the derivative definition of normcdf() changed to "pi"

1.0.0.0

Initial code version

...