automaticdifferenti​ation

Computes the differentiation of a function handle with respect to specified variables at queried points
30 Downloads
Updated 9 May 2023

View License

automaticdifferentiation
Computes the differentiation of a function handle with respect to specified variables at provided points. It makes use of the built-in automatic differentiation (AD) framework of Matlab's Deep Learning Toolbox.
Calls
df = automaticdifferentiation(f,x,n);
df = automaticdifferentiation(f,x,nx,y,ny...);
df = automaticdifferentiation(...,ParallelFlag); % ParallelFlag = true/false
Inputs
f
Function handle. The number of inputs that f admits must match the number of provided Coordinates and Orders of differentiation. Must return a numeric array for each evaluation point.
x, y, ...
Coordinates of the points where derivatives are to be evaluated (numeric arrays of the same size).
nx,ny, ...
Order of differentiation for each variable (non-negative integer scalars).
ParallelFlag
Allow parallel for loops or not (scalar boolean). Optional input, the default is serial for loops.
Outputs:
df
Derivative of f with respect to specified variables, evaluated at the provided points. Numeric array with size equal to the concatenation of the sizes of theCoordinate inputs and the output of f, without singleton dimensions.
Note: The evaluation of the function f is not vectorized. Instead, a serial or parallel for loop is executed for each evaluation point. Therefore, f does not need to be defined using point-wise operators.
Examples
Scalar field in R2
x = 0:0.01:0.1;
y = 0:0.01:0.2;
[xx,yy] = meshgrid(x,y);
f = @(x,y) sin(2*pi*x.*y)+ y.^2; figure; surf(xx,yy,f(xx,yy));
dfdx = automaticdifferentiation(f,xx,1,yy,0); figure; surf(xx,yy,dfdx);
d2fdx2 = automaticdifferentiation(f,xx,2,yy,0); figure; surf(xx,yy,d2fdx2);
d2fdxdy = automaticdifferentiation(f,xx,1,yy,1); figure; surf(xx,yy,d2fdxdy);
Vectorial field in R1
g = @(x) [sin(2*pi.*x), x.*tan(2*pi.*x)]; figure; plot(x,g(x));
dgdx = automaticdifferentiation(g,x,1); figure; plot(x,dgdx(:,1),x,dgdx(:,2));

Cite As

David Codony (2025). automaticdifferentiation (https://www.mathworks.com/matlabcentral/fileexchange/129229-automaticdifferentiation), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2022b
Compatible with R2021a and later releases
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
1.0.0