0

I have a Matlab function which I need to 1) Speed up 2) Generate derivatives of the function by means of Automatic Differentiation (AD).

The speed up part I have accomplished by using Matlab Coder to generate C code and a mex function. Now I am looking for an AD tool which out of the box (or with minor code modifications) can be used for this auto generated C code. It seems to be a lot of C/C++ AD tools out there, see C/C++ AD tools, and I have tested a couple for this purpose:

  • Tapenade: did not work well with the Matlab Coder generated files
  • ADOL-C: Need to rewrite the C functions to some specific data types (I think many of the tools has such requirements)

Does anyone have some experience using such tools for Matlab Coder generated code?

PS. The AD tool should work on Windows.

Petter T
  • 3,387
  • 2
  • 19
  • 31
  • Is it a requirement to use MATLAB in the first place? MATLAB code can be quite easily ported to Julia (http://julialang.org) which, when used correctly, can get C-like speed and has several AD tools available (e.g. https://github.com/tshort/AutoDiff.jl) – Tomas Aschan Sep 03 '15 at 08:55
  • @tomas-lycken: Matlab would definitely be my preferred tool, but if I do not get this to work within Matlab I may do as you suggest. – Petter T Sep 03 '15 at 13:18
  • A question, is it the plotting which makes matlab your choice? In that case, you can write the data to file, and plot it later. The reason I take this up is that you seem to have requirements for your program, which seemingly requires a lot of adoptions to work. If you need to convert major parts of your code to c code, then something else may be easier. – patrik Sep 04 '15 at 06:52
  • @patrik: The rest of my code base is in Matlab, and the data which the mentioned function is based on is generated in Matlab. I only need to speed up a small part of my code, which is the cost function / constraints for an optimization problem. Thus the need for AD – Petter T Sep 04 '15 at 13:54

1 Answers1

1

If you have the symbolic toolbox, you can use it to generate the analytic derivatives. Assuming you have a function like z = f(x,y) you create symbolic variables

x = sym('x', [mx,nx]); 
y = sym('y', [my,ny]);

where mx,nx,mx,my specify the size of the input variables. Call f using your symbolic variables (e.g. z = f(x,y)).

Now you can create the derivatives using the function jacobian.

jac_z_x = jacobian(z,x(:));
jac_z_y = jacobian(z,y(:));

Write the derivatives to a matlab file using matlabFunction. If required you can compile the function with MATLAB Coder. This is how I do it for my optimization Problems.

Hope this helps Matthias

Matthias
  • 26
  • 1
  • Thanks for your answer. Actually my problem was too complex for this to work. I solved it by using the AdiMat package. However, your solution is a good one to the problem as it was stated, I have used this myself on an earlier occation. I will mark this as the accepted solution. – Petter T Sep 25 '15 at 10:03
  • If writing the matlab function took very long this is due to the fact that matlab automatically does some code optimization. Since MATLAB2015a you can diable that optimization allowing you to use more complex functions. – Matthias Oct 08 '15 at 00:10