Code covered by the BSD License  

Highlights from
Automatic Differentiation with Matlab Objects

4.5

4.5 | 2 ratings Rate this file 31 Downloads (last 30 days) File Size: 137 KB File ID: #26807

Automatic Differentiation with Matlab Objects

by

 

01 Mar 2010 (Updated )

Automatically compute derivatives of functions, without using finite-difference approximations.

| Watch this File

File Information
Description

Automatic differentiation is a technique for computing the derivatives of a function using the chain rule. Matlab objects make it easy to implement automatic differentiation. Note that this package is implemented in a rather old version of Matlab. You may need to edit it for newer versions.

An example of using automatic differentiation to compute the value and derivative of the Rosenbrock function at the point [1,2] is as follows:

x=adiff([1,2]); % create the automatic differentiation object at [1,2]
rosen = 100*(x(1)^2-x(2))^2+(x(1)-1)^2; % compute rosenbrock func.
[x,dx] = adiffget(x); % retrieve the value x and derivative dx

Then x = 100 and dx = [-400,200].

The adiff object includes a helper function to convert any optimization without derivatives into one with derivatives. For example, if you have a function f which you wish to optimize, but it doesn't compute derivatives, it is usually enough to call

fminunc('autodiff',x0,options,'f',...)

The zip file includes a pdf help file.

MATLAB release MATLAB 5.3.1 (R11.1)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (7)
22 Aug 2014 William McIlhagga

To Jeffrey Steward:

The documentation says that adiff has restrictions on transpose. (See page 4 where it says that your example _won't_ work). However, you can get a quadratic form to work if you bracket things just right. Writing

f = x'*(A*x)

does work.

21 Aug 2014 Jeffrey Steward

Doesn't seem to be able to handle a really simple function like this:

x'*A*x

where A is a matrix. Try this

x = adiff([1,2]);
A = eye(2);
f = x'*A*x;

and there is an error in mtimes.m

Error using *
Inner matrix dimensions must agree.

24 May 2013 William McIlhagga

To Edward: For simplicity, the package was coded to work solely on functions that take a column vector and return a scalar argument. I can't see reshape helping much because of that restriction. After you've got the derivative though, you can extract it from the adiff object as an ordinary matlab vector (using adiffget) and reshape that.

22 May 2013 Edward

How hard would it be to have the function reshape supported? Currently it throws errors. I think because numel(a) = 1, where a is an adiff object.

I am just beginning to learn about auto differentiation and am pretty excited about something that seems to be a very powerful tool. Thanks for helping me get started.

29 Mar 2011 William McIlhagga

It's just plain forward auto.

29 Mar 2011 Jichao Yin

really good. I used this developed a simple single-phase nonlinear reservoir simulator. One question: is this adiff based on Reverse AD (Rad) or Forward AD (Fad)? Based on its usage, looks Rad to me compared to Sacado or original Fad

21 Nov 2010 Dan

The idea is great. It is impossible to compute jacobian with respect to numerous variables using standard Symbolic Toolbox.
However I found that the expression x'*A*x causes an error in Matlab 2010a. I was able to correct it by writing x'*(A*x) and correcting the 'adiffadiff' case in mtimes.m file (transposition should be added to first factors).
Is there a way to allow free usage of matrix expressions (i.e. free transposition usage and maybe allow to calculate derivatives with respect to a matrix)? I haven't found any matlab library that allows of doing it with respect to vector of variable length.

Updates
13 Jul 2010

Minor bug fix to atan function

29 Mar 2011

Changed code & documentation to fix problem noted by Dan.

Contact us