Вы находитесь на странице: 1из 15

Using Excel, Visual Basic and

NAG’s Numerical Libraries – getting started

Marcin Krzysztofik

NAG Financial Mathematics Day


Manchester Business School
2nd February 2011

Experts in numerical algorithms


and HPC services
Agenda

 Using NAG Library in Excel


 Shall I use NAG C Library or NAG Fortran Library?
 Examples

2
Why NAG & Excel?

 Microsoft Office Excel is the most widely used


spreadsheet application in the world.
 Easy to use, intuitive, practical.
 However it’s not a statistical data analysis package...
 And that’s where NAG being great at serious
numerical calculations comes in!

3
Why you don’t need to be a C/Fortran programmer?

 The main library is provided in Fortran and C.


 Fortran is a programming language especially suited
to scientific computing and numerical analysis, also
very popular in high-performance computing.
 C is one of the most popular programming
languages, know for its speed, flexibility and
readability.
 The underlying programming language for Excel is
VBA, which is relatively easy to learn and use.

4
Why you don’t need to be a C/Fortran programmer?

NAG does all the programming (in Fortran or C),


so all you need to know is
how to call the NAG functions in VBA!

5
NAG C Library vs. NAG Fortran Library
A function can be called either from NAG Fortran
Library or NAG C Library. What are the differences?
 Essentially they are the same;
 NAG provides declare statements for the Fortran
Library and defines C types in VBA;
VB6 FL headers VB6 CL headers
 C and VBA arrays start with 0, while Fortran arrays
with 1;
 C arrays need transposing.

6
The IFAIL parameter
IFAIL in NAG Fortran Library serves two purposes:
 On input it determines what action the Library
routine takes when an error is detected:
 -1 (Soft Fail & Noisy Exit) – error message and continue;
 0 (Hard Fail) – error message and stop;
 1 (Soft Fail & Quiet Exit) – no error message and continue.

 On output it informs what kind of error has occurred,


depending on the function in use (look into the
documentation for details).
 The C Library has an equivalent type NagError
7
How to call a simple function?
 S17AEF (s17aec) – calculates the value of Bessel
function J_0(x);
 Note:
 Declaration statements in VB (C types, no types for
Fortran)
 Calling straight from the spreadsheet
 Calling through a wrapper function
 Error handling

8
How to handle callbacks?
 A callback is a piece of executable code that is passed
as an argument to other code.
 D01BAF (d01bac) – calculates one-dimensional
Gaussian quadrature
 Note:
 Visual Basic AddressOf operator to handle callbacks
 Argument Passing ByRef in Fortran and ByVal in C

9
Debugging
 When writing your own code consider:
 How the variables are declared
 NAG doesn’t accept Variants
 Integers are declared as Long
 How arguments are passed (ByRef/ ByVal)
 What the sizes and dimensions of arrays are
 If the arrays are numbered from 0 or 1
 If arrays need to be transposed
 If IFAIL is set properly

10
How to pass arrays as arguments?
 F03AFF (f03aff) – calculates the determinant of a real
matrix and performs LU factorization.
 Note:
 Reading the array from the worksheet (Range->Double)
 Checking if the matrix is a square matrix
 The result of the factorization is not shown. How can it be
shown?

11
Further applications of NAG Libraries
 Option Pricing:
Option_Pricing_Example.xls
 Portfolio Optimization:
Portfolio_Optimization_Example.xls
 Wavelet Transforms, ODE’s, PDE’s, Numerical
Differentiation, Linear Algebra, Matrix Operations,
Statistical Analysis and many others...

12
Conclusions

 Calling NAG routines from Excel is very simple due to


very extensive but concise documentation files;

 Using NAG Libraries does not require knowledge of


Fortran or C;

 NAG & Excel together allow for a lot of flexibility and


individual approach concerning the GUI.

13
References

 NAG & Excel webpage


http://www.nag.co.uk/numeric/nagandexcel.asp

 Product documentation
http://www.nag.co.uk/support_documentation.asp

14
Questions?

 www.nag.co.uk
 support@nag.co.uk
 Marcin.Krzysztofik@nag.co.uk

Thank you for your attention!

15

Вам также может понравиться