清华大学 nag matlab 工具箱培训

91
Experts in numerical algorithms and HPC services NAG for MATLAB Toolbox Training Course 刘泰兴 Ted Liu [email protected] Technical Manager Numerical Algorithms Group Greater China Office 英商纳格资讯 大中华区分公司 http://cn.nag-gc.com

Upload: ted-liu

Post on 10-Mar-2016

386 views

Category:

Documents


3 download

DESCRIPTION

2012 清华大学 NAG MATLAB 工具箱培训

TRANSCRIPT

Page 1: 清华大学 NAG MATLAB 工具箱培训

Experts in numerical algorithms and HPC services

NAG for MATLAB Toolbox Training Course

刘泰兴 Ted Liu [email protected]

Technical Manager Numerical Algorithms Group

Greater China Office

英商纳格资讯 大中华区分公司 http://cn.nag-gc.com

Page 2: 清华大学 NAG MATLAB 工具箱培训

2

Agenda

NAG Software Introduction Problems in numerical computation

How to use the NAG Toolbox for MATLAB

Functionality and Performance Comparison

Workshop and hands on sessions, exercises, Q&A

Page 3: 清华大学 NAG MATLAB 工具箱培训

3

Numerical Algorithms Group

1970 • Birmingham, Leeds,

Manchester, Nottingham & Oxford

1971 • First Library released • Algol 60 & Fortran

1973 • Numerical Algorithms

Group

1978 • NAG US Office

1990 • NAG C Library

1996 • NAG Japan Office

2009 • NAG Greater China

Office

2010 • NAG Library for .NET

2011 • NAG Fortran Mark 23

Page 4: 清华大学 NAG MATLAB 工具箱培训

4

Our Products

Numerical Libraries

Fortran 77/90

C/C++

Multi-Core (OpenMP)

MPI

.NET

Connector Products

MATLAB

Excel (VBA)

R

Python, LabVIEW, etc.

HPC Services

UK HECToR Service

Oil Company HPC Service

Consultancy Services

Application Development

Algorithms Development

Page 5: 清华大学 NAG MATLAB 工具箱培训

5

NAG Libraries Play Key Role of Application

Page 6: 清华大学 NAG MATLAB 工具箱培训

6

NAG Library Contents

Root Finding Summation of Series Quadrature Ordinary Differential Equations Partial Differential Equations Numerical Differentiation Integral Equations Mesh Generation Interpolation Curve and Surface Fitting Optimization Approximations of Special

Functions

Dense Linear Algebra Sparse Linear Algebra Correlation & Regression

Analysis Multivariate Methods Analysis of Variance Random Number Generators Univariate Estimation Nonparametric Statistics Contingency Table Analysis Smoothing in Statistics Survival Analysis Time Series Analysis Operations Research …

Page 7: 清华大学 NAG MATLAB 工具箱培训

7

Some Cases Studies…

Morningstar uses NAG Library routines to assist portfolio and optimization Smartleaf offers daily tune-ups on personalized portfolios with the help of NAG

routines Share Dimension used for theater capacity planning by running linear regressions ZS Associates use NAG Library for data analysis Institute for Transport Studies used NAG Library to underpin advanced Transport

Planning and Optimization QinetiQ GRC use NAG for marine design Powergen optimizes power plant University of Graz used NAG Library in Spintronics of Magnetic Nanostructures WCI streamlines the steel production The Chicken or the Egg?

Page 8: 清华大学 NAG MATLAB 工具箱培训

8

Why Use NAG Libraries and Toolbox?

Global reputation for quality accuracy, reliability and robustness…

Extensively tested, supported and maintained code Reduce development time

Concentrate on your key areas Components

Fit into your environment Simple interfaces to your favorite packages

Regular performance improvements! For new hardware and software

Page 9: 清华大学 NAG MATLAB 工具箱培训

9

Why Use NAG Toolbox for MATLAB?

Prototype Using MATLAB, Python, R, etc.

Production C/C++, .NET, Java,

Fortran, etc. Solution

Consistent results between prototype and production applications!!!

WHY?

Page 10: 清华大学 NAG MATLAB 工具箱培训

10

PROBLEMS IN

NUMERICAL COMPUTATION

Page 11: 清华大学 NAG MATLAB 工具箱培训

11

Why Bother?

Numerical computation is difficult to do accurately 11.11 - 11 (11.11 - 11) * 100 vs. (11.11 * 100) - (11 * 100) 10* (.5 - .4 - .1) >> (5. - 4. - 1.)

Problems of Overflow / underflow

How does the computation behave for large / small numbers?

𝑎2 + 𝑏2 = 𝑐2

𝑎

𝑏 𝑐 = 𝑠 (𝑎𝑠)2+(

𝑏𝑠)2

Where s = a + b

a=1.0; b=2.0; c=sqrt(a^2+b^2) a=1.e200; b=2.e200; c=sqrt(a^2+b^2)

a=1.e200; b=2.e200; s=a+b; c=s*sqrt((a/s)^2+(b/s)^2)

Pythagorean theorem

Page 12: 清华大学 NAG MATLAB 工具箱培训

12

Why Bother?

Condition How is it affected by small changes in the input?

Stability How sensitive is the computation to rounding errors?

Importance of … error analysis information about error bounds on solution

𝟗𝟗.𝟎𝟎 98100 99

𝑥1𝑥2 = 197

199

𝟗𝟖.𝟗𝟗 98100 99

𝑥1𝑥2 = 197

199

a=[99.00,98;100,99]; b=[197;199]; a\b

a=[98.99,98;100,99]; b=[197;199]; a\b

Page 13: 清华大学 NAG MATLAB 工具箱培训

13

For a collection of observations the mean is defined as and the variance as

An Example: Sample Variance

}...1,{ nixi =

2

1

2 )(1

1 xxn

sn

ii −−

= ∑=

∑=

=n

iix

nx

1

1

Page 14: 清华大学 NAG MATLAB 工具箱培训

14

Example Calculation

x

2s

x

2sc

cx

2s

x

x=

=

+

Page 15: 清华大学 NAG MATLAB 工具箱培训

15

Example Calculation

xc 1+c1−c

Page 16: 清华大学 NAG MATLAB 工具箱培训

16

Example Calculation

For this collection of observations

the mean is

and the variance is 1)10)1((2

1 222 =++−=s

ccccx =+++−= )( 1131

},,{ 11 +− ccc

Page 17: 清华大学 NAG MATLAB 工具箱培训

17

Use Excel’s Function 1/5

c: 1

c - 1: 0

c + 1: 2

mean(c -1, c, c + 1): 1

variance(c -1, c, c + 1): 1

sumsq(c-1-c, c+1-c)/2: 1

Page 18: 清华大学 NAG MATLAB 工具箱培训

18

Use Excel’s Function 2/5

c: 91,999

c - 1: 91,998

c + 1: 92,000

mean(c -1, c, c + 1): 91,999

variance(c -1, c, c + 1): 1

sumsq(c-1-c, c+1-c)/2: 1

Page 19: 清华大学 NAG MATLAB 工具箱培训

19

Use Excel’s Function 3/5

c: 9,113,129,999

c - 1: 9,113,129,998

c + 1: 9,113,130,000

mean(c -1, c, c + 1): 9,113,129,999

variance(c -1, c, c + 1): 1

sumsq(c-1-c, c+1-c)/2: 1

Page 20: 清华大学 NAG MATLAB 工具箱培训

20

Use Excel’s Function 4/5

c: 111,312,911,119,991

c - 1: 111,312,911,119,990

c + 1: 111,312,911,119,992

mean(c -1, c, c + 1): 111,312,911,119,991

variance(c -1, c, c + 1): 1

sumsq(c-1-c, c+1-c)/2: 1

Page 21: 清华大学 NAG MATLAB 工具箱培训

21

Use Excel’s Function 5/5

c: 111,312,911,119,996

c - 1: 111,312,911,119,995

c + 1: 111,312,911,119,997

mean(c -1, c, c + 1): 111,312,911,119,996

variance(c -1, c, c + 1): 1

sumsq(c-1-c, c+1-c)/2: 1

?

Page 22: 清华大学 NAG MATLAB 工具箱培训

22

What’s Gone Wrong?

Instead of

Before Excel 2010 uses an (analytically identical) formula faster to calculate (one pass) accuracy problems if variance is small compared to x

2

1

2

11 )( xx

ns

n

ii −−

= ∑=

−= ∑∑

==

2

11

22 11

1 n

ii

n

ii x

nx

ns

How fast do you want the wrong answer?

Page 23: 清华大学 NAG MATLAB 工具箱培训

23

NAG TOOLBOX FOR

MATLAB

Page 24: 清华大学 NAG MATLAB 工具箱培训

24

Structure of the Toolbox

Contains essentially all NAG functionality, not a subset Available for Windows and Linux (32/64-bit) and Mac OS

X 10.5 (Intel 64 bit) Installed under the usual MATLAB toolbox directory Makes use of a DLL or shared version of the NAG Library You do not need to have preinstalled the library

The latest (Windows) version exploits multicore!

Page 25: 清华大学 NAG MATLAB 工具箱培训

25

Structure of the Toolbox

Comprehensive interfaces to NAG Fortran Library Fully integrated into MATLAB Many routine parameters

become optional Easier to read code Complete documentation for

each routine including examples

Complementary functionality to MATLAB

An alternative to several specialist toolboxes

MATLAB Toolbox

Optimization

Curve Fitting

Spline

Statistics Partial

Differential Equation

Financial

Wavelet

Page 26: 清华大学 NAG MATLAB 工具箱培训

26

Structure of the Toolbox

Documentation as any NAG Library: informative introduction to each chapter and routine document All documentation is available through the MATLAB help

system There are also some demos included

Page 27: 清华大学 NAG MATLAB 工具箱培训

27

Page 28: 清华大学 NAG MATLAB 工具箱培训

28

Structure of the Toolbox

The Toolbox has the same structure as the NAG Library, divided into chapters, each devoted to a branch of mathematics or statistics Each has a 3-character name and a title, e.g., F03 –Determinants

Exceptionally, Chapters H and S have one-character names All documented routines in the Toolbox have five-character

names, beginning with the characters of the chapter name e.g., d01aj

Note that the second and third characters are digits, not letters e.g., 0 is the digit zero, not the letter O

Page 29: 清华大学 NAG MATLAB 工具箱培训

29

THE NAG PRODUCTS

Page 30: 清华大学 NAG MATLAB 工具箱培训

30

NAG Product Code

MBW3222DDL

Product Code MB: MATLAB toolbox

System Code W32: Windows x86 32 bit

Mark Number

Minor Number DAL, DBL, …

Page 31: 清华大学 NAG MATLAB 工具箱培训

31

How to Get NAG Toolbox?

http://cn.nag-gc.com/downloads/mbdownloads.asp Environment MATLAB 2007

and 2008 MATLAB 2009a MATLAB 2009b and 2010a

MATLAB 2010b and 2011a/b

Microsoft Windows

64-bit MBW6I22DCL MBW6I22DKL MBW6A22DJL MBW6A22DJL

Microsoft Windows

32-bit MBW3222DCL MBW3222DCL MBW3222DDL MBW3222DDL

Page 32: 清华大学 NAG MATLAB 工具箱培训

32

How to Install NAG Toolbox on Windows?

You need to have MATLAB installed

Run the setup_mbw3222ddl.exe to install

By default, NAG Toolbox is installed within the user's MATLAB installation The help files will be placed in the \help\toolbox\NAG the executable files will be placed in \toolbox\NAG\mex.w32

If you choose to have your MATLAB path updated during

installation, the file \toolbox\local\pathdef.m will be modified

Product Code

Page 33: 清华大学 NAG MATLAB 工具箱培训

33

How to Install NAG Toolbox on Windows?

The GUI license key request and key installation program Start|All Programs|NAG|Toolbox for MATLAB Mark 22| Request

or Install License Key for MBW3222DDL

Enter the License Key

Page 34: 清华大学 NAG MATLAB 工具箱培训

34

GETTING START USING

MATLAB 2011B 32-BIT

Page 35: 清华大学 NAG MATLAB 工具箱培训

35

Multi-Threaded Functions

A number of functions in the toolbox have been enhanced to exploit multiple cores

This should deliver speed-ups for moderate or large datasets over the single-threaded versions when run on a multicore system

Performance improvements depend on many factors, including: Nature of algorithm Problem size The characteristics of the computer Some INTEL processors support hyper-threading

maxNumCompThreads

Control maximum number of computational threads

Page 36: 清华大学 NAG MATLAB 工具箱培训

36

A Simple Example

Here is an example of how to use the NAG Library to compute the solution of a real system of linear equations, Ax = b, where A is an n by n matrix and x and b are n vectors

a = [ 1.80, 2.88, 2.05, -0.89; 5.25, -2.95, -0.95, -3.80; 1.58, -2.69, -2.90, -1.04; -1.11, -0.66, -0.59, 0.80]; b = [ 9.52;24.35;0.77;-6.22];

Page 37: 清华大学 NAG MATLAB 工具箱培训

37

A Simple Example

And we call like this: Here the NAG routine f07aa takes two parameters,

the matrix of coefficients, A and the vector representing the right-hand side, b

>> [aOut, ipiv, bOut, info] = f07aa(a, b); >> bOut bOut = 1.0000 -1.0000 3.0000 -5.0000

Page 38: 清华大学 NAG MATLAB 工具箱培训

38

Parameters

Hang on! The routine more then two parameters!

Some parameters have been omitted as they can always be determined at runtime: Dimensions of arrays Workspace whose size depends on the problem data Parameters whose value depends entirely on that of other input data

Others are optional ...

Syntax: [a, ipiv, b, info] = f07aa(a, b, 'n', n, 'nrhs_p', nrhs_p)

Page 39: 清华大学 NAG MATLAB 工具箱培训

39

Optional Parameters

Optional parameters are provided after all compulsory parameters Optional parameters appear in pairs:

A string representing the name followed by the value The pairs can be provided in any order

There are optional parameters where: A sensible default value exists which applies to many problems The parameter only applies to some cases The value of the parameter can normally be determined from

that of other parameters at runtime

Page 40: 清华大学 NAG MATLAB 工具箱培训

40

Optional Parameters

For example, in the system of equations given in the previous example, it is obvious that the size of the matrix A, n, is 4 However we can tell MATLAB that n is 3, in which case it

will solve the system represented by the top-left 3x3 section of A, and the first three elements of b And we call like this …

Page 41: 清华大学 NAG MATLAB 工具箱培训

41

Optional Parameters

The last element of bOut can (should) be ignored Since b was a 4x1 matrix on input, it will be a 4x1 matrix

on output, even though the last element is not being used

>> [aOut, ipiv, bOut, info] = f07aa(a, b, 'n', int32(3)); >> bOut bOut = 4.1631 -2.1249 3.9737 -6.2200

Page 42: 清华大学 NAG MATLAB 工具箱培训

42

Parameters

A similar outcome can be achieved by: Here bOut is of appropriate size

>> [aOut, ipiv, bOut, info] = f07aa(a(1:3,1:3), b(1:3)); >> bOut bOut = 4.1631 -2.1249 3.9737

Page 43: 清华大学 NAG MATLAB 工具箱培训

43

Creating Variables of Correct Type

The interfaces to NAG routines in this toolbox are quite precise about the types of their parameters int32(1) - to create an a 4-byte integer, value 1 int64(1) - to create an a 8-byte integer, value 1 complex(1,1) - to create 1.0000 + 1.0000i logical(0) - to create a logical that is .FALSE.

If an object of the incorrect type is provided then a NAG:typeError will be thrown:

MATLAB sparse arrays not supported, B = full(A)

>> [aOut, ipiv, bOut, info] = f07aa(a, b, 'n', 3.0); Error using f07aa Parameter number 4 is not a scalar of class int32.

Page 44: 清华大学 NAG MATLAB 工具箱培训

44

Providing M-Files as Parameters

Many NAG routines expect the user to provide an M-File to evaluate a function, which might represent an integrand, the objective function in an optimization problem etc. Here is an example showing how to solve an integral:

d01ah(0, 1, 1e-5, 'd01ah_f', int32(0)) ans= 3.1416

Page 45: 清华大学 NAG MATLAB 工具箱培训

45

Providing M-Files as Parameters

The integrand is contained in the file 'd01ah_f.m', which looks like: Anonymous functions are not supported

function [result] = d01ah_f(x) result = 4.0/(1.0+x^2);

Page 46: 清华大学 NAG MATLAB 工具箱培训

46

User Workspace

Many routines have parameters for user workspace, which are passed unchanged to user-supplied functions or subroutines This allows data to be passed to these subprograms in a

thread-safe way Typically there are two or three such parameters, an

integer and real array and, sometimes, a character array In the MATLAB toolbox these are replaced by a single

MATLAB object, to provide more flexibility

Page 47: 清华大学 NAG MATLAB 工具箱培训

47

User Workspace Parameters

For example in the following case “user” is a cell array containing three pieces of data: The data is then passed on to e04fy_lsfun1.m

x = [0.5; 1; 1.5]; y = [0.14,0.18,0.22, < snip > 2.10,4.39]; t = [[1.0, 15.0, 1.0], < snip > [15.0, 1.0, 1.0]]; user = {y, t, 3}; [xOut, fsumsq] = e04fy(int32(15), 'e04fy_lsfun1', ... x, 'iuser', user);

Page 48: 清华大学 NAG MATLAB 工具箱培训

48

FUNCTIONALITY AND

PERFORMANCE

Page 49: 清华大学 NAG MATLAB 工具箱培训

49

First Step

Check the version of NAG Toolbox a00aa

Check the availability of a valid license key a00ac

Page 50: 清华大学 NAG MATLAB 工具箱培训

50

Comparisons

In this section we will look at some of the functionality of the NAG Library We will offer some comparisons of the Toolbox compared

with (mainly) core MATLAB

Page 51: 清华大学 NAG MATLAB 工具箱培训

51

Comparisons

Random Numbers Interpolation Eigen Value Curve Fitting Optimization

Page 52: 清华大学 NAG MATLAB 工具箱培训

52

Random Numbers – Chapter G05

Random numbers – used to model real-life processes Humans are bad at choosing them

e.g. faking a random sequence of coin tosses is very difficult: HTHHTHTTHTHHTTTHTTHHTTHTHHTHTH …

Most people would choose a sequence easily proved not to be random

Therefore good algorithms are required

Page 53: 清华大学 NAG MATLAB 工具箱培训

53

Random Numbers – Chapter G05

Pseudo-random numbers: Generated systematically Statistical properties are close to true random numbers Negligible correlation between consecutive numbers

Quasi-random numbers: Not statistically independent Give more even distribution in space (“looks more random”) Useful for Monte Carlo integration G05 has the Sobol, Faure and Neiderreiter generators

Page 54: 清华大学 NAG MATLAB 工具箱培训

54

Pseudo-versus Quasi-Random Numbers

Core MATLAB only has pseudo random numbers Quasi-Random Numbers are only available in the

Statistics Toolbox This is the difference:

Page 55: 清华大学 NAG MATLAB 工具箱培训

55

Interpolation 1/2 x = [0;0.2;0.4;0.6;0.75;0.9;1]; y = [1;1.22;1.49;1.82;2.11;2.45;2.71]; plot (x, y, ‘o’)

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 11

1.2

1.4

1.6

1.8

2

2.2

2.4

2.6

2.8

Insert 100,000,000 Points t = 0 : 0.000005 : 1;

Page 56: 清华大学 NAG MATLAB 工具箱培训

56

Interpolation 2/2

MATLAB’s Interp1 versus NAG’s e01be N = 100000000; loops=1; x = [0;0.2;0.4;0.6;0.75;0.9;1]; y = [1;1.22;1.49;1.82;2.11;2.45;2.71]; t=0 : 1/N : 1; matlabtimes = zeros(1, loops); for i=1:loops tic y1 = interp1 (x, y, t, 'pchip'); matlabtimes(i) = toc; end mean(matlabtimes)

N MATLAB Memory (G) NAG Memory (G) MATLAB/ NAG

100,000,000 9.05 5.0 0.71 1.8 12.75

120,000,000 10.77 6.0 0.82 2.2 13.13

150,000,000 72.85 7.4 1.06 2.6 69.67

200,000,000 892 9.7 1.33 3.5 674.44

250,000,000 2666 12.1 1.76 4.2 1514.77

nagtimes = zeros(1, loops); for i=1:loops tic [d, ifail] = e01be (x, y); [y2, ifail] = e01bf (x, y, d, t); nagtimes(i) = toc; end mean(nagtimes)

My Intel Core i5 / 8G RAM

Page 57: 清华大学 NAG MATLAB 工具箱培训

57

Numerical Linear Algebra

The dense linear algebra in MATLAB built on LAPACK However, you can only get part of LAPACK The NAG Library implements the whole of LAPACK For example, you want to compute a subset of

eigenvalues? We expect the performance to the same, don’t we? Let us look at solving equations ...

Page 58: 清华大学 NAG MATLAB 工具箱培训

58

Linear Systems – Chapter F07

MATLAB uses “\” (backslash) This examines the properties of the matrix and calls an

appropriate routine Symmetric, positive definite, or…

For general matrices that is DGESV in LAPACK f07aa also implements this

We take twice the time backslash does!

Page 59: 清华大学 NAG MATLAB 工具箱培训

59

Eigenvalues and Vectors – Chapter F08

We compare: The MATLAB function eig

[v,d] = eig(a); f08fa (DSYEV in LAPACK). QR algorithm like eig

[a, w, info] = f08fa('V', 'U', a) f08fc (DSYEVD), divide and conquer algorithm

[a, w, info] = f08fc('V', 'U', a)

Here are the results on my Intel Core i5 @ 2.3GHz laptop ...

Page 60: 清华大学 NAG MATLAB 工具箱培训

60

Eigenvalues and Eigenvectors

0

20

40

60

80

100

120

140

160

1000 1500 2000 2500 3000 3500 4000 4500 5000

Tim

e (S

ec.)

Matrix Size

eigf08faf08fc

Page 61: 清华大学 NAG MATLAB 工具箱培训

61

Subset of Eigenvalues and Eigenvectors

What about a subset of eigenvalues? Not possible with eig However, we can use the “expert” drivers in LAPACK via

the Toolbox For example

Specify either a range of values or a range of indices for the desired eigenvalues

[a, m, w, z, isuppz, info] = ... f08fd(jobz, range, uplo, a, vl, vu, il, iu, abstol, 'n', n)

Page 62: 清华大学 NAG MATLAB 工具箱培训

62

NAG Curve Fitting 1/2

Finding a function which approximates a set of data points

0 1 2 3 4 5 6 7 8-1

0

1

2

3

4

5

6

7

8Using NAG routines e02be, e02bb to fit a cubic spline

Smoothness = 0.001

Number of knots = 8

0 1 2 3 4 5 6 7 8-1

0

1

2

3

4

5

6

7

8Using NAG routines e02be, e02bb to fit a cubic spline

Page 63: 清华大学 NAG MATLAB 工具箱培训

63

NAG Curve Fitting 2/2

MATLAB’s polyfit versus NAG’s e02ac N=10000000; loops=1; x = (0: 1000./N: 1000)'; y = 2.011*x.*x.*x + 123*x.*x + 0.123*x + 0.1234; matlabtimes = zeros(1,loops); for n=1:loops tic p=polyfit(x,y,3); matlabtimes(n) = toc; end mean(matlabtimes) N MATLAB Memory (G) NAG Memory (G) MATLAB/

NAG

10,000,000 1.22 1.1 0.32 0.5 3.81

20,000,000 2.44 1.9 1.11 0.7 2.20

50,000,000 5.79 4.2 3.07 1.2 1.89

100,000,000 321 8.1 4.33 1.9 74.13

150,000,000 696 8.9 5.56 2.7 125.18

200,000,000 NA NA 6.18 3.5

nagtimes = zeros(1,loops); for n=1:loops tic [a, ref] = e02ac(x, y, int32(4)); nagtimes(n) = toc; end mean(nagtimes)

My Intel Core i5 / 8G RAM

Page 64: 清华大学 NAG MATLAB 工具箱培训

64

NAG Curve Fitting -Chapter E02

The curve fitting chapter includes: Allows different fits, including L1(minimizes sum of errors) and

mini-max (minimizes the maximum error) Curve Fitting - polynomials and cubic splines Surface Fitting - bivariate polynomials and bicubic splines General Linear and Nonlinear Fitting Functions Padé Approximants Constrained Problems Some allowing “weighting” of data

Let’s look at the bicubic spline demo

Page 65: 清华大学 NAG MATLAB 工具箱培训

65

Deployment Matlab Applications

The deployment generate wrapper code to allow your MATLAB application to run as a stand-alone executable The application remains in MATLAB code

For Curve Fitting example (N=10,000,000), it still 1.3238 vs. 0.5479

Page 66: 清华大学 NAG MATLAB 工具箱培训

66

NAG Optimization

Optimization

E04 Local

optimization

E05 Global

optimization

S Integer

programming

C05 Roots of

Transcendental Equations

Page 67: 清华大学 NAG MATLAB 工具箱培训

67

NAG Optimization – Local Optimization

Variable Single variable f(x) Multiple variable f(x)

Objective Function Linear Quadratic Sum of squares Nonlinear

Constrain None Simple bounds on variables lb <= x <= ub Linear Nonlinear

It is important to choose a method appropriate to your problem type, for efficiency and the best chance of success

Page 68: 清华大学 NAG MATLAB 工具箱培训

68

Best Advice – Use the Decision Trees

Page 69: 清华大学 NAG MATLAB 工具箱培训

69

Linearly Constrained Optimization

Page 70: 清华大学 NAG MATLAB 工具箱培训

70

Optimization - Nonlinear Constraints

Page 71: 清华大学 NAG MATLAB 工具箱培训

71

Solve 2D Rosenbrock unconstrained function

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

Minimize is f(1,1) = 0

Using MATLAB fminsearch function, the result is x = 1.0000e+000

y = 1.0000e+000

fval = 8.1777e-010, (near 0.0000)

Choose suitable algorithm How about NAG’s e04uc routine…

NAG Optimization Routine

Page 72: 清华大学 NAG MATLAB 工具箱培训

72

Case Study - e04uc and fmincon

A problem from a customer from a European bank The problem involves 48 variables and has 9 linear constraints (No

nonlinear constraints) No derivatives supplied

MATLAB’s fmincon required 1890 evaluations of the objective

function and tool 87.6 seconds

NAG’s e04uc (uses a SQP sequential quadratic programming method) required only 1129 evaluations and took 49.4 seconds

Page 73: 清华大学 NAG MATLAB 工具箱培训

73

Case Study - c05nb and fsolve

Nonlinear equation F(X) = 0 F(1)=exp(-x(1)) + sinh(2*x(2)) + tanh(2*x(3)) – 5.01 F(2)=exp(2*x(1)) + sinh(-x(2) ) + tanh(2*x(3) ) – 5.85 F(3)=exp(2*x(1)) + sinh(2*x(2) ) + tanh(-x(3) ) – 8.88

function F=fsolve_obj_MATLAB(x) F=zeros(1,3); F(1)=exp(-x(1)) + sinh(2*x(2)) + tanh(2*x(3)) - 5.01; F(2)=exp(2*x(1)) + sinh(-x(2) ) + tanh(2*x(3) ) - 5.85; F(3)=exp(2*x(1)) + sinh(2*x(2) ) + tanh(-x(3) ) - 8.88; end

options=optimset('Display','off'); startX=[0 0 0]; tic for i =0 : 1000 X = fsolve(@fsolve_obj_MATLAB, startX, options); end toc

function [F,iflag]=fsolve_obj_NAG(n,x,iflag) F=zeros(1,3); F(1)=exp(-x(1)) + sinh(2*x(2)) + tanh(2*x(3)) - 5.01; F(2)=exp(2*x(1)) + sinh(-x(2) ) + tanh(2*x(3) ) - 5.85; F(3)=exp(2*x(1)) + sinh(2*x(2) ) + tanh(-x(3) ) - 8.88; end

startX=[0 0 0]; tic for i =0 : 1000 X = c05nb('fsolve_obj_NAG', startX); end toc

X = 0.9001 1.0002 1.0945

5.82 seconds vs. 0.52 seconds

11 times

Page 74: 清华大学 NAG MATLAB 工具箱培训

74

Case Study - e04fy and lsqcurvefit

Nonlinear curve fitting xdata = [0.9 1.5 13.8 19.8 24.1 28.2 35.2 60.3 74.6 81.3]; ydata = [455.2 428.6 124.1 67.3 43.2 28.1 13.1 -0.4 -1.3 -1.5]; x0 = [100; -1]; % Starting guess

function F = lsq(x, xdata) F = x(1)*exp(x(2)*xdata);

tic [x, fsumsq] = lsqcurvefit(@lsq, x0, xdata, ydata) toc

function [fvec, user] = lsq_nag(m, n, x, user) f = x(1)*exp(x(2)*user{1}); fvec = f - user{2};

user={xdata,ydata}; tic; [x, fsumsq, ~, ~] = e04fy(int32(10), 'lsq_nag', x0, 'user', user) toc

0.075083 seconds vs. 0.009630 seconds

The value of the sum of squared residual 9.504886892390886 vs. 9.504886892366862

7.8 times

Page 75: 清华大学 NAG MATLAB 工具箱培训

75

NAG Optimization – Global Optimization

Global optimization involves finding the absolute maximum or minimum value of a function

Multi-level Coordinate Search

Searches for a global minimizer using branching to recursively split the search space in a nonuniform manner

Divides, or splits, the root box of the search into smaller sub-boxes

Particle Swarm Optimization (Mark 23) Search for a global minimum using a variant of the particle swarm

heuristic

Page 76: 清华大学 NAG MATLAB 工具箱培训

The search space is split into sub-boxes. Eventually

local searches are performed.

7

Page 77: 清华大学 NAG MATLAB 工具箱培训

The branching-style algorithm keeps track of

boxes that contain the best minimum so far, and others that are yet to be explored

fully

7

Page 78: 清华大学 NAG MATLAB 工具箱培训

A global minimum is found in just 158 function

evaluations

7

Page 79: 清华大学 NAG MATLAB 工具箱培训

79

Conclusions

Covers many area of mathematics and statistics, and all this functionality is available from within MATLAB Performance is sometimes achieved by providing a choice

of algorithms for a particular problem Documentation is provided to guide you in selecting the

correct algorithm Example programs help you become familiar with a

routine quickly Information about error bounds of computed solutions

are provided to help interpret your results

Page 80: 清华大学 NAG MATLAB 工具箱培训

80

APPENDIX

Page 81: 清华大学 NAG MATLAB 工具箱培训

81

Errors and Warnings

The NAG routines can throw a number of errors: NAG:licenceError - A valid license couldn’t be found NAG:arrayBoundError - Array provided is too small NAG:callBackError - An error occurred when executing an M-File

passed as a parameter to the routine NAG:missingInputParameters NAG:optionalParameterError - Not in name/value pairs, or the name

provided is not an optional parameter NAG:tooManyOutputParameters NAG:typeError - A parameter is of the wrong type NAG:valueError - An incorrect value has been provided for a

parameter

Page 82: 清华大学 NAG MATLAB 工具箱培训

82

Errors and Warnings

In most cases the error message will give more precise details of how the error was triggered For example a NAG:arrayBoundError might display the

message: ??? The dimension of parameter 2 (A)

should be at least 4

Page 83: 清华大学 NAG MATLAB 工具箱培训

83

Errors and Warnings

The NAG routines can throw two warnings: NAG:truncationWarning -A string was truncated when copying

cell array of strings to a Fortran data structure NAG:warning - The NAG routine returned an error or warning

The latter is important, and means that on exit the value of the parameter ifail (or, in chapters f07 and f08, info) was non-zero on exit

Page 84: 清华大学 NAG MATLAB 工具箱培训

84

Errors and Warnings

If the user does not wish to see a warning then they can disable it in the usual way, for example: In this case it is vital that the user checks the value of

ifailor info on exit from the routine

warning('off', 'NAG:warning')

Page 85: 清华大学 NAG MATLAB 工具箱培训

85

EXERCISES SHORT QUESTIONS

Page 86: 清华大学 NAG MATLAB 工具箱培训

86

Exercise 1

Using NAG routine X01AB write a MATLAB function that returns the value of Euler’s Constant

Page 87: 清华大学 NAG MATLAB 工具箱培训

87

Exercise 2

S15AB returns the value of the cumulative Normal distribution function Use this to write a MATLAB function that returns the

value of the cumulative Normal distribution at the point x

Page 88: 清华大学 NAG MATLAB 工具箱培训

88

Exercise 3

Use D01AH to find the area under the sine curve between 0 and pi Using the documentation look what other information

can be retrieved on output from the routine and print it out

Page 89: 清华大学 NAG MATLAB 工具箱培训

89

Exercise 4

Find the zeros of the real cubic polynomial x3 - 6x2 + 11x – 6

Plot the function to check your result (X range from 0.0 ~

4.0)

Page 90: 清华大学 NAG MATLAB 工具箱培训

Experts in numerical algorithms and HPC services

Question and Answer

[email protected] http://cn.nag-gc.com

Page 91: 清华大学 NAG MATLAB 工具箱培训

91

Useful Information

Download page http://cn.nag-gc.com/downloads/index.asp

Quick start guide http://cn.nag-gc.com/doc/inun_gc.asp

Documentation http://cn.nag-gc.com/support_documentation.asp

Industry articles http://cn.nag-gc.com/IndustryArticles/Index.asp

NAG News http://cn.nag-gc.com/NagNews/index.asp

Success stories http://cn.nag-gc.com/Market/casestudies.asp

Technical support

[email protected] +886 2 2509 3288