# MAD Using MAD

### From TomWiki

This page is part of the MAD Manual. See MAD Manual. |

MAD can be used for automatic differentiation in TOMLAB. TOMLAB features several methods for obtaining derivatives of objective function, gradients, constraints and jacobians.

- Supplying analytical derivatives. This is the recommended method at almost all times, as the highest speed and robustness are provided.

- Five methods for numerical differentiation. This is controlled by the flags,
*Prob.NumDiff*and*Prob.ConsDiff*.

- The solver can estimate the derivatives internally. This is not available for all solvers. TOMLAB /SNOPT also provides derivative checking. See the TOMLAB /SNOPT manual for more information.

- Automatic differentiation with MAD. This is controlled by setting the flags
*Prob.ADObj*and*Prob.ADCons*to 1 or -1. If the flags are set to 1, only the objective function and constraints are given. If the flags are set to -1, second derivatives will be calculated. In this case the first derivatives must be supplied.

## Example

The following example illustrates how to use TOMLAB /MAD when the objective function and constraints are given. MAD uses global variables that need to be initialized.

madinitglobals; Prob = conAssign( ... ); % A standard problem structure is created. Prob.ADObj = 1; % First derivatives obtained from MAD for objective. Prob.ADCons = 1; % First derivatives obtained from MAD for constraints. Result = tomRun('snopt', Prob, [], 1) % Using driver routine tomRun.

If the user has provided first order information as well, the following code could be used. Observe

that the solver must use second order information for this to be useful.

madinitglobals; Prob = conAssign( ... ); % A standard problem structure is created. Prob.ADObj = -1; % Second derivatives obtained from MAD for objective. Prob.ADCons = -1; % Second derivatives obtained from MAD for constraints. Result = tomRun('knitro', Prob, [], 1) % Using driver routine tomRun. % KNITRO uses second order information.

The number of MATLAB functions that MAD supports is limited. Please report needed additions.