Symbolic derivatives

Computation of symbolic derivatives without the Symbolic Math Toolbox.
Updated 5 Oct 2016

View License

Entire functionality is accessible via symbderiv() function.
Only Matlab BASE is needed, additional toolboxes are not required.
The codes have been tested on M2010b, M2012b, M2016b, M2011A (Mac version)
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

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.

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

Cite As

Jakub Rysanek (2024). Symbolic derivatives (, MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2016b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!
Version Published Release Notes

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

Initial code version