Using the NAG Toolbox for MATLAB

The NAG Toolbox makes the full functionality of the NAG Library available through MATLAB. An advantage of calling NAG via MATLAB is that many routine arguments become optional or unnecessary, thus making code easier to read and maintain.

All the NAG Library documentation - in printed form that's seventeen large volumes taking up a full yard of shelf space - has been converted to MATLAB help format, and so it is simple to access via MATLAB's usual documentation facilities. Included in the documentation for each NAG Library routine is example MATLAB code showing how to use the routine.

To show how easy it is to use the new Toolbox, we demonstrate how to call some popular NAG routines, and use MATLAB's plotting facilities to view the results.

Note: The code examples in this article have been extracted from demo scripts, and will not necessarily work properly if cut and pasted from this page into MATLAB. The full version of the scripts, which have been used to make the figures in this article, are available in this archive.

We start with some of the simplest possible NAG calls. Among many other special functions, the S chapter contains the Bessel functions Y0(x), Y1(x), J0(x) and J1(x). Following the usual NAG naming convention, these are in routines s17ac, s17ad, s17ae and s17af respectively.

A common task in many areas of science is to find a function which approximates a set of data points. The data may contain random perturbations, such as measurement errors, which it is desirable to smooth out.

e02dc computes a bicubic spline approximation to a set of data values, given on a rectangular grid in the x-y plane. A single parameter is specified to control the trade-off between closeness of fit and smoothness of fit.

A small value for this smoothing parameter, named s in e02dc, will give a closer fit to the data, but the exact meaning of “small” in this context may vary according to the particular data set. In theory, a value of s = 0 will produce an interpolating spline, though in practise that may cause problems of oscillation around the original data.

NAG routine e04wd is designed to find a minimum point of an arbitrary smooth function, which may be subject to linear and nonlinear constraints, using a sequential quadratic programming (SQP) method - see the e04wd document for more details of the method.

e04wd will be used to find a minimum of the well-known Rosenbrock function, which is a 2D function given by

f(x,y) = 100*(y-x*x)^2 + (1-x)^2

Solving Rosenbrock's function is an unconstrained problem - without hindrance, x and y may take any values at the minimum point.

Because of the “banana shaped valley” contours characteristic of Rosenbrock's function, as shown in the figures below, the function is often used as a test problem for minimization routines like e04wd. Simplistic algorithms such as a basic “steepest descent” method have a hard time following the shallow curving valley floor in their quest for the minimum point, but the NAG routine has no trouble.

A feature of e04wd is that the first derivatives of the function to be minimized are not required to be known analytically. If they are not known, or are hard to determine, e04wd can estimate them using difference approximations. If the user is able to supply them, though, convergence of the algorithm is likely to be faster and more robust.

The function to be evaluated is supplied to e04wd through a MATLAB .m file - here we name the file e04wd_objfun.m. This routine may also evaluate the first derivatives of the objective function if requested to. What if the derivatives are not available? Simply don't compute them; e04wd is smart enough to notice whether we did or not, and will estimate them itself.

Note that as well as the function in e04wd_objfun.m, e04wd also requires a function confun.m to evaluate any nonlinear constraint functions. In this case, we have no constraint functions so we can supply a dummy for this function.

Also note that for clarity, all the MATLAB commands used for plotting the contours and results are omitted.

The figure below shows the path to the minimum of Rosenbrock's function when analytical derivatives are supplied. Every time the objective function is evaluated, the evaluation point is plotted. The starting point is shown by a blue circle, and the target minimum shown by a green circle is at the point (1,1). Notice how the path traced backtracks and corrects itself on the way to the minimum.

Figure 4: Path to the minimum of Rosenbrock's function when derivatives are known

To illustrate how e04wd still works even if derivatives are not supplied, we can replace the function in objfun.m by this code:

Figure 5: Path to the minimum of Rosenbrock's function when derivatives are not supplied

This time the lack of derivative information makes it harder for e04wd. The routine must estimate derivatives using extra evaluations of the objective function. In total, 231 evaluations are now required, as opposed to the 54 evaluations when derivatives were available. In addition, the minimum value obtained is not quite as good, though still a good approximation to the exact solution of 0.0.

The moral of the story? Supply derivatives if you can, but let the NAG routine do it for you if you cannot!

The MATLAB script for this demo is available as the fileNAGToolboxDemos/Minimization/e04wd_demo.m, distributed in this archive.

For more communication

About Us

The Numerical Algorithms Group (NAG) delivers trusted, high quality numerical computing software and high performance computing (HPC) services. For four decades NAG experts have worked closely with world-leading researchers in academia and industry to create powerful, accurate and flexible software which today is relied upon by tens of thousands of users, companies, and learning institutions as well as numerous independent software vendors. ...more