Using the NAG Toolbox for MATLAB Part 2

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, which makes code easier to read and maintain. In addition, all of the NAG Library documentation (seventeen large volumes taking up a substantial amount of shelf space) has been converted to MATLAB help format, and so it is simple to access via MATLAB's usual documentation facilities. The documentation for each NAG Library routine includes example MATLAB code showing how to use the routine.

We have already highlighted some of the contents of the NAG Toolbox in our article here, we continue our exploration of the Toolbox, demonstrating how to call some of the other NAG routines, using 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.

The C05 chapter - Finding the root of an equation

We start with the calculation of the zeros of a continuous function. The C05 chapter contains several routines for doing this (as well as other routines which solve a set of nonlinear equations in several unknowns). In our first example, we solve a quadratic equation.

  25x^2 - 10x + 1 = 0

Of course, it is trivial to determine analytically that both roots are at x = 0.2 (which provides a handy check on our numerical result - see below) but the C05 routines can be used to find the zeros of any function, as long as it is continuous. Here, we use routine c05ax; this evaluates the function via reverse communication, which allows us to display the intermediate results on the way to finding the root.

Here's the code, including some plotting commands:

   
% Specify the function as a MATLAB anonymous function - this facilitates
% changing the function for other examples.
myfun = @(x)25*x*x - 10*x + 1;

% Specify the initial estimate of the zero, and the value of the function
% there (the latter is only needed for plotting).
xstart = 1.0;
fstart = myfun(xstart);
xx = xstart;
fx = fstart;

% Specify other parameters for the NAG routine. tol is the tolerance,
% used by the routine to control the accuracy of the solution.
tol = 0.00001;
ir = int32(0);
c = zeros(26, 1);
ind = int32(1);

% Iterate until the solution is reached.
while (ind ~= int32(0))

% Call the NAG routine to get an improved estimate of the zero,
% then plot it.
[xx, c, ind, ifail] = c05ax(xx, fx, tol, ir, c, ind);
fx = myfun(xx);

plot(axes, xx, fx, 'or', 'MarkerFaceColor', [1,0,0], 'MarkerSize', 8);

end

% Plot initial estimate, and final solution.
plot(axes, xstart, fstart, 'og', 'MarkerFaceColor', [0,1,0], 'MarkerSize', 8);
plot(axes, xx, fx, 'oy', 'MarkerFaceColor', [1,1,0], 'MarkerSize', 8);

and the results are shown in this picture:

Figure 1: Finding the root of a quadratic.

As a second example, we can use the same code to find the root of a different function (for example, one that is harder to determine analytically) by changing the first few lines of the code above to:

   
% Specify the function as a MATLAB anonymous function - this facilitates
% changing the function for other examples.
myfun = @(x)x - exp(-x);

% Specify the initial estimate of the zero, and the value of the function
% there (the latter is only needed for plotting).
xstart = 5.0;

giving the results shown in Figure 2.


Figure 2: Finding the root of a transcendental function.

The MATLAB script for this demo is available as the file NAGToolboxDemos/Root_finding/c05ax_demo.m, distributed in this archive.