NLLS stands for nonlinear least squares and SQP is sequential quadratic programming. So essentially this is an optimization problem, and everyone knows that NAG Library Chapter E04 is the best place to look for optimization solvers. The appropriate NAG routine in our C Library is nag_opt_nlin_lsq (e04unc).
A few weeks ago one of our users contacted NAG and asked for an example program of using e04unc in Excel. The NAG and Excel page has quite a few examples and guidelines about using NAG Library in Excel, but we didn't have this particular one.
I wrote this example and it is available for download on the Excel page. I encourage readers of this blog to download it and play with it on your own. It wasn't difficult to create, but there was one issue that caused me a nasty headache. Some routines that have callback functions (just as e04unc does) where a vector or matrix is passed to/from a subroutine require usage of Windows API subroutine pRtlMoveMemory.
Since the underlying NAG Library is a C Library, array arguments are simply declared as the appropriate type. VBA passes arguments, by default, by reference (ByRef). Hence we have access to a pointer to the array. In the case of input array arguments, the appropriate amount of storage has to be copied to a VB array before it can be used. At the end of the function, output arrays must be copied back to the pointer. For more information please have a look at our NAG and Excel page.
Well, I guess it all sounds easy. Nevertheless I had some problems with RtlMoveMemory - I couldn't pass the data to and from a callback without some sort of memory violation error or if it didn't crash I kept on getting incorrect results.
The trick is not to use the default declaration of RtlMoveMemory, but actually have two versions of it: one for passing memory to a callback function via a pointer, and the second for copying memory from an array in the callback function back to a pointer. They differ slightly in declarations:
OK, but why is this so important in this example? e04unc has two callback functions.
- objfun, which returns the value of the objective function and its Jacobian.
- confun, which returns the values of constraint functions and their respective Jacobians.
This call means that we focus on the first element of vector f, take a specific amount of memory and make sure that pointer f_rptr will point at this particular vector. A similar approach applies to constraint function and the Jacobians.
This is essentially how RtlMoveMemory Windows API function is used with NAG C Library routines.
I decided not to put the whole VBA code here in order to encourage you, dear reader of this blog, to download the mentioned example and check the code on your own. In order to run it you need the 32-bit Windows implementation of the NAG Library. You can obtain a trial licence key for the Library from firstname.lastname@example.org.
Please let us know if you found the example useful and if you would like us to create example programs for other NAG routines!