NAG – The Ultimate MATLAB Toolbox?

September 23rd, 2008 | Categories: applications, math software, matlab, NAG Library | Tags:


MATLAB is an incredibly powerful piece of software that is used by students and researchers in a wide variety of disciplines. If you have some maths to do then MATLAB can probably help you out – curve fitting, statistics, plotting, signal processing, optimization, linear algebra, symbolic calculus – the list just goes on and on. There is one big problem though – much of this stuff doesn’t actually come with the basic install of MATLAB.

Let’s say that you are a new academic researcher and you have just purchased a copy of MATLAB at a cost of several hundred pounds. At first it seems that it can do everything that you require and so you are happy. One day though, you find yourself needing to find the minimum of a constrained nonlinear multivariable function. After a bit of searching you realise that the function you need is called fmincon but, when you try to use it, you discover that it is part of an add-on called the Optimization Toolbox which costs a couple of hundred pounds. You duly pay for the toolbox and are happy once again.

Some time later in your career you find yourself in need of a good quasi random number generator to help you implement a monte-carlo integration scheme. MATLAB can help you out in the form of the sobolset and haltonset functions but you have to pay yet more money to get access to the Statistics Toolbox.

MATLAB has a toolbox for almost every eventuality:

  • You need to symbolically integrate a function. – buy the Symbolic Toolbox
  • You need to do some advanced curve fitting. – buy the Curve Fitting Toolbox
  • Working with Splines? – buy the Spline Toolbox
  • Financial Mathematics?- buy the Financial Toolbox

You get the idea. I guess Mathworks do this for a good reason – by splitting off these more specialised functions into separate products they can keep the price of the basic version of MATLAB down at a reasonable level. This is a good thing for many users. After all, who wants to pay for a lot of functionality they will never use.

There are problems with this model though. If you are unlucky enough to have a problem that requires several of these specialised functions then MATLAB can be a very expensive solution for you. If happen to be the head of a research group with, say 20 members, and all of them require access to several of these specialised toolboxes then MATLAB can be an extremely expensive option for you.

Finally, if you are someone like me and you need to maintain network licenses for over 50 different MATLAB toolboxes with varying numbers of seats for an entire large university and co-ordinate who pays for what and give recommendations on which toolboxes academics might choose for teaching and research then the whole MATLAB toolbox system can become rather well….tiring. The license related issues alone are enough to give you sleepless nights – trust me on this!

NAG – A new kind of toolbox for MATLAB

Wouldn’t it be nice if there was a MATLAB toolbox that did the work of several other toolboxes which also had an amazingly straightforward licensing system? Well, the Numerical Algorithms Group (NAG) have come up with a contender for such a toolbox – The NAG toolbox for MATLAB.

NAG have got history – they have been around for a long time. They published the first version of their numerical libraries (which they refer to as Mark 1) back in 1971 so they are even older than I am. Mark 1 of the NAG libraries contained 98 different functions and could be had in 2 flavors – ANSI Fortran or Algol 60! Fast forward to 2008 and they are up to Mark 21 of their Fortran library that contains around 1,500 different functions, which is a lot by anyone’s measure. Subjects covered by these functions include optimization, statistics, splines, curve fitting, numerical quadrature, differential equations and a whole lot more. The recent release of the NAG toolbox for MATLAB allows you to call most of these functions directly from MATLAB – not a whiff of Fortran anywhere.

Did you notice anything about the subjects covered by the NAG library in the paragraph above? That’s right – they look just like the names of various MATLAB toolboxes. A very quick survey of the NAG functions on offer suggests to me that you might be able to substitute the NAG toolbox for MATLAB for the following standard MATLAB toolboxes:

  • Statistics toolbox
  • Optimization toolbox
  • Curve fitting toolbox
  • Spline toolbox
  • Partial Differential Equation toolbox

That’s at least 5 toolboxes in one – which is great news and it gets better. If your institution has a site license for the NAG Fortran library then, at the time of writing at least, it will also have a site license for the NAG toolbox for MATLAB. That’s a genuine site license as well – one that you could use to install the toolbox on every machine owned by your institution. No mucking around with concurrent network licenses here – which is one major point in it’s favour from my point of view.

As you might expect, however, there are complications. The NAG toolbox may well offer similar functionality to the MATLAB toolboxes listed above but you cannot use it as a simple drop in replacement for them. For example, if you want to create quasi random numbers using the sobol sequence then the function you would use from the MATLB statistics toolbox is sobolset whereas if you use the NAG toolbox then the function you need is called G05YAF. The exact properties of these two functions may differ as well since they have probably been implemented differently and the two companies have rather different ways of doing things.

Function Naming

While on the subject of function naming – I have to say that I simply don’t like the naming convention used by the NAG toolbox for MATLAB (which is identical to what they use for their Fortran library). Let’s take the function mentioned above as an example – G05YAF. It’s hardly memorable is it? The MATLAB equivalent – sobolset – is much more reasonable in my opinion. Of course your mileage may vary but I have been brought up on systems such as Mathematica, MATLAB and, more recently, Python and in all of these systems the naming conventions used are almost obvious. Let’s take the function for calculating the eigenvalues of a real general matrix as another example:

  • Mathematica: Eigenvalues[]
  • Matlab: eig()
  • NAG toolbox: F02EBF()

Of course the NAG naming convention isn’t completely random even though it may look like it at first sight of the function names above. It’s very well organised in fact and you can quickly get used to it once you understand the system but it definitely looks out of place when put up against its competitors in my opinion.

Functionality Differences

Although I keep comparing the NAG product with those of the Mathworks, it’s probably worth mentioning that NAG have not set out to build a product that directly competes with the MATLAB toolboxes. They are very different beasts and so if you are looking for a one to one correspondence between the NAG toolbox and MATLAB then you are going to be disappointed. NAG were around long before the Mathworks and they have their own particular way of doing things. I’m not saying that one approach is better than another but they are certainly different.

As a particular example of what I mean consider finding the eigenvalues of a matrix again. The approach taken by the Mathworks is to provide you with one function in MATLAB – eig(). The algorithm that MATLAB uses to actually find the eigenvalues depends upon the type of matrix you give it. The system takes a look at your matrix at run-time and does its best to come up with the optimum algorithm to find the solution.

NAG take a rather different approach. They provide lots of individual functions (click here to see them) that can find eigenvalues for various matrix types and they expect the user to choose the most appropriate one.

These differences can make a direct comparison between NAG and MATLAB rather difficult – if it were easy I would have done it by now. In my opinion you should not be thinking “What is the NAG equivalent to the MATLAB function xyz” as there might not be a direct equivalent, instead you should be thinking “I need a function that does abc – which routine in the NAG toolbox might help me?”

If you find that you need to do something that is not contained within the NAG toolbox then you have essentially have three options.

  1. Try to find something in the Mathwork’s set of toolboxes that does what you need and use that instead.
  2. Code the algorithm yourself – or find someone else’s solution on the web.
  3. Email NAG and tell them what you want to do.

I have taken option 3 on behalf of myself and several users at my university and am very pleased to say that NAG have always responded. In some cases they told us exactly which function within the NAG toolbox we needed to use and in other cases they have actually implemented a suitable algorithm for us

There will be several functions in the next release of the NAG library (and hence the NAG toolbox for MATLAB) simply because we asked for them. That’s what I call customer service! Of course I can’t promise that they will do this in all cases but I have personally found them to be very approachable.


I might not like NAG’s naming convention but I seriously like the quality of their routines. If you came to me with two solutions to a problem – one which came from the NAG libraries and one which came from some other source then I would almost always trust the NAG result. Writing numerical code is pretty much the main focus of their business and they have been doing it for over 30 years so they know their onions (as my Australian friend Barrie would say). Take a quick look at the scientific literature on google scholar and you will find thousands of references to the NAG libraries – if thousands of academic feel that they can trust them then so can you.

Another high quality part of the NAG toolbox for MATLAB is the documentation. Every single function has been meticulously documented and contains details of the algorithms involved, references to the original literature and fully working example programs. It integrates well with MATLAB’s standard documentation system and so appears to the user as if it were any other MATLAB toolbox. The only thing that is missing is a set of demo’s that can be run directly from the help system. Every standard Mathwork’s toolbox has a nice set of illustrative demo applications that you can start running with just a few mouse clicks but the NAG toolbox has none. This is a shame isn’t exactly a show-stopper and I am reliably informed that there will be a whole set of demo’s included in the next version.



  • Truly massive set of robust, accurate numerical routines that could potentially negate the need for a whole set of Mathwork’s tooboxes (write me for more details)
  • Superb documentation and first class customer support.
  • Easy license administration for academic institutions.


  • Esoteric naming convention.
  • I couldn’t find a price for an individual who wants to buy the toolbox. NAG’s license model seems to be geared more towards site licenses.

The NAG toolbox for MATLAB is a great piece of software that deserves to be in the toolkit of everyone who is in the business of writing numerical code.

Full disclosure and the usual disclaimers

  • I work for the University of Manchester in the UK but these are my opinions alone and do not necessarily reflect the policy of the University.
  • NAG once bought me lunch but so have the Mathworks so it all evens out nicely.
  • I have never been paid by either company to do anything – I’m just a customer.
  • Comments are welcomed. Even if you disagree with me.

Other articles like this one:

If you enjoyed this article, feel free to click here to subscribe to my RSS Feed.

  1. September 24th, 2008 at 21:43
    Reply | Quote | #1

    Great read! MATLAB GOGOGOGO! Could be a major help to MATLABBERS everywhere! I gotta stumble this article

  2. algo rhythm
    February 5th, 2009 at 18:07
    Reply | Quote | #2

    I would love to know NAG pricing as well, since I am comparison shopping with other numerical libraries. This press release indicates that an individual license may be had for around $2500 – less than half of combined matlab toolbox prices: “Pricing for the NAG Toolbox for MATLAB begins at $2,490 for an individual license, with discounts available to existing NAG license holders. It is available for both Microsoft Windows 32-bit and 64-bit and Linux 32-bit and 64-bit and is compatible with MATLAB versions 2007a, 2007b, and 2008a. ”

  3. Mike Croucher
    February 7th, 2009 at 12:20
    Reply | Quote | #3


    It turned out that I was in a seminar with members of the NAG team when you posted your comment so I asked them to contact you and give you pricing info. You should have heard from them by now..,don’t worry they are not pushy salespeople.


  4. January 19th, 2010 at 11:38
    Reply | Quote | #4

    Alas, NAG’s not available for the OS X platform. Perhaps they could be persuaded to compile for that too…

  5. January 19th, 2010 at 11:45
    Reply | Quote | #5

    Hi Brian

    The NAG library is available for OS X in C and Fortran flavours but no MATLAB toolbox as yet :(
    I get asked so many times about a Mac OS X version of their toolbox that it’s not funny. Hopefully they will come up with the good soon.

    If not then I’ll roll up my sleeves and see if I can call their C library from MATLAB. After all I can do it in Python ;)


  6. February 3rd, 2010 at 04:45
    Reply | Quote | #6

    This certainly looks like it could be of good use for many. I use MATLAB often but have not used NAG.