File Exchange

image thumbnail

Symbolic derivatives

version (7.5 KB) by Jakub Rysanek
Computation of symbolic derivatives without the Symbolic Math Toolbox.


Updated 05 Oct 2016

View Version History

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 (2021). Symbolic derivatives (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (4)

Jakub Rysanek

Aah, I see your point + you are right. Luckily, the only change is to replace "3.1415....." with "pi", I will upload a fresh version of the code shortly...

Carl Witthoft

You hard-coded a value for pi inside the definition of normcdf. That's the one I'm asking about :-)

Jakub Rysanek

I am tempted to say that "pi" in fact gets carried through as a text, unless you plan to take a derivative w.r.t. a variable named 'pi'.

symbderiv('x*pi','x') yields 'pi'. You can then use eval() to store the derivative as a number and handle the working precision yourself, no digits get lost along the way.

Or am I missing something?

Carl Witthoft

Wouldn't it be possible (and more correct) to carry "pi" through as a text string than to dump in a decimal approximation?

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!