MatlabCC Compiler4
MatlabCC Compiler4
Third Edition
This book is a great tutorial for C/C++ programmers who use MATLAB to develop applications and solutions
All rights reserved. No part of this CD-ROM should be reproduced, or transmitted by any means, electronic, mechanical, photocopying, recording, or otherwise, without written permission from the publisher. ISBN 0-9725794-3-5 Trademark MATLAB is a registered trademark of The MathWorks, Inc. Microsoft is a registered trademark of Microsoft Corporation. Disclaimer The programs and applications on this CD-ROM have been carefully tested, but are not guaranteed for any particular purpose. The publisher does not offer any warranties and does not guarantee the accuracy, adequacy, or completeness of any information and is not responsible for any errors or omissions or the results obtained from use of such information.
Contents
Preface v
1
3 3 3 4 4 5 5 7 7 12 13 15 16 17 17 20 23 23 26 29 31
2 Opening a C++ File in Microsoft Visual C++ 6.0 2.1 2.2 2.3 2.4 2.5 Opening a New C++ File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Adding a Header File to a Project . . . . . . . . . . . . . . . . . . . . . . . . . . Writing a Code in a Header File . . . . . . . . . . . . . . . . . . . . . . . . . . . Writing a Code in a C++ File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Building and Executing a C++ Project . . . . . . . . . . . . . . . . . . . . . . . .
3 Setting Up a Microsoft Visual C++ 6.0 Project with MATLAB Compiler 4 3.1 3.2 Procedure of Project Setting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Testing of Project Setting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4 Opening a C++ File in Microsoft Visual C++ .Net 4.1 4.2 4.3 4.4 Creating a New Microsoft Visual C++ .Net Project . . . . . . . . . . . . . . . . . Adding a C++ Source File to the Project . . . . . . . . . . . . . . . . . . . . . . . Adding a Header File to the Project . . . . . . . . . . . . . . . . . . . . . . . . . Building and Executing the Project . . . . . . . . . . . . . . . . . . . . . . . . . . i
ii 5 Setting Up a Microsoft Visual C++ .Net Project with MATLAB Compiler 4 5.1 5.2 Procedure of Project Setting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Testing of Project Setting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.2.1 5.2.2 Writing a code for testing . . . . . . . . . . . . . . . . . . . . . . . . . . . Building and executing the project . . . . . . . . . . . . . . . . . . . . . . 33 33 43 43 44
Part II: Creating and Using C/C++ Shared Libraries to Solve Mathematical Problems 45
6 Generating C and C++ Shared Libraries from MATLAB M-Files for Using in Microsoft Visual C++ .Net 6.1 6.2 6.3 6.4 6.5 6.6 6.7 Generating a C Shared Library from a MATLAB M-File . . . . . . . . . . . . . . Writing a Code to Call Functions in a C Shared Library . . . . . . . . . . . . . . Generated Functions from MATLAB Compiler 4 . . . . . . . . . . . . . . . . . . Using Multiple C Shared Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . Generating a C++ Shared Library From a MATLAB M-File . . . . . . . . . . . . Writing a Code to Call Functions in a C++ Shared Library . . . . . . . . . . . . . Generated C++ Functions from MATLAB Compiler 4 . . . . . . . . . . . . . . . 47 48 51 53 54 57 57 59 61 61 66 69 72 83 91 94 98 99 102 104 106 107 111
7 Transfer of Values between C/C++ double, mxArray, and mwArray 7.1 7.2 7.3 7.4 7.5 Transfer of Values between C/C++ double and mxArray . . . . . . . . . . . . . . Transfer of Values from C/C++ double to mwArray . . . . . . . . . . . . . . . . . Transfer of Values from mwArray to C/C++ double . . . . . . . . . . . . . . . . . The Code of the Utility File mxUtilityCompilerVer4.h . . . . . . . . . . . . . . . The Code of the Utility File mwUtilityCompilerVer4.h . . . . . . . . . . . . . . .
8 Matrix Computations 8.1 8.2 8.3 8.4 8.5 8.6 8.7 8.8 Matrix Addition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Matrix Subtraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Matrix Multiplication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Matrix Determinant . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Inverse Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Transpose Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Assigning Directly Values for a Matrix . . . . . . . . . . . . . . . . . . . . . . . . Assigning Values for a Matrix from a File . . . . . . . . . . . . . . . . . . . . . .
iii 9 Linear System Equations 9.1 9.2 9.3 9.4 Linear System Equations Sparse Linear System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 118 126 131 140 151 154 165 166 167 173 175 179 183 187 192 195 210 227 229 234 237 . . . . . . . . . . . . . . . . . . . . . . 239 247 255 256 265 266 267 271 274 277 280
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
10 Ordinary Dierential Equations 10.1 First Order ODE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10.2 Second Order ODE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10.2.1 Analysis of second order ODE . . . . . . . . . . . . . . . . . . . . . . . . . 10.2.2 Using a second order ODE function . . . . . . . . . . . . . . . . . . . . . 11 Integration 11.1 Single Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11.2 Double-Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Curve Fitting and Interpolations 12.1 Polynomial Curve Fitting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12.2 One-Dimensional Polynomial Interpolation . . . . . . . . . . . . . . . . . . . . .
12.3 Two-Dimensional Polynomial Interpolation for Grid Points . . . . . . . . . . . . 12.4 Two-Dimensional Polynomial Interpolation for Non-Grid Points . . . . . . . . . . 13 Roots of Equations 13.1 Roots of Polynomials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13.2 The Root of a Nonlinear-Equation . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Fast Fourier Transform 14.1 One-Dimensional Fast Fourier Transform
14.2 Two-Dimensional Fast Fourier Transform . . . . . . . . . . . . . . . . . . . . . . 15 Eigenvalues and Eigenvectors 15.1 Eigenvalues and Eigenvectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Random Numbers 16.1 Uniform Random Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16.1.1 Generating Uniform Random Numbers in Range [0,1] . . . . . . . . . . . 16.1.2 Generating Uniform Random Numbers in Range [a,b] . . . . . . . . . . . 16.1.3 Generating a Matrix of Uniform Random Numbers in Range [0,1] . . . . . 16.1.4 Generating a Matrix of Uniform Random Numbers in Range [a,b] . . . . 16.2 Normal Random Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
iv 16.2.1 Generating Normal Random Numbers with mean=0 and variance=1 . . . 16.2.2 Generating Normal Random Numbers with mean=a and variance=b . . . 16.2.3 Generating a Matrix of Normal Random Numbers with mean=0 and variance=1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16.2.4 Generating a Matrix of Normal Random Numbers with mean=a and variance=b . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288 286 280 283
Part III: MATLAB Engine: Calling MATLAB Workspace in C/C++ Functions MEX-File: Calling C Functions in MATLAB Workspace Generating Stand Alone Applications from MATLAB M-Files 291
17 Calling MATLAB Workspace in C/C++ Functions 17.1 Calling MATLAB Workspace with Input/Output as a Scalar . . . . . . . . . . . 17.2 Calling MATLAB Workspace with Input/Output as a Vector and a Matrix . . . 17.3 Generating a MATLAB Graphic from a C/C++ Function . . . . . . . . . . . . . . 18 MEX-Files, Calling a C Function in MATLAB Workspace 18.1 MEX-File with Input/Output as Scalars . . . . . . . . . . . . . . . . . . . . . . . 18.2 MEX-File with Input/Output as Vectors . . . . . . . . . . . . . . . . . . . . . . . 18.3 MEX-File with Input/Output as Matrixes . . . . . . . . . . . . . . . . . . . . . . 18.4 MEX-Function Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Stand-Alone Applications 19.1 Installing MATLAB Component Runtime to a target machine . . . . . . . . . . . 19.2 Stand-Alone Application for an Addition Operator . . . . . . . . . . . . . . . . . 19.3 Stand-Alone Application for Linear Equations . . . . . . . . . . . . . . . . . . . . 19.4 Stand-Alone Application for Using Matlab Plots . . . . . . . . . . . . . . . . . . 19.5 Stand-Alone Application for Calculating an Integration . . . . . . . . . . . . . . References Index 295 295 298 301 305 305 307 308 313 315 315 317 319 320 322 325 327
Preface
At the time this book is written, most students and engineers know the basics of MATLAB and C/C++ programming. Their projects are often supported by C/C++ and/or MATLAB, but not many of them know how to use the C/C++ programming with MATLAB support to handle their problems. MATLAB is one of most powerful mathematical softwares used to solve student, engineering, and scientic problems, and C/C++ programming is one of the most used programming languages in the world with numerous applications. Therefore, the combination of both tools, C/C++ and MATLAB, has the potential to become one of the best tools for solving technical problems.
MATLAB provides a toolbox MATLAB Compiler to handle the works between MATLAB and C/C++. This book implements the combination of C/C++ and MATLAB to solve the problems. The features of this book are designed to handle the following projects: Common mathematical libraries were created from MATLAB M-les to use in C/C++ functions. The MATLAB workspace is called to perform particular tasks in C/C++ functions. A C function is called into the MATLAB workspace by writing a MEX-function. Stand-alone applications were created to use in the target machine which doesnt have the MATLAB software. The book contains all C/C++ programming codes in all chapters, that quickly help users solve their problems. This book tries to support C/C++ programmers, especially students and engineers who use C/C++ and MATLAB to develop applications and solutions for their projects and designs.
vi
Chapter 1
Introduction
1.1 Introduction
MATLAB is a special mathematical software that includes many toolboxes. MATLAB Compiler is the most important toolbox that supports C/C++ programmers. We can use MATLAB Compiler 4.0 to create C/C++ functions from MATLAB M-les. These generated C/C++ functions will then be called by another C/C++ functions in a C/C++ le. In addition, we can use a C/C++ function to call the MATLAB workspace to perform specic tasks by using MATLAB Engine, and we can write a MEX-le for a C function to call it in the MATLAB workspace. MATLAB Compiler also provides a feature that can create stand-alone applications using in the target machine which doesnt have the MATLAB software. There are some C/C++ compilers working with MATLAB Compiler 4.0. In this book, the compilers which were used to compile with MATLAB Compiler 4.0 are Microsoft Visual C++ 6.0, Microsoft Visual C++.Net 2002 (ver. 7.0), and Microsoft Visual C++.Net 2003 (ver. 7.1).
1.2
The features of this book are designed to handle following projects: 1. Common mathematical libraries were created from MATLAB M-les to use in C/C++ functions. This is the popular purpose of using MATLAB Compiler. In this project, C/C++ functions will call mathematical functions in a generated mathematical library to solve the mathematical problems. These solutions are explained from Chapter 8 to Chapter 16. In these chapters, the used functions are chosen based on the typical problems, therefore the user can choose another options or another functions to solve his/her particular problems to satisfy requirements. 2. Working on C/C++ programming, we wants to call the MATLAB workspace to perform
4 particular tasks in the MATLAB workspace then transfer back results to a C/C++ function. This solution is explained in Chapter 17. 3. From an existing C function, we wants to call this function in MATLAB by writing a MEXfunction. In this project, we will write a MEX-function for an existing C function, then call this function into the MATLAB workspace. This solution is explained in Chapter 18. 4. From existing M-les, we wants to generate stand-alone applications. In this project, MATLAB Compiler 4 will be used to generate stand-alone applications from existing MATLAB M-les. These generated stand-alone applications then will be used in a target machine which doesnt have the MATLAB software. This solution is explained in Chapter 19.
1.3
MATLAB Compiler had some versions and there are some changes of its features in dierent versions. The focus of this book is only on MATLAB Compiler 4. The example codes in this book are developed, compiled, and tested in Windows 2000, Microsoft Visual C++ 6.0, Microsoft Visual C++ .Net (2002 and 2003), MATLAB 7, and MATLAB Compiler 4.0. These examples are intended to establish common works for C/C++ programming and MATLAB. The example codes are working on scalars, vectors, and matrixes that are inputs/outputs of functions for every application. In addition, the example codes are portable and presented in the step-by-step method, therefore the user can easily reuse the codes or writes his/her own codes by following the step-by-step procedure while solving the problems.
The most C/C++ common functions in the examples are void functions (return type is void) to avoid ambiguity and to emphasize the topic being explained. The book also includes the settings of C++ compilers with MATLAB, and contains utility les to transfer values in dierent types. These les are very helpful in using MATLAB for C/C++ programming.
1.4
MATLAB Compiler 4 have two principle types, mwArray and mxArray. When coding, you can use mwArray and mxArray in input/output as the new types in C/C++ functions, or make transfers of values between C/C++ double, mwArray, and mxArray. Chapter 7 shows transfers between C/C++ double, mwArray, and mxArray. These transfers are very useful in working on the MATLAB Compiler toolbox.
1.5
There are many dierences between C/C++ and MATLAB C/C++ [4]. The most important dierence to know is: C/C++ stores its two-dimensional arrays in the row-major order, whereas MATLAB C/C++ stores arrays in the column-major order. You must, therefore, remember this when setting up matrix data.
1.6
Reference Manuals
In working with MATLAB Compiler 4 you may need more information to help your task. We refer here several manuals from the MATLAB website that you can download for more information. http://www.mathworks.com/access/helpdesk/help/pdf_doc/compiler/Compiler4.pdf http://www.mathworks.com/access/helpdesk/help/pdf_doc/matlab/apiext.pdf http://www.mathworks.com/access/helpdesk/help/pdf_doc/matlab/apiref.pdf http://www.mathworks.com/access/helpdesk/help/pdf_doc/matlab/refbook.pdf http://www.mathworks.com/access/helpdesk/help/pdf_doc/matlab/refbook2.pdf http://www.mathworks.com/access/helpdesk/help/pdf_doc/matlab/refbook3.pdf If you couldnt nd these les at the time you are looking for, The MathWorks Inc. may change URL of these les, but you can nd its somewhere in The MathWorks website www.mathworks.com.
Chapter 2
2.1
To open Microsoft Visual C++ version 6.0, click Start, click Programs, click Microsoft Visual C++ 6.0, and click Microsoft Visual C++ 6.0. You will obtain Fig. 2.1.
In Fig. 2.1, on menu bar, click File, New (obtain Fig. 2.2).
In Fig. 2.2, click Files tab, click C++ Source File (obtain Fig. 2.3).
In Fig. 2.3, click on the browse button to choose a folder, then type a le name, say Myfile.cpp (see Fig. 2.4), click OK.
9 You now have the blank le Myle.cpp. Write the following code into this le (Fig. 2.5) as follows:
Listing code
#include <iostream.h>
void main()
end code
On menu bar (Fig. 2.5), click Build, then click Build (obtain Fig. 2.6).
In Fig. 2.6, click Yes (to create a project), you will obtain Fig. 2.7.
10
In Fig. 2.7, click Yes. You will obtain a new project named MyFile (Fig. 2.8).
On the menu bar (Fig. 2.8), click Build, click Rebuild All. On the menu bar (Fig. 2.8), click Build, click Execute MyFile.exe (obtain an output result, see Fig. 2.9).
To copy the result, drag words to highlight, click on left-top corner button (C:\), click Edit, click Copy (Fig. 2.10).
11
To debug an error, on the menu bar (Fig. 2.8) click Tool, click Customize (obtain the customize dialog as shown in Fig. 2.11). In the customize dialog (Fig. 2.11), click Commands tab, Edit (in
Category), drag and drop two hammer-icons into the icon bar (Fig. 2.12).
12 If you have errors in your le when building Myle.exe le, click on these hammer-icons, these will reveal your errors. Figure 2.13 shows an example of the error with missing a semi colon at the end of the line.
There are many helpful features in MSVC that you can nd by clicking on Help in the menu bar. Since the purpose of this chapter is to help users who are new to MSVC, a number of the most basic features will be presented in the next sections.
2.2
To add a header le into the project, in the project workspace (Fig. 2.14), click FileView tab, right click on MyFile les, click "Add Files to Project .." (obtain Fig. 2.15), then type Test.h in this dialog (see Fig. 2.15).
13
In Fig. 2.15, click OK (obtain Fig. 2.16). In Fig. 2.16, click Yes (to generate the new header le named Test.h into the project. See Fig. 2.17).
2.3
This section describes how to write a simple code in the header le. In the project workspace (Fig. 2.17), double-click on Test.h (obtain Fig. 2.18).
14
In Fig. 2.18, click Yes (obtain a blank header le, see Fig. 2.19).
Now you will write a simple code in this header le Test.h (see Fig 2.20), as follows:
Listing code
class Test {
Test () ~Test ()
{ ; } { ; }
} ;
15
/* **************************** */
void Test::TestFunc()
end code
2.4
Modify the lines of the code in the C++ le MyFile.cpp (Fig. 2.21), as follows: #include <iostream.h> #include "Test.h"
void main()
16
2.5
On the menu bar (Fig. 2.21), click Build, click Rebuild All. On the menu bar (Fig. 2.21), click Build, click Execute MyFile.exe. It should contain no errors and should give the output result (Fig. 2.22).
At this point you can use Microsoft Visual C++ 6.0 to open and compile a C++ le.
Chapter 3
3.1
The following procedure is to set up a Microsoft Visual C++ 6.0 (MSVC) project for working with MATLAB Compiler 4. 1. From an MSVC project (as described in Chapter 2), click Project from the menu bar, click Setting. You will obtain the dialog box as shown in Fig. 3.1.
18 From Category shown in Fig. 3.2, choose Code Generation. In Use run-time Library, choose Multithreaded DLL (Fig. 3.2).
4. In Preprocessor denitions text box (Fig. 3.3), replace all with (see Fig. 3.4):
WIN32,_DEBUG,_CONSOLE,_MBCS,_WINDOWS,_AFXDLL,IBMPC,MSVC,MSWIND,__STDC__
5. Suppose that your MATLAB path is C:\MATLAB7. In Additional include directories, add (see Fig. 3.4):
C:\MATLAB7\extern\include
19
6. Click the Link tab (see Fig. 3.5). In Category (Fig. 3.5), choose Input. In Object/library modules, add these libraries (Fig. 3.6): libeng.lib libfixedpoint.lib libmat.lib libmex.lib libmwservices.lib libmx.lib libut.lib mclcom.lib mclcommain.lib mclmcr.lib mclmcrrt.lib mclxlmain.lib Note that they are separated by a space.
Figure 3.5: The Link tab in Project Settings 7. Suppose that your MATLAB path is C:\MATLAB7. In Additional library path, add (see Fig. 3.6):
C:\MATLAB7\extern\lib\win32\microsoft\msvc60
20
3.2
To test the project setting, create an MSVC project then perform the project setting as described in the above procedure. Write the simple following code in a C++ le, then build and execute the project.
The following is the code for testing the project setting. After building and executing the project, you should have no errors. Listing code
#include <iostream.h> #include "mclcppclass.h" int main() {
cout << " Testing setting-up MSVC 6.0 " << endl ; mwArray mw_test(1, 2, mxDOUBLE_CLASS) ;
return 0 ; }
21
Remarks
1. You can use the Microsoft Visual C++ 6.0 (MSVC) project set up in this chapter as a template for another MSVC projects in working with MATLAB Compiler 4. 2. Most of our example projects are Microsoft Visual C++ .Net projects, therefore for working with Microsoft Visual C++ 6.0, do these following steps: Copy the project that was set up as described in this chapter. This project is a current your project. Copy the les Example.cpp, Example.h, and the utility le (mxUtilityCompilerVer4.h or mwUtilityCompilerVer4.h) to your current project. Remove the top line : #pragma warning(disable : 4995)
22
Chapter 4
4.1
To open Microsoft Visual C++ .Net, click Start, Programs, Microsoft Visual Studio .Net, Microsoft Visual Studio .Net. You will obtain Fig. 4.1.
In Fig. 4.1, on menu bar, click File, New, Project (obtain Fig. 4.2).
24
In Fig. 4.2, click Visual C++ Projects, click Win32 Projects. Click the Browse button to choose a folder, then type the project name, say Example, as in Fig. 4.3. In Fig. 4.3 click OK . You will obtain Fig. 4.4
25
In Fig. 4.4, click Application Settings, select Console application. Select Empty project (see Fig. 4.5). Click Finish to create the project. Youll obtain Fig. 4.6.
26
4.2
You now have the blank project and you will add a C++ le to this project.
In Fig. 4.7, right click Example, click Add, click Add New Item to open a dialog box as in Fig. 4.18
In Fig. 4.8 then type the name of le Example.cpp. Click C++ File from the Templates list on the right, as Fig. 4.8.
In Fig. 4.8 click Open, you will obtain a blank le Example.cpp (Fig. 4.9).
27
Write the following code into this le Example.cpp, (Fig. 4.10) as follows:
Listing code
#include <iostream.h>
return 0 ; }
end code
28 To build the executive le, click Build on the menu, click Build Solution. You will have no error, but one warning as follows: : warning C4995: _OLD_IOSTREAMS_ARE_DEPRECATED: name was marked as #pragma deprecated" To ignore this warning, add this line code (Fig. 4.11): #pragma warning(disable : 4995)
To execute again, click Build on the menu, click Build Solution. You will have no errors and no warning. To see the result, on the menu bar click Debug, click Start Without Debugging (Fig. 4.13).
Note: You can avoid the above warning by using Standard Library, but this will give more diculties in working with MATLAB Compiler, therefore within the scope of working with MATLAB Compiler we dont recommend using Standard Library in MSVC++.Net. For more information of this warning, go to the Microsoft website: http://msdn.microsoft.com/library/
29
4.3
To add a header le to the project, right click Example, click Add, click Add New Item to open a dialog box (see Fig. 4.7). Type the name for the header le Example.h, click Header File from the Templates list on the right as in Fig. 4.13. Click Open, you will obtain the blank header le Example.h (Fig. 4.14).
30 Now you will write a simple code in this header le Example.h (see Fig 4.15), as follows: Listing code
class Test {
Test () ~Test ()
{ ; } { ; }
end code
Modify lines of the code in the C++ le Example.cpp (Fig. 4.16), as follows:
Listing code
#pragma warning(disable : 4995)
31
int main() { cout << "Hello World" << endl ;
end code
4.4
In Fig. 4.16, on the menu bar click Build, click Build Solution. In Fig. 4.16, on the menu bar click Debug, click Start Without Debugging. It should contain no errors and give the output result as shown in Fig. 4.17
At this point you can use Microsoft Visual C++ .Net to open and compile a C++ le.
32 Note If you have errors in your le when building, the error message will show in the Task List as in Fig. 4.18. Double click on this message, it will show the error in the code. There are many helpful features in MSVC++.Net that you can nd in Help menu.
Chapter 5
5.1
The following procedure is to set up an MSVC++.Net project for working with MATLAB Compiler 4. 1. Create an MSVC++.Net project as described in Chapter 4. This project is shown in Fig. 5.1.
34
2. In Fig. 5.1, right click on Example, click Properties (Fig. 5.2), youll obtain a property dialog as shown in Fig. 5.3.
3. In Fig. 5.3, click C/C++, click General, click Additional Include Directories (see Fig. 5.4). In Fig. 5.4 click on the browse button, youll obtain the Additional Include Directories dialog box as shown in Fig. 5.5
35
4. Suppose that your MATLAB path is C:\MATLAB7. In Fig. 5.5, add the following directorie to this dialog (Fig. 5.6): C:\MATLAB7\extern\include
36 In Fig. 5.6, click OK. These directories are added to the Property Pages (see Fig 5.7).
5. In Fig. 5.7, click Preprocessor, click Preprocessor Denitions (as shown in Fig. 5.8). Click the browse button, youll obtain Fig. 5.9.
37 6. In Fig 5.9, add the following lines to this dialog (see Fig. 5.10): _WINDOWS _AFXDLL IBMPC MSVC MSWIND __STDC__
7. In Fig. 5.10 click OK. The Preprocessor Denitions is changed as in Fig. 5.11.
8. In Fig. 5.11, click Code Generation, click Runtime Library (as shown in Fig. 5.12).
38
9. In Fig 5.12, click on the arrow button to select the option of Runtime Library: Multi-threaded Debug DLL (/MDd) as shown in Fig. 5.13.
10. In Fig. 5.13, click Precompiled Headers, click Create/Use Precompiled Header, click on the arrow button to select Not Using Precompiled Headers, as shown in Fig. 5.14.
39
11. In Fig. 5.14, click Linker, click General, click Additional Library Directory, as shown in Fig. 5.15.
In Fig. 5.15, click on the browse button, youll obtain the Additional Library Directory dialog box (Fig. 5.16).
40
12. In Fig. 5.16, add the following directory to this dialog (see Fig. 5.17): C:\MATLAB7\extern\lib \win32\microsoft\msvc70 Add C:\MATLAB7\extern\lib \win32\microsoft\msvc71 if youre using MSVC .Net 2003.
Click OK. These directories are added in Property Pages as shown in Fig. 5.18.
41 13. In Fig. 5.15, click Input, click Additional Dependencies as shown in Fig. 5.19.
14. In Fig. 5.19, click the browse button, youll obtain the Additional Dependencies dialog box (Fig. 5.20).
15. In Fig. 5.20, add the following libraries to this dialog (see Fig. 5.21).
16. In Fig. 5.21 click OK. These libraries are added in Property Pages as shown in Fig. 5.22.
17. In Fig. 5.22 click Apply, then click OK. You nished the setting of MSVC++.Net with MATLAB Compiler 4.
43
5.2
5.2.1
After setting up the procedure as above, write the following simple code in a C++ le Example.cpp (see Fig. 5.23), then build and execute the le.
Listing code
int main() {
end code
44 Note In the code, the line: #pragma warning(disable : 4995) is used to ignore a warnings 4995. To have more information about this warning, go to the Microsoft website: http://msdn.microsoft.com/library/
5.2.2
1. In Fig. 5.23, click Build, click Rebuild Solution. Click Debug, click Start Without Debugging. You have no error and warning and an output result as shown in Fig.5.24.
You completely nished the setting of the MSVC++.Net project with MATLAB Compiler 4. You can use this MSVC++.Net project as a template for using in another MSVC .Net projects.
Part II: Creating and Using C/C++ Shared Libraries to Solve Mathematical Problems
Chapter 6
Generating C and C++ Shared Libraries from MATLAB M-Files for Using in Microsoft Visual C++ .Net
This chapter describes how to generate a C and C++ shared libraries from MATLAB M-les and use it in Microsoft Visual C++ .Net (MSVC.Net). The MATLAB Compiler 4 will generate C/C++ functions from M-les. These functions then be used as library functions in another C/C++ functions. If we compiler all mathematical functions in M-les we will have a mathematical library for C/C++ functions. The main steps of the procedure to generate a C or C++ shared library from M-les and use in Microsoft Visual C++.Net are: 1. Write the command to generate an dll-le from the MATLAB M-les. 2. Set up a project in Microsoft Visual C++.Net (MSVC.Net) for working with MATLAB Compiler 4 as described in Chapter 5. 3. Add the generated les in appropriate directories, set up the project property, and write the code to call the generated C/C++ functions (in the dll-le). The following sections describe an example for compiling an M-le. The procedure to compile another M-les is the same.
48
6.1
The following are the steps of the procedure to generate a C shared library from a MATLAB M-le and use it in Microsoft Visual C++ .Net. 1. Create an M-le myplus.m as follows: function y = myplus(x, y) z = x + y ; 2. Open the command prompt, go to the current directory, and write the command (Fig. 6.1): mcc -B csharedlib:mypluslib myplus.m
This step will create 8 les in the current folder: mypluslib.c mypluslib.ctf mypluslib.dll mypluslib.exp mypluslib.exports mypluslib.h mypluslib.lib mypluslib_mcc_component_data.c
3. Create a project named Example in Microsoft Visual C++.Net (MSVC.Net) and set up for working with MATLAB Compiler 4 as described in Chapter 5. 4. Copy two les mypluslib.dll and mypluslib.ctf into the folder Debug (Fig. 6.2).
49 5. Copy two les mypluslib.h and mypluslib.lib into the folder Example (see Fig. 6.3).
6. In Solution Explore, right click on Example (see Fig. 6.4), click Property, and the Property Page dialog will appear (Fig. 6.5).
50 In Fig. 6.5 click Linker, click Input, click Additional Dependencies, and click the button at the top-right corner. The Additional Dependencies dialog will appear, then add mypluslib.lib to this dialog (Fig. 6.6). Click OK and click OK to close the dialogs.
7. From the M-function myplus.m MATLAB Compiler 4 has generated a function (see this name in mypluslib.h le), void mlfMyplus(int nargout, mxArray** y, mxArray* a, mxArray* b); ,with a rule well discuss in Section 6.3. In this function mlfMyplus(..), the arguments are:
51 nargout y a b : number of output (in this is case nargout = 1) : output variable : input variable : input variable
Note that the MATLAB Compiler has capitalized M in the function name mlfMyplus.
6.2
Following is the code in an MSVC .Net to call the function mlfMyplus(..). Note that this MSVC .Net has set with MATLAB as described in Chapter 5.
Listing code
/* Example.cpp */
int main() {
cout << " Generating a C Shared Library from MATLAB M-Files " ;
Test obj; cout << endl ; cout << obj.CalculatePlus(3.4, 2.1) << endl ;
return 0 ; }
/* Example.h */
class Test {
public:
52
Test () {
mclInitializeApplication(NULL,0); mypluslibInitialize(); }
~Test ()
mypluslibTerminate(); mclTerminateApplication(); }
} ;
/* **************************** */
/* step 1 : declare mxArray variables */ mxArray *mx_a ; mxArray *mx_b ; mxArray *mx_y = NULL ;
/* step 2 : assign memories */ mx_a = mxCreateDoubleMatrix(1, 1, mxREAL); mx_b = mxCreateDoubleMatrix(1, 1, mxREAL); mx_y = mxCreateDoubleMatrix(1, 1, mxREAL);
*/
53
mxDestroyArray(mx_b) ; mxDestroyArray(mx_y) ;
return result ;
end code Remarks 1. See the le mxUtilityCompilerVer4.h in Chapter 7. 2. From the eight generated les, we used only four les, mypluslib.h, mypluslib.lib, mypluslib.dll, and mypluslib.ctf. 3. After you built your project, MATLAB also created a folder mypluslib_mcr in the folder Debug. 4. If you want to use multiple libraries see Section 6.4. 5. To generate a C shared library from multiple M-les, we just normally add the adding M-les. For example: mcc -B csharedlib:mymathlib myplus.m mymtimes.m
6.3
The C function generated by MATLAB Compiler 4 from an M-function has a form depends on the M-function. With an M-functions with no return values, the C function has the form: void mlf<function-name>(<list_of_input_variables>);
With an M-function with at least one return value, the C function has the form: void mlf<function-name>(int number_of_return_values, <list_of_pointer_to_return_variables>, <list_of_input_variables>);
54 This generated C function has the pattern: 1. the return type is always void 2. the rst argument, nargout, is the number of output variables in the original M-function. 3. the next argument(s) are output variables in the original M-function. These output variables have the type double-pointer to mxArray, for example mxArray** y. 4. the next arguments are input variables in the original M-function. These variables have the type pointer to mxArray, for example, mxArray* a.
6.4
We often use the multiple C shared libraries in a C/C++ project. Adding and setting multiple C shared libraries in MSVC .Net are same as in single shared library (described in above). For example, suppose that we have two C shared libraries myplus and myminus. Well set up the myminus library same as we did for the myplus library as described in the previous sections: add two more les myminuslib.dll and myminuslib.ctf into the folder Debug (see Fig. 6.2). add two more les myminuslib.h and myminuslib.lib into the folder Example (see Fig. 6.3). add myminuslib.lib to the Additional Dependencies dialog (see Fig. 6.6). The following is the code to call functions in the multiple C shared libraries.
Listing code
/* Example.cpp */
int main() {
cout << " Using functions in multiple C shared libraries" ; cout << endl ;
return 0 ;
55
}
/* Example.h */
class Test {
public:
Test ()
~Test ()
} ;
/* **************************** */
56
mxArray *mx_y = NULL ;
/* step 2 : assign memories */ mx_a mx_b mx_y mx_y2 = mxCreateDoubleMatrix(1, 1, mxREAL); = mxCreateDoubleMatrix(1, 1, mxREAL); = mxCreateDoubleMatrix(1, 1, mxREAL); = mxCreateDoubleMatrix(1, 1, mxREAL);
/* step 4 : call the implemental function */ mlfMyplus (1, &mx_y , mx_a, mx_b); mlfMyminus(1, &mx_y2, mx_a, mx_b);
/* step 5: convert back mxArray to C/C++ double */ double resultPlus double resultMinus = mxArray2double_scalarReal(mx_y) = mxArray2double_scalarReal(mx_y2) ; ;
*/
<< endl ;
57
6.5
The steps of generating a C++ shared library are the same as generating a C shared library. 1. Write an M-le, for example myplus.m:
function y = myplus(x, y) z = x + y ;
2. Open the command prompt, go to the current directory, and write the command: mcc -W cpplib:cppmypluslib -T link:lib myplus.m 3. Set up the generated les as the same in Section 6.1. 4. The MATLAB Compiler 4 creates an implement function (see this name in cppmypluslib.h le), void myplus(int nargout, mwArray& y, const mwArray& a, const mwArray& b); , with the rule well discuss in Section 6.7. In this function myplus(..), the arguments are: nargout y a b : number of output (in this is case nargout = 1) : output variable : input variable : input variable
6.6
Following is the code in an MSVC .Net to call the function myplus(..). Note that this MSVC .Net has set with MATLAB as described in Chapter 5.
58 Listing code
/* Example.cpp */
int main() {
cout << " Generating a C++ Shared Library from MATLAB M-Files " ;
Test obj; cout << endl ; cout << obj.CalculatePlus(3.4, 2.1) << endl ;
return 0 ; }
/* Example.h */
class Test {
public:
Test ()
mclInitializeApplication(NULL,0); cppmypluslibInitialize(); }
~Test ()
cppmypluslibTerminate(); mclTerminateApplication(); }
59
} ;
/* declare mxArray variables */ mwArray mw_y(1, 1, mxDOUBLE_CLASS) ; mwArray mw_a(1, 1, mxDOUBLE_CLASS) ; mwArray mw_b(1, 1, mxDOUBLE_CLASS) ;
return result ;
6.7
The C++ function generated by MATLAB Compiler 4 from an M-function has a form depends on the M-function. With an M-function with no return values, the C function has the form: void <function-name>(<list_of_input_variables>);
With an M-function with at least one return value, the C function has the form:
for example: void myplus(int nargout, mwArray& y, const mwArray& a, const mwArray& b) This generated C++ function has the pattern: 1. the return type is always void 2. the rst argument, nargout, is the number of output variables in the original M-function. 3. the next argument(s) are output variables in the original M-function. 4. the next arguments are input variables in the original M-function.
Chapter 7
7.1
This section shows how to transfer values between C/C++ double type and mxArray through the example code. We write an utility le mxUtilityCompilerVer4.h for convenience in using the transfer of values between C/C++ double and mxArray. The following example code use functions in this le mxUtilityCompilerVer4.h to implement the transfers. The mxUtilityCompilerVer4.h le is shown at the end of this chapter. 1. scalar transfer: a. real scalar double db_scalar = 1.1 = NULL ; ;
mxArray *mx_scalar
62 double2mxArray_scalarReal(db_scalar, mx_scalar) ;
double db_scalarReturn = mxArray2double_scalarReal(mx_scalar) ; cout << " db_scalarReturn = " << db_scalarReturn << endl ;
mxArray *mx_Complex
= NULL
double db_returnReal, db_returnImag ; mxArray2double_scalarComplex(mx_Complex, db_returnReal, db_returnImag) ; cout << " db_returnReal = " << db_returnReal << endl ; cout << " db_returnImag = " << db_returnImag << endl ;
mxDestroyArray(mx_Complex) ; 2. vector transfer: a. real vector double db_vector[3] = int vectorSize = 3 ; { 1.1, 2.2, 3.3 } ;
double2mxArray_vectorReal(db_vector, mx_vector) ;
63
mxDestroyArray(mx_vector) ; delete [] db_vectorReturn ; b. complex vector double db_Real[3] = double db_Imag[3] = int vectorSize = 3 ; { 1.1, 2.2, 3.3 } ; { 4.4, 5.5, 6.6 } ;
double *db_returnReal = new double [vectorSize] ; double *db_returnImag = new double [vectorSize] ;
cout << db_returnReal[i] << " + " ; cout << db_returnImag[i] << "i" } << endl ;
64 3. matrix transfer: a. real matrix double db_A[3][3] = {{ 1.1, 2.2, 3.3} , {4.4, 5.5, 6.6} , {7.7, 8.8, 9.9} } ;
double **db_ReturnA
mxDestroyArray(mx_A) ; delete [] db_ReturnA ; b. complex matrix double db_Real[3][3] = {{ 1.1, 2.2, 3.3}, {4.4, 5.5, 6.6}, double db_Imag[3][3] = {{ 11 , 12 , 13 }, {14 , 15 , 16 }, {7.7, 8.8, 9.9} } ; {17 , 18 , 19 } } ;
65 double **db_returnReal = new double* [row] ; double **db_returnImag = new double* [row] ;
mxDestroyArray(mx_complex) ; delete [] db_returnReal ; delete [] db_returnImag ; Remark If a matrix in the double-pointer type, we pass to the function as follow: double ** db_Real = new double* [row] ; double ** db_Imag = new double* [row] ;
db_Real[i] = new double [col] ; db_Imag[i] = new double [col] ; } ... mxArray *mx_complex = NULL ; mx_complex = mxCreateDoubleMatrix(row, col, mxCOMPLEX) ;
66
7.2
This section shows how to transfer values from C/C++ double type to mwArray through the example code. 1. scalar transfer: a. real scalar mwArray mw_scalar(1, 1, mxDOUBLE_CLASS) ; mw_scalar(1,1) = 1.4 ;
b. complex scalar mwArray mw_scalarComplex(1, 1, mxDOUBLE_CLASS, mxCOMPLEX) ; mw_scalarComplex(1,1).Real() = 2.2 ; mw_scalarComplex(1,1).Imag() = 3.3 ;
2. vector transfer: a. real vector /* row vector */ double db_vector[6] = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0} ; int vectorSize = 6 ;
67 b. complex vector double realdata[4] = {1.0, 2.0, 3.0, 4.0}; double imagdata[4] = {10.0, 20.0, 30.0, 40.0}; int aSize = 4 ;
double db_matrix[3][2] = { {1.0, 2.0} , {3.0, 4.0}, {5.0, 6.0} } ; int arow = 3 ; int acol = 2 ;
mwArray mw_matrix = double2mwArray_matrixReal(&db_matrix[0][0], arow, acol) ; std::cout << mw_matrix << std::endl ;
/* or */
{ { // assign a number
68 mwArray mw_matrixA = double2mwArray_matrixReal(db_matrixA, arow, acol) ; std::cout << mw_matrixA << std::endl ;
delete [] db_matrixA ; See the functions double2mwArray_matrixReal(..) in mwUtilityCompilerVer4.h at the end of this chapter.
b. complex matrix double db_AReal[3][4] = { { 1.1, 2.2 { 5.5, 6.6 , 3.3 , 7.7 , 4.4 , 8.8 } } ,\ ,\ } ;
double db_AImag[3][4] = {
, 0.3 , 0.7
, 0.4 , 0.8
} } }
,\ ,\ } ;
{ 0.9, 0.21,
0.21 , 0.22
mwArray mw_complex = double2mwArray_matrixComplex(&db_AReal[0][0], &db_AImag[0][0], row, col) ; cout << " mw_complex " << endl ; std::cout << mw_complex << std::endl ;
/* or */ /* if matrix in double-pointer */
double ** db_Real = new double* [row] ; double ** db_Imag = new double* [row] ;
mwArray mw_complexOther = double2mwArray_matrixComplex(db_Real, db_Imag , row, col) ; cout << " mw_complex other: " << endl ; std::cout << mw_complexOther << std::endl ;
delete [] db_Real ; delete [] db_Imag ; See the functions double2mwArray_matrixComplex(..) in mwUtilityCompilerVer4.h at the end of this chapter.
7.3
This section shows how transfer values from mwArray to C/C++ double type through the example code. 1. scalar transfer: a. real scalar mwArray mw_scalar(1, 1, mxDOUBLE_CLASS) ; mw_scalar(1,1) = 1.4 ; double db_scalar = (double) mw_scalar(1,1) ;
b. complex scalar mwArray mw_scalarComplex(1, 1, mxDOUBLE_CLASS, mxCOMPLEX) ; mw_scalarComplex(1,1).Real() = 2.2 ; mw_scalarComplex(1,1).Imag() = 3.3 ;
70 ... /* suppose that mw_vector already had values */ int vectorSize = 6 ; double *db_vector2 = new double[vectorSize] ; mwArray2double_vectorReal(mw_vector, db_vector2) ;
delete [] db_vector2 ; See the function mwArray2double_vectorReal(..) in mwUtilityCompilerVer4.h at the end of this chapter.
int aSize = 4 ;
double* db_vectorReal = new double [aSize] ; double* db_vectorImag = new double [aSize] ; mwArray2double_vectorComplex(mw_vectorComplex, db_vectorReal, db_vectorImag) ;
{ ;
cout << db_vectorReal[i] << " + " << db_vectorImag[i] << "i" << endl } cout << endl ;
delete [] db_vectorReal ; delete [] db_vectorImag ; See the function mwArray2double_vectorComplex(..) in mwUtilityCompilerVer4.h at the end of this chapter.
delete [] db_matrixA ; See the function mwArray2double_matrixReal(..) in mwUtilityCompilerVer4.h at the end of this chapter.
double ** db_Real = new double* [row] ; double ** db_Imag = new double* [row] ;
db_Real[i] = new double [col] ; db_Imag[i] = new double [col] ; } ... /* suppose that mw_matrix already had values */ mwArray2double_matrixComplex(mw_complex, db_Real, db_Imag) ;
72
7.4
/* mxUtilityCompilerVer4.h */
/* 1a. transfer a C/C++ double scalar to a real mxArray */ void double2mxArray_scalarReal (double cpp, mxArray* mx_pointer) {
; ;
/* ************************* */ /* 1b. transfer C/C++ double scalars to a complex mxArray */ void double2mxArray_scalarComplex (double cppReal, double cppImag, mxArray* mx_pointer) {
; ;
; ;
} /* ************************* */
/* 2a. transfer a C/C++ double vector to a real mxArray */ void double2mxArray_vectorReal (double* db_vector, mxArray* mx_pointer) {
*/
73
int vectorSize ;
memcpy(mxGetPr(mx_pointer), db_vector,vectorSize*sizeof(double));
} /* ************************* */
/* 2b. transfer C/C++ double vectors to a complex mxArray */ void double2mxArray_vectorComplex (double* db_vectorReal, double* db_vectorImag , mxArray* mx_pointer) {
*/
int vectorSize ;
/* ************************* */ /* 3a. transfer a C/C++ double matrix to a real mxArray */ void double2mxArray_matrixReal (double** db_matrix, mxArray* mx_pointer) {
*/
74
int i, j, index ;
{ {
index
= j*row + i ;
db_vector[index] = db_matrix[i][j] ;
} }
delete[] db_vector ;
} /* ********************************************* */
/* 3a. transfer a C/C++ double matrix to a real mxArray */ void double2mxArray_matrixReal(double* addressMatrix00, mxArray* mx_pointer) {
*/
/*assign memories for a buffer */ int i, j ; double **db_matrixbuf ; db_matrixbuf = new double*[row] ;
75
double* db_vector ; db_vector = new double [row*col] ;
int index ;
{ {
index
= j*row + i ;
db_vector[index] = db_matrixbuf[i][j] ;
} }
/* ************************* */ /* 3b. transfer C/C++ double matrixes to a complex mxArray */ void double2mxArray_matrixComplex (double** db_matrixReal, double** db_matrixImag , mxArray* mx_pointer) {
*/
int i, j, index ;
76
for(j=0; j<col; j++) { {
} }
/* ********************************************* */ /* 3b. transfer C/C++ double matrixes to a complex mxArray */ void double2mxArray_matrixComplex(double* addressReal00, double* addressImag00 , mxArray* mx_pointer) {
*/
; ;
77
for(i=0; i<row; i++) {
int index ;
{ {
index
= j*row + i ;
} }
78
/* II. Transfer values from mxArray to C/C++ double */
/* ************************* */ /* 1a. transfer a real mxArray to a C/C++ double scalar */ double mxArray2double_scalarReal (mxArray* mx_pointer) {
return
db_scalar ;
/* ************************* */ /* 1b. transfer a complex mxArray to C/C++ double scalars */ void mxArray2double_scalarComplex (mxArray* mx_pointer, double &db_scalarReal , double &db_scalarImag) {
double* bufferImag ;
else { db_scalarImag } = 0 ;
/* ************************* */ /* 2a. transfer a real mxArray to a C/C++ double vector */ void mxArray2double_vectorReal (mxArray* mx_pointer, double* cpp) {
79
int row = mxGetM(mx_pointer) ; /* number of rows */
int vectorSize ;
cpp[i] = buffer[i] ;
/* ************************* */
/* 2b. transfer a complex mxArray to C/C++ double vectors */ void mxArray2double_vectorComplex (mxArray* mx_pointer, double* cppReal, double* cppImag) { /* row vector has row =1 */ int i ; int row = mxGetM(mx_pointer) ; /* number of rows */
int vectorSize ;
cppReal[i] = bufferReal[i] ; }
80
/* ************************* */ /* 3a. transfer a real mxArray to a C/C++ double matrix */ void mxArray2double_matrixReal (mxArray* mx_pointer, double** db_matrix) {
{ {
index
= j*row + i ;
db_matrix[i][j] = buffer[index] ;
} }
81
}
/* ************************* */
/* 3b. transfer a complex mxArray to C/C++ double matrixes */ void mxArray2double_matrixComplex (mxArray* mx_pointer, double** db_matrixReal , double** db_matrixImag) {
{ {
index
= j*row + i ;
db_matrixReal[i][j] = bufferReal[index] ;
} }
bufferImag = mxGetPi(mx_pointer) ;
{ {
index
= j*row + i ;
db_matrixImag[i][j] = bufferImag[index] ;
82
}
} else {
{ {
db_matrixImag[i][j] = 0.0 ;
} }
/* ******************************** */ /* ******************************** */ /* ******************************** */ void printMatrix(double** matrix, int row, int col) {
int i, j;
} /* ******************************** */
83
7.5
/* mwUtilityCompilerVer4.h */
/* 3a. Transfer a C/C++ double matrix to a real mwArray */ mwArray double2mwArray_matrixReal(double** db_matrix, int row, int col) {
} }
return }
mw_matrix ;
/* ********************************************* */ /* 3a. Transfer a C/C++ double matrix to a real mwArray */ mwArray double2mwArray_matrixReal(double* addressMatrix00, int row, int col) {
/*assign memories for a buffer */ int i, j ; double **db_matrixbuf ; db_matrixbuf = new double*[row] ;
84
} }
delete[] db_matrixbuf ;
return }
mw_matrix ;
} }
return }
mw_matrix ;
/* ********************************************* */ /* 3b. Transfer C/C++ double matrixes to a complex mwArray */ mwArray double2mwArray_matrixComplex(double* addressReal00, double* addressImag00 , int row, int col) {
85
/*assign memories for buffers */ int i, j ; double **db_bufReal ; double **db_bufImag ;
; ;
} }
return }
mw_matrix ;
/* ************************* */ /* ************************* */
86
/* ************************* */
/* 2a. Transfer a real mwArray to C/C++ double vector */ void mwArray2double_vectorReal (mwArray mw, double* cpp) {
int i ;
mwArray dim = mw.GetDimensions() ; int row = (int) dim(1,1) ; int col = (int) dim(1,2) ;
87
mwArray dim = mw.GetDimensions() ; int row = (int) dim(1,1) ; int col = (int) dim(1,2) ;
for (i=0; i<vectorSize; i++) { cppReal[i] = (double)mw(i+1, 1).Real() ; if( mw.IsComplex() ) { cppImag[i] = (double)mw(i+1, 1).Imag() ; } else { } cppImag[i] = 0.0 ; }
for (i=0; i<vectorSize; i++) { cppReal[i] = (double)mw(1, i+1).Real() ; if( mw.IsComplex() ) { cppImag[i] = (double)mw(1, i+1).Imag() ; } else { cppImag[i] = 0.0 ; }
int i,j ;
mwArray dim = mw_matrix.GetDimensions() ; int row = (int) dim(1,1) ; int col = (int) dim(1,2) ;
88
for (i=0; i<row; i++) { for (j=0; j<col; j++) {
} }
int i,j ;
mwArray dim = mw_matrix.GetDimensions() ; int row = (int) dim(1,1) ; int col = (int) dim(1,2) ;
} }
89
void printMatrix(double** matrix, int row, int col) {
int i, j;
90
Chapter 8
Matrix Computations
In this chapter well generate a C shared library matrixcomputationslib and a C++ shared library cppmatrixcomputationslib from common M-les working on matrix computation problems. The generated functions of these libraries will be used in a MSVC .Net project to solve matrix computation problems.
Following are steps to create a C shared library matrixcomputationslib.dll and a C++ shared library cppmatrixcomputationslib.dll which will be used to solve matrix computation problems in the next sections.
We will write the M-les as shown below. These les are used to generate the C and C++ shared libraries. These les are: mydet.m, myinv.m, myminus.m, mymtimes.m, myplus.m, and mytranspose.m
function y = mydet(a)
y = det(a) ;
function y = myinv(a)
y = inv(a) ;
function y = myminus(a, b)
y = a - b ;
92
function y = mymtimes(a, b)
y = a*b ;
function y = myplus(a, b)
y = a + b ;
function y = mytranspose( x )
y = x ;
matrixcomputationslib.exports matrixcomputationslib.lib
matrixcomputationslib_mcc_component_data.c
Add and set these les to the MSVC .Net project as described in Chapter 6. 3. In the following sections, well use the following implemental functions in this library to solve the common problems in the matrix computations (open the le matrixcomputationslib.h to see the names of these functions): void mlfMydet void mlfMyinv void mlfMyminus (int nargout, mxArray** y, mxArray* a); (int nargout, mxArray** y, mxArray* a); (int nargout, mxArray** y, mxArray* a, mxArray* b);
93 void mlfMymtimes void mlfMyplus (int nargout, mxArray** y, mxArray* a, mxArray* b); (int nargout, mxArray** y, mxArray* a, mxArray* b);
1. Write the command in Windows Command Prompt as follows to create a C++ shared library cppmatrixcomputationslib : mcc -W cpplib:cppmatrixcomputationslib -T link:lib mydet.m myinv.m myminus.m mymtimes.m myplus.m mytranspose.m 2. From this command MATLAB Compiler 4 will create eight les for C++ shared library: cppmatrixcomputationslib.cpp cppmatrixcomputationslib.dll cppmatrixcomputationslib.exports cppmatrixcomputationslib.lib cppmatrixcomputationslib.ctf cppmatrixcomputationslib.exp cppmatrixcomputationslib.h
cppmatrixcomputationslib_mcc_component_data.c
Add and set these les to the MSVC .Net project as described in Chapter 6. 3. In the following sections, well use the following implemental functions in this library to solve the common problems in the matrix computations (open the le cppmatrixcomputationslib.h to see the names of these functions): void mydet void myinv void myminus (int nargout, mwArray& y, const mwArray& a); (int nargout, mwArray& y, const mwArray& a); (int nargout, mwArray& y, const mwArray& a, const mwArray& b);
void mymtimes (int nargout, mwArray& y, const mwArray& a, const mwArray& b); void myplus (int nargout, mwArray& y, const mwArray& a, const mwArray& b);
94
8.1
Matrix Addition
Problem 1
input
Matrix A and B
11 B = 14 17
12 15 18
13
16 19
output
Listing code
/* Example.cpp */
int main() {
return 0 ; }
/* Example.h */
95
#include <iostream.h> #include "matrixcomputationslib.h" #include "mxUtilityCompilerVer4.h"
class Test {
Test ()
mclInitializeApplication(NULL,0); matrixcomputationslibInitialize(); }
~Test ()
matrixcomputationslibTerminate(); mclTerminateApplication(); }
} ;
/* **************************** */ void Test::addMatrix() { int i; double A[3][3] = {{ 1.1, 2.2, 3.3} , {4.4, 5.5, 6.6} , double B[3][3] = {{ 11 , 12 , 13 } , {14 , 15 , 16 } , {7.7, 8.8, 9.9} } ; {17 , 18 , 19 } } ;
/* step 1 : declare mxArray variables */ mxArray *mx_A = NULL ; mxArray *mx_B = NULL ; mxArray *mx_C = NULL ;
/* step 2 : assign memory */ mx_A = mxCreateDoubleMatrix(row, col, mxREAL) mx_B = mxCreateDoubleMatrix(row, col, mxREAL) mx_C = mxCreateDoubleMatrix(row, col, mxREAL) ; ; ;
96
/* step 3 : convert C/C++ matrix to mxArray */ double2mxArray_matrixReal(&A[0][0], mx_A) ; double2mxArray_matrixReal(&B[0][0], mx_B) ;
/* step 5 : convert back to C/C++ double double **db_matrixC ; db_matrixC = new double *[row] ; for(i=0; i<row; i++) {
*/
/*
*/ ; ; ;
delete [] db_matrixC ;
end code
Listing code
/* Example.cpp */
97
int main() {
cout << endl ; cout << "Matrix addition" << endl ; obj.addMatrix() ;
return 0 ; }
/* Example.h */
class Test {
Test ()
mclInitializeApplication(NULL,0); cppmatrixcomputationslibInitialize(); }
~Test ()
cppmatrixcomputationslibTerminate(); mclTerminateApplication(); }
98
double db_B[3][3] = {{ 11 , 12 , 13 } , {14 , 15 , 16 } , {17 , 18 , 19 } } ;
/* convert C/C++ matrix to mxArray */ mwArray mw_A = double2mwArray_matrixReal(&db_A[0][0], row, col) ; mwArray mw_B = double2mwArray_matrixReal(&db_B[0][0], row, col) ;
/* call an implemental function */ mwArray mw_C ; myplus (1, mw_C, mw_A, mw_B); std::cout << mw_C << std::endl ;
*/
*/
end code
8.2
Matrix Subtraction
99
8.3
Matrix Multiplication
Problem 2
input
4.4
13 15 17
output
void Test::multipleMatrix() { int i ; double A[3][4] = { { 1.1, 2.2 { 5.5, 6.6 , 3.3 , 7.7 , 4.4 , 8.8 } } ,\ ,\ } ;
double B[4][2] = {{ 10, 11}, {12, 13}, {14, 15}, {16, 17} } ;
100
int rowC = rowA ; int colC = colB ;
/* step 1 : declare mxArray variables */ mxArray *mx_A = NULL ; mxArray *mx_B = NULL ; mxArray *mx_C = NULL ;
/* step 2 : assign memory */ mx_A = mxCreateDoubleMatrix(rowA, colA, mxREAL) ; mx_B = mxCreateDoubleMatrix(rowB, colB, mxREAL) ; mx_C = mxCreateDoubleMatrix(rowC, colC, mxREAL) ;
/* step 5 : convert back to C/C++ double double **db_matrixC ; db_matrixC = new double *[rowC] ; for(i=0; i<rowC; i++) {
*/
mxArray2double_matrixReal(mx_C, db_matrixC) ;
/*
*/ ; ; ;
end code
101
Listing code
{ 5.5, 6.6
, 7.7
, 8.8
double db_B[4][2] = {{ 10, 11}, {12, 13}, {14, 15}, {16, 17} } ;
/* convert C/C++ matrix to mxArray */ mwArray mw_A = double2mwArray_matrixReal(&db_A[0][0], rowA, colA) ; mwArray mw_B = double2mwArray_matrixReal(&db_B[0][0], rowB, colB) ;
/* call an implemental function */ mwArray mw_C ; mymtimes (1, mw_C, mw_A, mw_B); std::cout << mw_C << std::endl ;
*/
102
mwArray2double_matrixReal(mw_C, db_C) ; /* print out */ printMatrix(db_C, rowC, colC) ;
*/
end code
8.4
Matrix Determinant
Problem 3
input
Matrix A 1.1 2.2 3.3 A = 7.7 4.4 9.9 4.4 5.5 8.8
output
Listing code
void Test::determinantMatrix() {
} ;
103
/* step 2 : assign memory */ mx_A = mxCreateDoubleMatrix(row, col, mxREAL) , mxREAL) ; ;
mx_detA = mxCreateDoubleMatrix( 1 , 1
*/
db_detA = mxArray2double_scalarReal(mx_detA) ;
/*
*/ ; ;
mxDestroyArray(mx_A) mxDestroyArray(mx_detA)
end code
Listing code
void Test::determinantMatrix() {
} ;
104
/* call an implemental function */ mwArray mw_detA ; mydet(1, mw_detA, mw_A);
*/
end code
8.5
Inverse Matrix
Listing code
void Test::inverseMatrix() {
105
/* step 4 : call an implemental function */ mlfMyinv(1, &mx_inverseA, mx_A );
/* step 5 : convert back to C/C++ double double **db_inverseA; db_inverseA = new double *[row] ; for(i=0; i<row; i++) {
*/
mxArray2double_matrixReal(mx_inverseA, db_inverseA) ;
/*
*/ ; ;
mxDestroyArray(mx_A) mxDestroyArray(mx_inverseA)
delete [] db_inverseA ;
end code
Listing code
void Test::inverseMatrix() {
106
mwArray mw_A = double2mwArray_matrixReal(&db_A[0][0], row, col) ; /* call an implemental function */ mwArray mw_invA ; myinv(1, mw_invA, mw_A);
*/
mwArray2double_matrixReal(mw_invA, db_invA) ;
*/
end code
8.6
Transpose Matrix
107
8.7
In the above sections, we used a method to assign a matrix to an mxArray or mwArray for using in the generated functions. When the matrix declares in the double-pointer, we can assign directly values of the matrix by using the same name function double2mwArray_matrixReal(..) or double2mxArray_matrixReal(..). The follow code implements these transfers.
; ;
108
db_matrixB[0][0] = db_matrixB[0][1] = db_matrixB[0][2] = 11 12 13 ; ; ;
14 15 16
; ; ;
17 18 19
; ; ;
/* step 2 : declare mxArray variables */ mxArray *mx_A = NULL ; mxArray *mx_B = NULL ; mxArray *mx_C = NULL ;
/* step 3 : assign memory */ mx_A = mxCreateDoubleMatrix(row, col, mxREAL) mx_B = mxCreateDoubleMatrix(row, col, mxREAL) mx_C = mxCreateDoubleMatrix(row, col, mxREAL) ; ; ;
/* step 6 : convert back to C/C++ double double **db_matrixC ; db_matrixC = new double *[row] ; for(i=0; i<row; i++) {
*/
mxArray2double_matrixReal(mx_C, db_matrixC) ;
109
printMatrix(db_matrixC, row, col) ;
/*
*/ ; ; ;
end code
; ;
110
db_matrixA[1][0] = db_matrixA[1][1] = db_matrixA[1][2] = 4.4 ; 5.5 ; 6.6 ;
11 12 13
; ; ;
14 15 16
; ; ;
17 18 19
; ; ;
/* convert C/C++ matrix to mxArray */ mwArray mw_A = double2mwArray_matrixReal(db_matrixA, row, col) ; mwArray mw_B = double2mwArray_matrixReal(db_matrixB, row, col) ;
/* call an implemental function */ mwArray mw_C ; myplus (1, mw_C, mw_A, mw_B);
*/
mwArray2double_matrixReal(mw_C, db_C) ;
111
/* free memories delete[] db_C ; */
end code
8.8
We can also assign values for a matrix from a le. The following is the code that get values for the matrix from a data le matrixA.dat.
/* matrixA.dat 1.1 4.4 7.7 */ 2.2 5.5 8.8 3.3 6.6 9.9
int i,j ;
/* step 2 :
assign memory */ ;
/* step 3 :
*/
112
ifstream f ; f.open("matrixA.dat", ios::in | ios::nocreate ); if(!f) { f.close() ; delete [] db_matrixA ;
cout << "You dont have a file matrixA.dat" << endl ; return ; }
/* step 8 : convert back to C/C++ double double **db_transposeA; db_transposeA = new double *[row] ; for(i=0; i<row; i++) {
*/
113
mxArray2double_matrixReal(mx_transposeA, db_transposeA) ;
/*
*/ ; ;
mxDestroyArray(mx_A) mxDestroyArray(mx_transposeA)
delete [] db_matrixA
delete [] db_transposeA ; }
end code
int i,j ; /* step 1 : declare matrix */ double **db_matrixA ; int row = 3 ; int col = 3 ;
/* step 2 :
assign memory */ ;
/* step 3 :
*/
114
ifstream f ; f.open("matrixA.dat", ios::in | ios::nocreate ); if(!f) { f.close() ; delete [] db_matrixA ;
cout << "You dont have a file matrixA.dat" << endl ; return ; }
*/
delete [] db_matrixA
delete [] db_transposeA ; }
Chapter 9
We will write the M-les as shown below. These les will be used to generate the C and C++ shared libraries. mydiag.m, myfull.m, mylu.m, mymldivide.m, mymrdivide.m, mysparse.m, and myspdiags.m
function X = mydiag(v,k)
X = diag(v,k) ;
B = A(rowa:rowb, cola:colb) ; % extract from row a to row b, and from col a to col b
A = full(S) ;
[L,U,P] = lu(A) ;
function
S = mysparse(A)
S = sparse(A) ;
function
A = myspdiags(B,d,m,n)
A = spdiags(B,d,m,n)
linearsystemlib_mcc_component_data.c
117 Add and set these les to the MSVC .Net project as described in Chapter 6. 3. In the following sections, well use the following implemental functions in this library to solve the common problems in the linear system equations (open the le linearsystemlib.h to see the names of these functions): void mlfMydiag (int nargout, mxArray** X, mxArray* v, mxArray* k);
void mlfMyextractmatrix(int nargout, mxArray** B, mxArray* A, mxArray* rowa, mxArray* rowb, mxArray* cola, mxArray* colb);
void mlfMyfull
void mlfMylu (int nargout, mxArray** L, mxArray** U, mxArray** P, mxArray* A); void mlfMymldivide(int nargout, mxArray** x, mxArray* A, mxArray* b); void mlfMymrdivide(int nargout, mxArray** x, mxArray* A, mxArray* b); void mlfMysparse (int nargout, mxArray** S, mxArray* A);
void mlfMyspdiags (int nargout, mxArray** A, mxArray* B, mxArray* d, mxArray* m, mxArray* n);
cpplinearsystemlib_mcc_component_data.c
3. In the following sections, well use the following implemental functions in this library to solve the common problems in the linear system equations (open the le cpplinearsystemlib.h to see the names of these functions): void mydiag(int nargout, mwArray& X , const mwArray& v, const mwArray& k);
void myextractmatrix(int nargout, mwArray& B, const mwArray& A , const mwArray& rowa, const mwArray& rowb , const mwArray& cola, const mwArray& colb);
118 void myfull(int nargout, mwArray& A, const mwArray& S); void mylu (int nargout, mwArray& L, mwArray& U , mwArray& P, const mwArray& A); void mymldivide(int nargout, mwArray& x , const mwArray& A, const mwArray& b); void mymrdivide(int nargout, mwArray& x , const mwArray& A, const mwArray& b); void mysparse (int nargout, mwArray& S, const mwArray& A);
void myspdiags (int nargout, mwArray& A, const mwArray& B , const mwArray& d, const mwArray& m, const mwArray& n);
9.1
In general, the form of linear system equations (size n n) is: a11 x1 + a12 x2 + + a1n xn = b1 a21 x1 + a22 x2 + + a2n xn = b2 a31 x1 + a32 x2 + + a3n xn = b3 (9.1)
Problem 1
input
output
. Finding the solution x of linear system equations, Ax = b . Finding the lower L and upper U of the matrix A
Listing code
/* Example.cpp */
int main() {
return 0 ; }
/* Example.h */
class Test {
Test ()
mclInitializeApplication(NULL,0); linearsystemlibInitialize(); }
~Test ()
120
linearsystemlibTerminate(); mclTerminateApplication(); }
double db_A[3][3]
{ {1.1,
5.6, 3.3}
,\ ,\ };
double db_vectorb[3] =
/* step 1 : declare mxArray variables */ mxArray *mx_A = NULL ; mxArray *mx_b = NULL ; mxArray *mx_x = NULL ;
/* step 2 : assign memory */ mx_A = mxCreateDoubleMatrix(row, col, mxREAL) mx_b = mxCreateDoubleMatrix(row, mx_x = mxCreateDoubleMatrix(row, 1 , mxREAL) 1 , mxREAL) ; ; ;
*/
121
double *db_vectorx = new double[col] ; mxArray2double_vectorReal(mx_x, db_vectorx) ;
cout<< *(db_vectorx + i) }
/*
*/ ; ; ;
delete [] db_vectorx ;
double db_A[3][3]
{ {1.1,
5.6, 3.3}
,\ ,\ };
/* step 1 : declare mxArray variables */ mxArray *mx_A = NULL ; mxArray *mx_L = NULL ; mxArray *mx_U = NULL ; mxArray *mx_P = NULL ;
/* step 2 : assign memory */ mx_A = mxCreateDoubleMatrix(row, col, mxREAL) mx_L = mxCreateDoubleMatrix(row, col, mxREAL) mx_U = mxCreateDoubleMatrix(row, col, mxREAL) ; ; ;
122
mx_P = mxCreateDoubleMatrix(row, col, mxREAL) ;
/* step 5 : convert back to C/C++ double double **db_L = new double *[row] ; double **db_U = new double *[row] ;
*/
/* setp 9 : print out */ cout << endl << "The lower matrix:" << endl ; printMatrix(db_L, row, col) ;
cout << endl << "The upper matrix:" << endl ; printMatrix(db_U, row, col) ;
/*
*/ ; ; ; ;
end code
123
Listing code
/* Example.cpp */
int main() {
obj.LU_decompression() ; return 0 ; }
/* Example.h */
class Test {
public:
Test ()
mclInitializeApplication(NULL,0);
124
cpplinearsystemlibInitialize(); }
~Test ()
cpplinearsystemlibTerminate(); mclTerminateApplication(); }
} ;
double db_A[3][3]
{ {1.1,
5.6, 3.3}
,\ ,\ };
double db_vectorb[3] =
/* convert C/C++ matrix to mxArray */ mwArray mw_A = double2mwArray_matrixReal(&db_A[0][0], row, col) ; mwArray mw_vectorb(row, 1, mxDOUBLE_CLASS) ; mw_vectorb.SetData(db_vectorb, row) ;
/* call an implemental function */ mwArray mw_x(row, 1, mxDOUBLE_CLASS) ; mymldivide(1, mw_x, mw_A, mw_vectorb) ;
*/
125
/* print out */ cout << "Solution x: " << endl ; for(i=0; i<col; i++) { << endl ;
cout<< db_vectorx[i] }
/*
free memories
*/
delete [] db_vectorx ;
double db_A[3][3]
{ {1.1,
5.6, 3.3}
,\ ,\ };
/* call an implemental function */ mwArray mw_L (row, col, mxDOUBLE_CLASS) ; mwArray mw_U (row, col, mxDOUBLE_CLASS) ; mwArray mw_P (row, col, mxDOUBLE_CLASS) ;
*/
double **db_L = new double *[row] ; double **db_U = new double *[row] ;
126
}
/* print out */ cout << endl << "The lower matrix:" << endl ; printMatrix(db_L, row, col) ;
cout << endl << "The upper matrix:" << endl ; printMatrix(db_U, row, col) ;
*/
9.2
The sparse linear system is a common system created to solve a technical problem. In this system the main matrix is a sparse matrix (a matrix that has numbers where the nonzero is minor). To obtain an accurate solution and a better computational simulation in the sparse system, MATLAB provided specied functions to handle this task.
Problem 2
input
0 0 0 0 5.5
0 0 0 6.6 0
1.1
0 2.2 A = 3.3 0 0 0 0 0
0 0 0 0
output
127
The following is the code to solve Problem 2 by using the functions, mlfMysparse(..) and mlfMymldivide(..) Listing code
void Test::sparseSystem() /* A = 0 0 3.3 0 0 0 2.2 0 0 0 0 0 0 0 5.5 0 0 0 6.6 0 1.1 0 0 0 0 {
b = */
11.1
22.2
33.3
2.2, 0 0 0 , , ,
{3.3, {0 {0 , ,
6.6, 0 ,
5.5,
double db_vectorb[5] =
{ 11.1, 0,
22.2,
0,
33.3 } ;
/* step 1 : declare mxArray variables */ mxArray *mx_A mxArray *mx_b = NULL ; = NULL ;
128
mxArray *mx_x = NULL ;
/* step 2 : assign memory */ mx_A mx_b mx_x = mxCreateDoubleMatrix(row, col, mxREAL) = mxCreateDoubleMatrix(row, = mxCreateDoubleMatrix(row, 1 , mxREAL) 1 , mxREAL) ; ; ;
/* step 4 : call an implemental function */ mlfMysparse(1, &mx_A, mx_A); mlfMysparse(1, &mx_b, mx_b);
*/
mxArray2double_vectorReal(mx_x, db_vectorx) ;
/* setp 6 : print out */ cout << endl ; cout << "Solution x :" << endl ; for(i=0; i<col; i++) { << endl ;
cout<< *(db_vectorx + i) }
/*
*/ ; ; ;
delete [] db_vectorx ; }
end code
129
The following is the code to solve Problem 2 by using the functions, mysparse(..) and mymldivide(..) Listing code
void Test::sparseSystem() /* A = 0 0 3.3 0 0 0 2.2 0 0 0 0 0 0 0 5.5 0 0 0 6.6 0 1.1 0 0 0 0 {
b = */
11.1
22.2
33.3
2.2, 0 0 0 , , ,
{3.3, {0 {0 , ,
6.6, 0 ,
5.5,
double db_vectorb[5] =
{ 11.1, 0,
22.2,
0,
33.3 } ;
/* note: we create mw_b and mw_x are column vectors */ /* convert C/C++ double to mxArray */ mwArray mw_A = double2mwArray_matrixReal(&db_A[0][0], row, col) ;
130
mwArray mw_b(row, 1, mxDOUBLE_CLASS) ; mw_b.SetData(db_vectorb, row) ;
mwArray mw_x(row, 1, mxDOUBLE_CLASS) ; mymldivide(1, mw_x, mw_A, mw_b); myfull(1, mw_x, mw_x);
*/
mwArray2double_vectorReal(mw_x, db_vectorx) ;
/* print out */ cout << endl ; cout << "Solution x :" << endl ; for(i=0; i<col; i++) {
/* free memories
*/
delete [] db_vectorx ;
end code
131
9.3
This section focuses on nding the solution of tridiagonal linear system equations Ax = d, as follows:
a1
b1 a2 c3 0
0 b2 a3 0 0 cn1 0 b3
an1 cn
c2 0 0 0
x1
d1
(9.2)
Problem 3
input
c 1 c2 c3 B= c4 c5 c6
a1 a2 a3 a4 a5 a6
2.1
output
void mlfMyspdiags (int nargout, mxArray** bufferA, mxArray* B, mxArray* d, mxArray* m, mxArray* n);
132 This function mlfMyspdiags(..) creates an m-by-n sparse matrix buerA by taking the columns of B and placing them along the diagonals specied by d as follows: c1 a1 b1 0 0 0 0 c2 a2 b2 0 0 0 0 c3 a3 b3 0 0 buerA = 0 0 0 cn1 an1 bn1 0 0 0 0 0 cn an bn 2. Obtaining the matrix A as in Eq. 9.2 by extracting from the matrix buerA 3. Using the functions in the library linearsytemlib to solve the tridiagonal linear system equations.
(9.4)
The following is the code to solve Problem 3 by using the functions in the library linearsytemlib.
Listing code
void Test::tridiagonalSystem()
double B[6][3] ; B[0][0] = B[1][0] = B[2][0] = B[3][0] = B[4][0] = B[5][0] = 1.1 1.2 1.3 1.4 1.5 1.6 ; ; ; ; ; ;
/* columns 2 */ B[0][1] = B[1][1] = B[2][1] = B[3][1] = B[4][1] = B[5][1] = 4.1 4.2 4.3 4.4 4.5 4.6 ; ; ; ; ; ;
133
B[2][2] = B[3][2] = B[4][2] = B[5][2] = 2.3 2.4 2.5 2.6 ; ; ; ;
/* tridiagnal */
mxArray *mx_one
= NULL ;
134
mxArray *mx_two mxArray *mx_seven mxArray *mx_row = NULL ; = NULL ; = NULL ;
/* step 2 : assign memory */ mx_B mx_bufferA mx_A = mxCreateDoubleMatrix( rowB , = mxCreateDoubleMatrix( m , , colB , mxREAL) n , mxREAL) ; ; ;
= mxCreateDoubleMatrix( row
col , mxREAL)
; ; ;
; ; ; ;
mx_vectord mx_x
= mxCreateDoubleMatrix(row, = mxCreateDoubleMatrix(row,
1 , mxREAL) 1 , mxREAL)
; ;
double2mxArray_vectorReal(d , mx_d
) ;
double2mxArray_matrixReal(&B[0][0] , mx_B) ;
double2mxArray_vectorReal(db_vectord, mx_vectord) ;
135
/* step 4 : call an implemental function */
*/
mlfMyspdiags (1, &mx_bufferA, mx_B, mx_d , mx_m, mx_n); mlfMyfull(1, &mx_bufferA, mx_bufferA) ;
/* plot to see */ double **db_bufferA = new double *[m] ; for(i=0; i<m; i++) {
mxArray2double_matrixReal(mx_bufferA, db_bufferA) ;
cout << endl << "The buffer matrix A:" << endl ; printMatrix(db_bufferA, m, n) ;
/* extract the need-matrix from the buffter matrix, from row 1 to row 6 and from column 2 to column 7 */ mlfMyextractmatrix(1, &mx_A, mx_bufferA, mx_one, mx_row, mx_two, mx_seven);
/* plot to see */ double **db_A = new double *[row] ; for(i=0; i<row; i++) {
mxArray2double_matrixReal(mx_A, db_A) ;
cout << endl << "The need-matrix A:" << endl ; printMatrix(db_A, row, col) ;
136
/* step 5 : convert back to C/C++ double double *db_vectorx = new double[col] ; mxArray2double_vectorReal(mx_x, db_vectorx) ; */
/* step 6 : print out */ cout << "Tridiagnal system solution:" << endl ; for(i=0; i<row; i++) { << endl ;
cout<< *(db_vectorx + i) }
/*
*/
) ; ) ; ) ;
mxDestroyArray(mx_one mxDestroyArray(mx_two
) ; ) ;
mxDestroyArray(mx_seven ) ; mxDestroyArray(mx_row ) ;
mxDestroyArray(mx_vectord) ; mxDestroyArray(mx_x ) ;
delete [] db_vectorx ;
end code
137
columns of B and placing them along the diagonals specied by d as follows: c1 a1 b1 0 0 0 0 c2 a2 b2 0 0 0 0 c3 a3 b3 0 0 buerA = 0 0 0 cn1 an1 bn1 0 0 0 0 0 cn an bn 2. Obtaining the matrix A as in Eq. 9.2 by extracting from the matrix buerA
(9.5)
3. Using the functions in the library cpplinearsytemlib to solve the tridiagonal linear system equations.
The following is the code to solve Problem 3 by using the functions in the library cpplinearsytemlib. Listing code
void Test::tridiagonalSystem()
double B[6][3] ; B[0][0] = B[1][0] = B[2][0] = B[3][0] = B[4][0] = B[5][0] = 1.1 1.2 1.3 1.4 1.5 1.6 ; ; ; ; ; ;
/* columns 2 */
138
B[0][1] = B[1][1] = B[2][1] = B[3][1] = B[4][1] = B[5][1] = 4.1 4.2 4.3 4.4 4.5 4.6 ; ; ; ; ; ;
/* columns 3 */ B[0][2] = B[1][2] = B[2][2] = B[3][2] = B[4][2] = B[5][2] = 2.1 2.2 2.3 2.4 2.5 2.6 ; ; ; ; ; ;
int i ;
/* tridiagnal */
139
/* convert C/C++ double to mxArray */ mwArray mw_B = double2mwArray_matrixReal(&B[0][0], rowB, colB) ;
/* create a sparse matrix, size mxn, from column-matrix B mwArray mw_m(1, 1, mxDOUBLE_CLASS) ; mw_m = m ;
*/
mwArray mw_bufferA(m, n, mxDOUBLE_CLASS) ; myspdiags (1, mw_bufferA, mw_B, mw_d , mw_m, mw_n);
/* plot to see */ cout << endl << "The buffer matrix A:" << endl ; std::cout << mw_bufferA << std::endl ;
/* extract the need-matrix A from the buffter matrix, from row 1 to row 6 and from column 2 to column 7 */ mwArray mw_A(row, col, mxDOUBLE_CLASS) ;
140
mwArray mw_seven(1, 1, mxDOUBLE_CLASS) ; mw_seven = 7;
/* plot to see */ cout << endl << "The need-matrix A:" << endl ; std::cout << mw_A << std::endl ;
/* solve the tridiagnal system equations */ mwArray mw_x(row, 1, mxDOUBLE_CLASS) ; mymldivide(1, mw_x, mw_A, mw_vectord);
*/
/* print out */ cout << "Tridiagnal system solution:" << endl ; for(i=0; i<row; i++) { << endl ;
cout<< db_vectorx[i] }
/* free memories
*/
delete [] db_vectorx ;
end code
9.4
The band diagonal system is a common system in engineering applications. The band diagonal matrix is a matrix with nonzero elements existing only along a few diagonal lines adjacent to the main diagonal (above and below). This section is a study of nding the solution of band diagonal system equations where the width = 4. This system is Ax = d as follows:
c2 0 0 0 0
The procedure to solve band diagonal system equations is similar to the procedure to solve tridiagonal system equations. Problem 4 input Matrix B includes columns c, a, b, and e. Vector d
c 1 c2 c3 B= c4 c5 c6
a1 a2 a3 a4 a5 a6
b1 b2 b3 b4 b5 b6
7.1
output
The steps to solve this problem are similar to the steps in the tridiagnal problem. These steps are: 1. Establishing a buer matrix buerA (in Eq. 9.8) from given matrix B (in Eq. 9.7) by using a function in the library. c a1 b 1 1 0 c2 a2 0 0 c3 buerA = 0 0 0 0 0 0
e1 b2 a3 0 0 0
0 e2 b3 0 e3
0 0 0 en2 bn1 an
0 0 0 0 en1 bn
cn2 0 0
an2 cn1 0
bn2 an1 cn
0 0 0 0 en
(9.8)
142 2. Obtaining the matrix A as in Eq. 9.6 by extracting from the matrix buerA. 3. Using the functions in the library to solve the band system diagnal equations.
double B[6][4] ;
/* columns 1 */ B[0][0] = B[1][0] = B[2][0] = B[3][0] = B[4][0] = B[5][0] = 1.1 1.2 1.3 1.4 1.5 1.6 ; ; ; ; ; ;
/* columns 2 */ B[0][1] = B[1][1] = B[2][1] = B[3][1] = B[4][1] = B[5][1] = 4.1 4.2 4.3 4.4 4.5 4.6 ; ; ; ; ; ;
/* columns 3 */ B[0][2] = B[1][2] = B[2][2] = B[3][2] = B[4][2] = B[5][2] = 2.1 2.2 2.3 2.4 2.5 2.6 ; ; ; ; ; ;
143
B[4][3] = B[5][3] = 7.5 7.6 ; ;
/* band width */
144
mxArray *mx_row = NULL ;
/* step 2 : assign memory */ mx_B mx_bufferA mx_A = mxCreateDoubleMatrix( rowB , = mxCreateDoubleMatrix( m , , colB , mxREAL) n , mxREAL) ; ; ;
= mxCreateDoubleMatrix( row
col , mxREAL)
; ; ;
; ; ; ;
mx_vectord mx_x
= mxCreateDoubleMatrix(row, = mxCreateDoubleMatrix(row,
1 , mxREAL) 1 , mxREAL)
; ;
double2mxArray_vectorReal(d , mx_d
) ; ) ;
double2mxArray_matrixReal(&B[0][0] , mx_B
double2mxArray_vectorReal(db_vectord, mx_vectord) ;
145
/* create a sparse matrix, size mxn, from column-matrix B mlfMyspdiags (1, &mx_bufferA, mx_B, mx_d , mx_m, mx_n); mlfMyfull(1, &mx_bufferA, mx_bufferA) ; */
/* plot to see */ double **db_bufferA = new double *[m] ; for(i=0; i<m; i++) {
mxArray2double_matrixReal(mx_bufferA, db_bufferA) ;
cout << endl << "The buffer band-matrix A:" << endl ; printMatrix(db_bufferA, m, n) ;
/* extract the need-matrix A from the buffter matrix, from row 1 to row 6 and from column 2 to column 7 */ mlfMyextractmatrix(1, &mx_A, mx_bufferA, mx_one, mx_row, mx_two, mx_seven);
/* plot to see */ double **db_A = new double *[row] ; for(i=0; i<row; i++) {
mxArray2double_matrixReal(mx_A, db_A) ;
cout << endl << "The band-need-matrix A:" << endl ; printMatrix(db_A, row, col) ;
*/
146
mxArray2double_vectorReal(mx_x, db_vectorx) ;
/* step 6 : print out */ cout << "Band matrix system solution:" << endl ; for(i=0; i<row; i++) { << endl ;
cout<< *(db_vectorx + i) }
/*
*/
) ; ) ; ) ;
mxDestroyArray(mx_one mxDestroyArray(mx_two
) ; ) ;
mxDestroyArray(mx_seven ) ; mxDestroyArray(mx_row ) ;
mxDestroyArray(mx_vectord) ; mxDestroyArray(mx_x ) ;
delete [] db_vectorx ;
end code
147
Listing code
void Test::bandMatrixSystem()
double B[6][4] ;
/* columns 1 */ B[0][0] = B[1][0] = B[2][0] = B[3][0] = B[4][0] = B[5][0] = 1.1 1.2 1.3 1.4 1.5 1.6 ; ; ; ; ; ;
/* columns 2 */ B[0][1] = B[1][1] = B[2][1] = B[3][1] = B[4][1] = B[5][1] = 4.1 4.2 4.3 4.4 4.5 4.6 ; ; ; ; ; ;
/* columns 3 */ B[0][2] = B[1][2] = B[2][2] = B[3][2] = B[4][2] = B[5][2] = 2.1 2.2 2.3 2.4 2.5 2.6 ; ; ; ; ; ;
/* columns 4 */ B[0][3] = B[1][3] = B[2][3] = B[3][3] = B[4][3] = 7.1 7.2 7.3 7.4 7.5 ; ; ; ; ;
148
B[5][3] = 7.6 ;
int i ;
/* band width */
/* note: we create mw_vectord and mw_x as column vectors */ mwArray mw_B = double2mwArray_matrixReal(&B[0][0], rowB, colB) ;
/* create a sparse matrix, size mxn, from column-matrix B mwArray mw_m(1, 1, mxDOUBLE_CLASS) ; mw_m = m ;
*/
149
mw_n = n ;
mwArray mw_bufferA(m, n, mxDOUBLE_CLASS) ; myspdiags (1, mw_bufferA, mw_B, mw_d , mw_m, mw_n);
/* plot to see */ cout << endl << "The buffer band-matrix A:" << endl ; std::cout << mw_bufferA << std::endl ;
/* extract the need-matrix A from the buffter matrix, from row 1 to row 6 and from column 2 to column 7 */ mwArray mw_A(row, col, mxDOUBLE_CLASS) ;
/* plot to see */ cout << endl << "The band-need-matrix A:" << endl ; std::cout << mw_A << std::endl ;
/* solve the tridiagnal system equations */ mwArray mw_x(row, 1, mxDOUBLE_CLASS) ; mymldivide(1, mw_x, mw_A, mw_vectord);
*/
150
mwArray2double_vectorReal(mw_x, db_vectorx) ;
/* print out */ cout << "Band matrix system solution:" << endl ; for(i=0; i<row; i++) { << endl ;
cout<< db_vectorx[i] }
/* free memories
*/
delete [] db_vectorx ;
end code
Note: 1. If all elements in each column of the matrix B are equal (for example, one case shown in Eq.9.9), you can create directly the matrix A without through the buer matrix buerA as follows:
c 1 c2 c3 B= c4 c5 c6
a1 a2 a3 a4 a5 a6
1 b2 1 b3 1 = b4 1 b5 1 b6 1 b1
4 4 4 4 4 4
1 1 1 1 1
(9.9)
2. The matrix A can also be created by using the MATLAB function diags(..) [5].
Chapter 10
The major MATLAB function of M-les used to generate the libraries to solve ODE problems is ode45(..). There are another functions, ode23(..), ode113(..), ode15s(..), and ode23s(..), can be used to solve the ODE problems. Therefore you can choose a function with an options that satises your problem requirements. For more information on these functions, refer to the manual [7].
Following are steps to create a C shared library odelib.dll and a C++ shared library cppodelib.dll which will be used to solve ODE problems in the next sections. We will write the M-les as shown below. These functions will be used to generate the C and C++ shared libraries. myode45firstorder.m, yourfunc.m myode45secondorder.m, yoursecondfunc.m
% example: % y - 2y -6y = cos(3t) % y = cos(3t) + 2y + 6y % write an expression string with replace y by yprime: % cos(3*t) + 2*yprime + 6*y
%trick for a function with/without t, yprime, y strfunction = strcat(strfunc, + 0*t + 0*yprime + 0*y) ;
153
1. Write the command in Windows Command Prompt as follows to generate the C shared library odelib: mcc -B csharedlib:odelib myode45rstorder.m myode45secondorder.m 2. MATLAB Compiler 4.0 will create eight les for this C shared library: odelib.c odelib.exp odelib.lib odelib.ctf odelib.exports odelib.dll odelib.h
odelib_mcc_component_data.c
Add and set these les to the MSVC .Net project as described in Chapter 6. 3. In the following sections, well use the following implemental functions in this library to solve the common ODE problems (open the le odelib.h to see the names of these functions): void mlfMyode45firstorder(int nargout, mxArray** t, mxArray** y , mxArray** lengthtime, mxArray* strfunc , mxArray* tspan, mxArray* y0);
void mlfMyode45secondorder(int nargout, mxArray** t, mxArray** y , mxArray** lengthtime, mxArray* strfunc , mxArray* tspan, mxArray* y0);
1. Write the command in Windows Command Prompt as follows to generate the C shared library cppodelib: mcc -W cpplib:cppodelib -T link:lib myode45rstorder.m myode45secondorder.m 2. MATLAB Compiler 4.0 will create eight les for this C shared library: cppodelib.cpp cppodelib.exp cppodelib.lib cppodelib.ctf cppodelib.exports cppodelib.dll cppodelib.h
cppodelib_mcc_component_data.c
Add and set these les to the MSVC .Net project as described in Chapter 6.
154 3. In the following sections, well use the following implemental functions in this library to solve the common ODE problems (open the le cppodelib.h to see the names of these functions): void myode45firstorder(int nargout, mwArray& t, mwArray& y , mwArray& lengthtime, const mwArray& strfunc , const mwArray& tspan, const mwArray& y0);
void myode45secondorder(int nargout, mwArray& t, mwArray& y , mwArray& lengthtime, const mwArray& strfunc , const mwArray& tspan, const mwArray& y0);
10.1
Problem 1
Note: 1. In solving rst order ODE problems, the MATLAB function ode45(..) has an input argument that is an interval tspan=[a, b], and the function outputs are two arrays: array t[ ] contains the values of time t, t [a, b] array y[ ] contains the values of the function y(t) The beginning of the interval is given, a = to . The end of the interval, b, is chosen by the user to show the time range in the problem. 2. The time step is set to the default if you do not provide a time step. 3. The time step can be set by providing tspan=[t0 , t2 , . . . , tn ] as a vector including the values of time. The output value y will be a column vector. Each row in the solution array y corresponds to the time in the column vector tspan. 4. In the following code, well call the implement function mlfMyode45rstorder(..) or myode45rstorder(..) twice. First time, the function is called to obtain the size of the vector t . Second time, the function is called to get values of t and y.
155 5. The ODE function is passed as an expression string to the generated function mlfMyode45rstorder(..) or myode45rstorder(..) which is a function-function and has an argument as an expression string. The form of this expression string follows the rule of a MATLAB expression string.
Listing code
/* Example.cpp */
int main() {
Test obj ; cout << "First order ODE." << endl obj.FirstOrder() ; ;
return 0 ; }
/* Example.h */
class Test {
public:
156
void FirstOrder() ;
Test ()
mclInitializeApplication(NULL,0); odelibInitialize(); }
~Test ()
odelibTerminate(); mclTerminateApplication(); }
} ;
/* Calculating first order ODE */ const char //const char strfunc[] = "cos(t)" ; strfunc[] = "2+y" ;
; /* initial condition at t0
*/
int i ;
/* step 1 : declare mxArray variables */ mxArray *mx_strfunc ; mxArray *mx_y0 mxArray *mx_tspan = NULL ; = NULL ;
= NULL ; = NULL ;
157
/* step 2 : assign memory */ mx_y0 mx_tspan mx_length = mxCreateDoubleMatrix(1, 1, mxREAL) = mxCreateDoubleMatrix(2, 1, mxREAL) = mxCreateDoubleMatrix(1, 1, mxREAL) ; ; ;
mx_dum01 mx_dum02
; ;
double2mxArray_scalarReal (db_y0
, mx_y0
) ; ) ;
mlfMyode45firstorder(3, &mx_dum01, &mx_dum02, &mx_length, mx_strfunc, mx_tspan, mx_y0); int int_length = (int)mxArray2double_scalarReal(mx_length) ;
cout << "Length = " << int_length << endl; mx_t mx_y = mxCreateDoubleMatrix(int_length, 1, mxREAL) = mxCreateDoubleMatrix(int_length, 1, mxREAL) ; ;
/* solve the problem */ mlfMyode45firstorder(3, &mx_t, &mx_y, &mx_length, mx_strfunc, mx_tspan, mx_y0);
/* step 5 : convert back to C/C++ double double* db_t = new double[int_length] ; double* db_y = new double[int_length] ;
*/
cout << "The column of time" << endl ; for (i=0 ; i<int_length; i++) cout << db_t[i] << endl ; } {
cout << "The column of the function values y" << endl ;
158
for (i=0 ; i<int_length; i++) cout << db_y[i] << endl ; } {
*/
mxDestroyArray(mx_t) mxDestroyArray(mx_y)
; ; ;
mxDestroyArray(mx_length)
mxDestroyArray(mx_dum01) mxDestroyArray(mx_dum02)
; ;
; ;
end code
Listing code
/* Example.cpp */
int main() {
159
obj.FirstOrder() ;
return 0 ; }
/* Example.h */
class Test {
Test ()
mclInitializeApplication(NULL,0); cppodelibInitialize(); }
~Test ()
cppodelibTerminate(); mclTerminateApplication(); }
/* Calculating first order ODE */ int i ; mwArray mw_y0(1, 1, mxDOUBLE_CLASS) ; mw_y0(1,1) = 2.2 ;
mwArray mw_tspan(2, 1, mxDOUBLE_CLASS) ; mw_tspan(1,1) = 0.2 ; /* begin interval t0 = 0.2 mw_tspan(2,1) = 6.5 ; /* end interval, we choose this */ */
160
mwArray mw_dum01(100, 1, mxDOUBLE_CLASS) ; mwArray mw_dum02(100, 1, mxDOUBLE_CLASS) ;
/* call an implemental function */ /* get size of the vector t */ mwArray mw_length(1, 1, mxDOUBLE_CLASS) ; myode45firstorder(3, mw_dum01, mw_dum02, mw_length, mw_strfunc, mw_tspan, mw_y0);
int int_length = (int)mw_length(1,1) ; cout << "Length = " << int_length << endl;
/* solve the problem */ myode45firstorder(3, mw_t, mw_y, mw_length, mw_strfunc, mw_tspan, mw_y0);
*/
cout << "The column of time" << endl ; for (i=0 ; i<int_length; i++) cout << db_t[i] << endl ; } {
cout << "The column of the function values y" << endl ; for (i=0 ; i<int_length; i++) cout << db_y[i] << endl ; } {
161
/* free memories delete [] db_t delete [] db_y */ ; ;
end code
Problem 2
This Problem 2 is solved similarly to Problem 1. In the code we just change the expression string: const char Remark To nd particular function values we need to set the argument tspan as a column vector including the nding time. For example, the following code to nd the particular values y at t = 0.15, 0.2, 2.6, and 5.0 in Problem 2. strfunc[] = "6.4*t.^2 - 3.8*t*y" ;
void Test::FirstOrderGetParticularValues()
; /* initial condition at t0
*/
double db_tspan[4] db_tspan[0] = 0.15 db_tspan[1] = 0.2 db_tspan[2] = 2.6 db_tspan[3] = 5.0
; ; /* begin interval t0 = 0.15 ; /* choose a particular time t = 0.2 ; /* choose a particular time t = 2.6 ; /* choose a particular time t = 5.0 */ */ */ */
162
int i ;
/* step 1 : declare mxArray variables */ mxArray *mx_strfunc ; mxArray *mx_y0 mxArray *mx_tspan = NULL ; = NULL ;
= NULL ; = NULL ;
/* step 2 : assign memory */ mx_y0 mx_tspan mx_length = mxCreateDoubleMatrix(1, 1, mxREAL) = mxCreateDoubleMatrix(4, 1, mxREAL) = mxCreateDoubleMatrix(1, 1, mxREAL) ; ; ;
mx_dum01 mx_dum02
; ;
double2mxArray_scalarReal (db_y0
, mx_y0
) ; ) ;
mlfMyode45firstorder(3, &mx_dum01, &mx_dum02, &mx_length, mx_strfunc, mx_tspan, mx_y0); int int_length = (int)mxArray2double_scalarReal(mx_length) ;
cout << "Length = " << int_length << endl; mx_t mx_y = mxCreateDoubleMatrix(int_length, 1, mxREAL) = mxCreateDoubleMatrix(int_length, 1, mxREAL) ; ;
/* solve the problem */ mlfMyode45firstorder(3, &mx_t, &mx_y, &mx_length, mx_strfunc, mx_tspan, mx_y0);
163
/* step 5 : convert back to C/C++ double double* db_t = new double[int_length] ; double* db_y = new double[int_length] ;
*/
cout << "The column of time" << endl ; for (i=0 ; i<int_length; i++) cout << db_t[i] << endl ; } {
cout << "The column of the function values y" << endl ; for (i=0 ; i<int_length; i++) cout << db_y[i] << endl ; } {
*/
mxDestroyArray(mx_t) mxDestroyArray(mx_y)
; ; ;
mxDestroyArray(mx_length)
mxDestroyArray(mx_dum01) mxDestroyArray(mx_dum02)
; ;
; ;
end code
164
void Test::FirstOrderGetParticularValues()
/* Calculating first order ODE */ int i ; mwArray mw_y0(1, 1, mxDOUBLE_CLASS) ; mw_y0(1,1) = 1.24 ;
mwArray mw_tspan(4, 1, mxDOUBLE_CLASS) ; mw_tspan(1,1) = 0.2 ; /* begin interval t0 = 0.2 mw_tspan(2,1) = 6.5 ; /* end interval, we choose this */ */
; /* begin interval t0 = 0.15 ; /* choose a particular time t = 0.2 ; /* choose a particular time t = 2.6 ; /* choose a particular time t = 5.0
*/ */ */ */
/* call an implemental function */ /* get size of the vector t */ mwArray mw_length(1, 1, mxDOUBLE_CLASS) ; myode45firstorder(3, mw_dum01, mw_dum02, mw_length, mw_strfunc, mw_tspan, mw_y0);
int int_length = (int)mw_length(1,1) ; cout << "Length = " << int_length << endl;
/* solve the problem */ myode45firstorder(3, mw_t, mw_y, mw_length, mw_strfunc, mw_tspan, mw_y0);
165
*/
cout << "The column of time" << endl ; for (i=0 ; i<int_length; i++) cout << db_t[i] << endl ; } {
cout << "The column of the function values y" << endl ; for (i=0 ; i<int_length; i++) cout << db_y[i] << endl ; } {
*/ ; ;
end code
10.2
Problem 3
166
10.2.1
1. To solve Problem 3 by writing M-les, we can write an M-le mysecondfunc.m as follows: function dy = mysecondfunc(t, y)
dy = [y(2) ; cos(3*t) + 2*y(2) + 6*y(1)] ; and in MATLAB Command Window write: >> tspan = [1.2 ; 2.5] ; >> ybc = [0.2 ; 1.1] ;
>> [t,y] = ode45(@mysecondfunc, tspan, ybc) 2. To explain the code in the M-le mysecondfunc.m, we rewrite and set from the provided equation : y = y1 y = y2 y = y2 Problem 3 then becomes: y = y1 y = y2 y = cos(3t) + 2y + 6y = cos(3t) + 2y2 + 6y1 This is the second expression in the M-le mysecondfunc.m. 3. The function mysecondfunc(..) which is passed to the ode function ode45(..) has a return including two arrays: First array is the rst derivative of the function y, as y(2) Second array is the second derivative of the function y, as cos(3*t) + 2*y(2) + 6*y(1) ; The M-le yoursecondfunc.m, with which we use for creating a C shared library odelib or a C++ library cppodelib, as shown in above, also has a return including two arrays: First array is the rst derivative of the function y, as follows: f0 = inline(yy) ; dy(1,:) = feval( f0, y(2) ) ;
167 Second array is the second derivative of the function y, as follows: cos(3*t) + 2*y(2) + 6*y(1) ; This second array is represented in the code: f1 = inline(strfunction) ; dy(2,:) = feval( f1, t , y(1), y(2) ) ;
10.2.2
As explain in above, we have an easy way to use the generated function mlfMyode45secondorder(..) in the C shared library odelib or myode45secondorder(..) in the C++ shared library cppodelib to solve second order ODE problems by following the steps: 1. Write your ode-function with second derivative in the left-hand-side, for example: y = cos(3t) + 2y + 6y 2. Rewrite your ode-function as an MATLAB expression string, for example: y = cos(3*t) + 2*y + 6*y 3. Replace y by yprime, for example: y = cos(3*t) + 2*yprime + 6*y 4. Use the right-hand-side as a string to use in the code, for example: constchar strfunc[] = "cos(3*t) + 2*yprime + 6*y" ;
Listing code
void Test::SecondOrder()
/* Calculating second order ODE */ //const char const char strfunc[] = "cos(t)" ; strfunc[] = "cos(3*t) + 2*yprime + 6*y" ;
*/ */ */
; /* initial condition of y at t0
double db_tspan[2]
168
db_tspan[0] = 1.2 db_tspan[1] = 2.5 ; /* begin interval t0 = 1.2 ; /* end interval, we choose this */ */
int i ;
/* step 1 : declare mxArray variables */ mxArray *mx_strfunc ; mxArray *mx_ybc mxArray *mx_tspan = NULL ; = NULL ;
= NULL ; = NULL ;
/* step 2 : assign memory */ mx_ybc mx_tspan mx_length = mxCreateDoubleMatrix(2, 1, mxREAL) = mxCreateDoubleMatrix(2, 1, mxREAL) = mxCreateDoubleMatrix(1, 1, mxREAL) ; ; ;
mx_dum01 mx_dum02
; ;
, mx_ybc
/* step 4 : call an implemental function */ /* get size of the vector t */ mlfMyode45secondorder(3, &mx_dum01, &mx_dum02, &mx_length, mx_strfunc, mx_tspan, int int_length = (int)mxArray2double_scalarReal(mx_length) ; mx_ybc);
mx_t mx_y
; ;
169
mlfMyode45secondorder(3, &mx_t, &mx_y, &mx_length, mx_strfunc, mx_tspan, mx_ybc);
*/ ;
mxArray2double_vectorReal(mx_t, db_t) ;
double** db_y
; ; {
cout << "The column of time" << endl ; for (i=0 ; i<int_length; i++) cout << db_t[i] << endl ; } {
cout << "The column of the function values y :" << endl ; /* first column of the matrix y */ for (i=0 ; i<int_length; i++) {
cout << "The column of the first derivative y :" << endl ; /* second column of the matrix y */ for (i=0 ; i<int_length; i++) {
*/
mxDestroyArray(mx_t)
170
mxDestroyArray(mx_y) ; ;
mxDestroyArray(mx_length)
mxDestroyArray(mx_dum01) mxDestroyArray(mx_dum02)
; ;
; ;
end code
Listing code
/* Calculating second order ODE */ int i ; mwArray mw_ybc(2, 1, mxDOUBLE_CLASS) ; mw_ybc(1,1) = 0.2 ; mw_ybc(2,1) = 1.1 ;
mwArray mw_tspan(2, 1, mxDOUBLE_CLASS) ; mw_tspan(1,1) = 1.2 ; /* begin interval t0 = 0.2 mw_tspan(2,1) = 2.5 ; /* end interval, we choose this */ */
/* call an implemental function */ /* get size of the vector t */ mwArray mw_length(1, 1, mxDOUBLE_CLASS) ; myode45secondorder(3, mw_dum01, mw_dum02, mw_length, mw_strfunc, mw_tspan, mw_ybc);
171
int int_length = (int)mw_length(1,1) ; cout << "Length = " << int_length << endl;
/* solve the problem */ myode45secondorder(3, mw_t, mw_y, mw_length, mw_strfunc, mw_tspan, mw_ybc);
*/ ;
= new double[int_length]
mwArray2double_vectorReal(mw_t, db_t) ;
double** db_y
; ; {
cout << "The column of time" << endl ; for (i=0 ; i<int_length; i++) cout << db_t[i] << endl ; } {
cout << "The column of the function values y :" << endl ; /* first column of the matrix y */ for (i=0 ; i<int_length; i++) {
cout << "The column of the first derivative y :" << endl ; /* second column of the matrix y */ for (i=0 ; i<int_length; i++) {
/* free memories
*/
172
delete [] db_t delete [] db_y ; ;
1. In solving second order ODE problem, the output matrix y includes two columns: The rst column is values of the function y(t) and the second column is values of the rst derivative y (t). 2. In this chapter we describe the methods to solve ODE problems by passing your odefunction to the C/C++ code. These methods are useful when your function are changing in the run-time or your function is provided in an application. If your ode-function is known in the design time, you can call directly. For example, the M-le myotherode.m as follow below will directly call the M-le mysecondfunc.m: function dy = mysecondfunc(t, y) dy = [y(2) ; cos(3*t) + 2*y(2) + 6*y(1)] ;
Chapter 11
Integration
In this chapter well generate a C shared library integrationlib and a C++ shared library cppintegrationlib from common M-les working on problems of single and double integrations. The generated functions of these libraries will be used in a MSVC .Net project to solve the integral problems.
Following are steps to create a C shared library integrationlib.dll and a C++ shared library cppintegrationlib.dll which will be used to solve integral problems in the next sections.
We will write the M-les myquad.m and mydblquad.m. These functions will be used to generate the C and C++ shared libraries.
function y = myquad(strfunc, a, b)
F = inline(strfunc) ; y = quad(F, a, b) ;
F = inline(strfunc)
174
integrationlib_mcc_component_data.c
Add and set these les to the MSVC .Net project as described in Chapter 6. In the following sections, well use the following implemental functions in this library to solve the common problems in the integration (open the le integrationlib.h to see the names of these functions): void mlfMyquad(int nargout, mxArray** y, mxArray* strfunc , mxArray* a, mxArray* b);
void mlfMydblquad(int nargout, mxArray** dbint , mxArray* strfunc, mxArray* x1 , mxArray* x2, mxArray* y1, mxArray* y2);
cppintegrationlib_mcc_component_data.c
Add and set these les to the MSVC .Net project as described in Chapter 6. In the following sections, well use the following implemental functions in this library to solve the common problems in the integration (open the le cppintegrationlib.h to see the names of these functions):
175 void myquad(int nargout, mwArray& y, const mwArray& strfunc , const mwArray& a, const mwArray& b);
void mydblquad(int nargout, mwArray& dbint, const mwArray& strfunc , const mwArray& x1, const mwArray& x2 , const mwArray& y1, const mwArray& y2);
11.1
Single Integration
Calculate the integration :
Problem 1
I=
0
sin(x) + x2 dx
Listing code
/* Example.cpp */
int main() {
cout << "Single integration:" << endl; Test obj ; obj.singleIntegration() ; cout << endl;
return 0 ; }
176
/* Example.h */
class Test {
Test ()
mclInitializeApplication(NULL,0); integrationlibInitialize(); }
~Test ()
integrationlibTerminate(); mclTerminateApplication(); }
} ;
/* **************************** */
177
mxArray *mx_endInterval mxArray *mx_y = NULL ; = NULL ;
/* step 2 : assign memory */ mx_beginInterval mx_endInterval mx_y = mxCreateDoubleMatrix(1, 1, mxREAL) = mxCreateDoubleMatrix(1, 1, mxREAL) = mxCreateDoubleMatrix(1, 1, mxREAL) ; ; ;
/* step 5 : convert back to C/C++ double double db_y ; db_y = mxArray2double_scalarReal(mx_y) ; cout << " I = " << db_y ;
*/
*/ ; ; ; ;
end code
int main() {
return 0 ; }
/* Example.h */
Test ()
mclInitializeApplication(NULL,0); cppintegrationlibInitialize(); }
~Test ()
cppintegrationlibTerminate(); mclTerminateApplication(); }
} ;
179
/* **************************** */ void Test::singleIntegration() {
/* call an implemental function */ mwArray mw_y(1, 1, mxDOUBLE_CLASS) ; myquad(1, mw_y, mw_strfunc, mw_beginInterval, mw_endInterval);
*/
double db_y = (double) mw_y(1,1) ; cout << " I = " << db_y << endl ;
end code Note 1. The generated function mlfMyquad(..) or myquade(..) is a function-function and has an argument as an expression string. The form of this expression string follows the rule of a MATLAB expression string. 2. See the MATLAB function quad(..) for the other method to calculate the single integration.
11.2
Double-Integration
Calculate the double-integration:
Problem 2
I=
0 0
sin(x) + x2 + y 3 dx dy
180
Listing code
void Test::dbIntegration() {
const char
/* step 1 : declare mxArray variables */ mxArray *mx_strfunc ; mxArray *mx_x1 = NULL ; mxArray *mx_x2 = NULL ;
mxArray *mx_II
= NULL ;
; ;
181
mx_strfunc = mxCreateString(strfunc) ;
/* step 4 : call an implemental function */ mlfMydblquad(1, &mx_II, mx_strfunc, mx_x1, mx_x2, mx_y1, mx_y2) ;
/* step 5 : convert back to C/C++ double double db_II ; db_II = mxArray2double_scalarReal(mx_II) ; cout << " II = " << db_II ;
*/
*/
mxDestroyArray(mx_y1 ) ; mxDestroyArray(mx_y2 ) ;
mxDestroyArray(mx_II
) ;
end code
182
mw_x1 = 0 ;
/* call an implemental function */ mwArray mw_II(1, 1, mxDOUBLE_CLASS) ; mydblquad(1, mw_II, mw_strfunc, mw_x1, mw_x2, mw_y1, mw_y2) ;
*/
double db_II = (double) mw_II(1,1) ; cout << " II = " << db_II ;
end code
Chapter 12
Following are steps to create a C shared library curvettinglib.dll and a C++ shared library cppcurvettinglib.dll which will be used to solve curve tting problems in the next sections.
We will write the M-les as shown below. These functions will be used to generate the C and C++ shared libraries. myinterp1.m, myinterp2.m, mypolyfit.m, mypolyval.m, mymeshgrid.m, mygriddata.m, and myfinemeshgrid.m
function yi = myinterp1(x,y,xi)
yi = interp1(x,y,xi) ;
function ZI = myinterp2(X,Y,Z,XI,YI,method)
ZI = interp2(X,Y,Z,XI,YI,method) ;
function p = mypolyfit(x,y,n)
p = polyfit(x,y,n) ;
184
function y = mypolyval(p,x)
y = polyval(p,x) ;
%Using two colons to create a vector with increments between %first and end elements.
function ZI = mygriddata(x,y,z,XI,YI)
ZI = griddata(x,y,z,XI,YI) ;
%Using two colons to create a vector with increments between %first and end elements.
curvefittinglib_mcc_component_data.c
Add and set these les to the MSVC .Net project as described in Chapter 6. Note: In this chapter we use typical functions to solve the particular problems. There are another options and MATLAB functions to solve the curve tting problems, refer to [7]. 3. In the following sections, well use the following implemental functions in this library to solve the common curve tting problem (open the le curvettinglib.h to see the names of these functions): void mlfMyinterp1(int nargout, mxArray** yi, mxArray* x , mxArray* y, mxArray* xi);
void mlfMyinterp2(int nargout, mxArray** ZI, mxArray* X , mxArray* Y, mxArray* Z, mxArray* XI , mxArray* YI, mxArray* method);
void mlfMygriddata(int nargout, mxArray** ZI, mxArray* x, mxArray* y , mxArray* z, mxArray* XI, mxArray* YI);
cppcurvefittinglib_mcc_component_data.c
Add and set these les to the MSVC .Net project as described in Chapter 6. Note: In this chapter we use typical functions to solve the particular problems. There are another options and MATLAB functions to solve the curve tting problems, refer to [7].
3. In the following sections, well use the following implemental functions in this library to solve the common curve tting problem (open the le cppcurvettinglib.h to see the names of these functions):
void myinterp1(int nargout, mwArray& yi, const mwArray& x , const mwArray& y, const mwArray& xi);
void myinterp2(int nargout, mwArray& ZI, const mwArray& X , const mwArray& Y, const mwArray& Z, const mwArray& XI , const mwArray& YI, const mwArray& method);
void mypolyfit(int nargout, mwArray& p, const mwArray& x , const mwArray& y, const mwArray& n);
void mymeshgrid(int nargout, mwArray& X, mwArray& Y , const mwArray& vectorstepx , const mwArray& vectorstepy);
void mygriddata(int nargout, mwArray& ZI, const mwArray& x , const mwArray& y, const mwArray& z , const mwArray& XI, const mwArray& YI);
void myfinemeshgrid(int nargout, mwArray& row, mwArray& col , const mwArray& vectorstepx , const mwArray& vectorstepy);
187
12.1
This section describe how to use the functions in the generated libraries to to nd the coecients of a polynomial function that ts a set of data in the least-squares sense. An array c representing these coecients is in the polynomial form: f (x) = c1 xn + c2 xn1 + c3 xn2 + + cn x + cn+1 Problem 1 (12.1)
There are two arrays X and Y which have a relationship via a function, y = f (x),
x X and y Y. input Array X = { 1, 2, 3, 4, 5, 6 } Array Y = { 6.8, 50.2, 140.8, 280.5, 321.4, 428.6 } output Finding an array c of the polynomial function in Eq. 12.1 with degree n=3; since degree n=3, the function y = f (x) has the form: y = c1 x3 + c2 x2 + c3 x + c4 Calculating the interpolation value of the function y(x), at x = 2.2
Listing code
/* Example.cpp */
int main() {
return 0 ; }
188
/* Example.h */
class Test {
Test ()
mclInitializeApplication(NULL,0); curvefittinglibInitialize(); }
~Test ()
curvefittinglibTerminate(); mclTerminateApplication(); }
} ;
/* **************************** */
void Test::PolynomialFittingCurve()
double db_X[6] = { 1, 2, 3, 4, 5, 6 } ; double db_Y[6] = { 6.8, 50.2, 140.8, 280.5, 321.4, 428.6 };
= 3 = 2.2
; ;
/* step 1 : declare mxArray variables */ mxArray *mx_X mxArray *mx_Y mxArray *mx_coefs = NULL ; = NULL ; = NULL ;
mxArray *mx_three
= NULL ;
189
mxArray *mx_oneValue mxArray *mx_funcValue = NULL ; = NULL ;
/* step 2 : assign memory */ int vectorSize = 6 ; mx_X mx_Y mx_coefs = mxCreateDoubleMatrix(1, vectorSize, mxREAL) = mxCreateDoubleMatrix(1, vectorSize, mxREAL) = mxCreateDoubleMatrix(1, 4 , mxREAL) ; ; ;
mx_three
= mxCreateDoubleMatrix(1,
1 1 1
; ; ;
/* step 3 : convert C/C++ double to mxArray */ double2mxArray_vectorReal (db_X, mx_X) ; double2mxArray_vectorReal (db_Y, mx_Y) ;
double2mxArray_scalarReal (db_three
, mx_three
) ;
/* step 5 : convert back to C/C++ double double *db_coefs = new double [4] ;
*/
mxArray2double_vectorReal(mx_coefs, db_coefs) ;
/* print out */ cout << "The polynomial:" << endl ; cout << db_coefs[0] << " x^3 " << " + " ; cout << db_coefs[1] << " x^2 " << " + " ; cout << db_coefs[2] << " x " cout << db_coefs[3] << endl; << " + " ;
/* calculate the function value at oneValue */ mlfMypolyval(1, &mx_funcValue, mx_coefs, mx_oneValue); double db_funcValue = mxArray2double_scalarReal(mx_funcValue) ; cout << "The function value at 2.2 is: " << db_funcValue << endl ;
*/
190
mxDestroyArray(mx_Y ) ;
mxDestroyArray(mx_coefs);
mxDestroyArray(mx_three
);
mxDestroyArray(mx_oneValue ); mxDestroyArray(mx_funcValue);
delete []db_coefs ;
end code
Listing code
/* Example.cpp */
int main() {
return 0 ; }
/* Example.h */
191
#include "mwUtilityCompilerVer4.h"
class Test {
Test ()
mclInitializeApplication(NULL,0); cppcurvefittinglibInitialize(); }
~Test ()
cppcurvefittinglibTerminate(); mclTerminateApplication(); }
} ;
double db_X[6] = { 1, 2, 3, 4, 5, 6 } ; double db_Y[6] = { 6.8, 50.2, 140.8, 280.5, 321.4, 428.6 };
int vectorSize = 6 ;
/* convert C/C++ double to mwArray */ mwArray mw_X(1, vectorSize, mxDOUBLE_CLASS) ; mw_X.SetData(db_X, vectorSize) ;
192
/* convert back to C/C++ double */
/* print out */ cout << "The polynomial:" << endl ; cout << db_coefs[0] << " x^3 " << " + " ; cout << db_coefs[1] << " x^2 " << " + " ; cout << db_coefs[2] << " x " cout << db_coefs[3] << endl; << " + " ;
/* calculate the function value at oneValue */ mwArray mw_funcValue(1, 1, mxDOUBLE_CLASS) ; mwArray mw_oneValue(1, 1, mxDOUBLE_CLASS) ; mw_oneValue(1,1) = 2.2 ;
*/
double db_funcValue = (double)mw_funcValue(1,1) ; cout << "The function value at 2.2 is: " << db_funcValue << endl ;
/* free memories
*/
delete []db_coefs ;
end code
12.2
This section describe how to use the functions in the generated libraries to solve an one-dimensional interpolation problem. This function uses polynomial techniques to evaluate value of a function at a desired interpolation point.
Problem 2
There are two arrays X and Y have a relationship via a function, y = f (x),
x X and y Y.
193 input Array X = { 1, 2, 3, 4, 5, 6, 7, 8 } Array Y = { 6.8, 24.6, 50.2, 74, 140.8, 280.5, 321.4, 428.6 }
output
The following is the code to solve Problem 2. In the code, you will use the function mlfMyinterp1(..) or myinterp1(..) with the default method (liner method) to solve the problem. To learn more about other possible methods see the MATLAB function inter1(..) in [6].
Listing code
void Test::OneDimensionInterpolation()
double db_X[8] = { 1, 2, 3, 4, 5, 6, 7, 8 } ; double db_Y[8] = { 6.8, 24.6, 50.2, 74, 140.8, 280.5, 321.4, 428.6 };
double db_oneValue
= 2.1
/* step 1 : declare mxArray variables */ mxArray *mx_X mxArray *mx_Y = NULL ; = NULL ;
= NULL ; = NULL ;
/* step 2 : assign memory */ int vectorSize = 6 ; mx_X mx_Y = mxCreateDoubleMatrix(1, vectorSize, mxREAL) = mxCreateDoubleMatrix(1, vectorSize, mxREAL) ; ;
1 1
, mxREAL) , mxREAL)
; ;
/* step 3 : convert C/C++ double to mxArray */ double2mxArray_vectorReal (db_X, mx_X) ; double2mxArray_vectorReal (db_Y, mx_Y) ;
194
double2mxArray_scalarReal (db_oneValue, mx_oneValue) ;
*/
double db_funcValue = mxArray2double_scalarReal(mx_funcValue) ; cout << "The function value at 2.1 is: " << db_funcValue << endl ;
*/
mxDestroyArray(mx_oneValue ); mxDestroyArray(mx_funcValue);
end code
void Test::OneDimensionInterpolation()
double db_X[8] = { 1, 2, 3, 4, 5, 6, 7, 8 } ; double db_Y[8] = { 6.8, 24.6, 50.2, 74, 140.8, 280.5, 321.4, 428.6 };
int vectorSize = 6 ;
195
mw_oneValue(1,1) = 2.1 ;
/* call an implemental function */ mwArray mw_funcValue(1, 1, mxDOUBLE_CLASS) ; myinterp1(1, mw_funcValue, mw_X, mw_Y, mw_oneValue);
*/
double db_funcValue = (double) mw_funcValue(1,1) ; cout << "The function value at 2.1 is: " << db_funcValue << endl ;
end code
12.3
This section describe how to use the functions in the generated libraries to solve a two-dimensional interpolation problem. This function uses polynomial techniques to evaluate value of a function at a desired interpolation point.
Problem 3
There are three matrixes, x, y, and z, that have a relationship via a function,
z = f (x, y), x x, y y, and z z. input Grid points (x,y). Representation of these grid points are two matrixes: matrix x contains the x-direction values of all grid points matrix y contains the y-direction values of all grid points Matrix z contains the function values z(x, y) of all grid points (the values of matrixes x, y, and z are shown in the next page)
output
Finding the interpolation function value z(x, y) at a particular point, (x = a = 2.3, y = b = 0.7)
196
A. Assigning values for a matrix Suppose that you have grid points as in Fig. 12.1.
The matrix x, which contains the x-direction values for all grid points, is: ( from left to right ) -3 -3 -3 -3 -3 -3 -3 -2 -2 -2 -2 -2 -2 -2 -1 -1 -1 -1 -1 -1 -1 0 0 0 0 0 0 0 1 1 1 1 1 1 1 2 2 2 2 2 2 2 3 3 3 3 3 3 3 from point 1 from point 8 to 7 to 14
from point 15 to 21 from point 22 to 28 from point 29 to 35 from point 36 to 42 from point 43 to 49
-3 -2 -1 0 1 2 3
1 8 15 22 29 36 43
2 9 16 23 30 37 44
3 10 17 24 31 38 45
4 11 18 25 32 39 46
5 12 19 26 33 40 47
6 13 20 27 34 41 48
7 14 21 28 35 42 49
-3
-2
-1
197 The matrix Y, which contains the y-direction values of all grid points, is: ( from left to right ) -3 -2 -1 0 1 2 3 -3 -2 -1 0 1 2 3 -3 -2 -1 0 1 2 3 -3 -2 -1 0 1 2 3 -3 -2 -1 0 1 2 3 -3 -2 -1 0 1 2 3 -3 -2 -1 0 1 2 3 from point 1 from point 8 to 7 to 14
from point 15 to 21 from point 22 to 28 from point 29 to 35 from point 36 to 42 from point 43 to 49
The matrix z, which contains the function values z(x, y) for all grid points, is:
( from left to right ) 0.0001 0.0007 0.0088 0.0365 0.0137 0.0000 0.0000 0.0034 0.0468 -0.1301 -1.3327 -0.4808 0.0797 0.0053 -0.0299 -0.5921 1.8559 -1.6523 0.2289 2.0967 0.1099 -0.2450 -4.7596 -0.7239 0.9810 3.6886 5.8591 0.2999 -0.1100 -2.1024 -0.2729 2.9369 2.4338 2.2099 0.1107 -0.0043 -0.0616 0.4996 1.4122 0.5805 0.1328 0.0057 -0.0000 (point 1 - 7) 0.0004 (point 8 -14) 0.0130 (point 15-21) 0.0331 (point 22-28) 0.0125 (point 29-35) 0.0013 (point 36-42) 0.0000 (point 43-49)
B. Programming code The following is the code to solve Problem 3. In the code, you will use the generated function mlfMyinterp2(..) or myinterp2(..) with the cubic method to solve the problem. To learn more about other possible methods see the MATLAB function inter2(..) in [6]. The procedure of this code is: 1. Assign values for matrix x and matrix y 2. Assign values for matrix z 3. Call the two-dimensional interpolation function to evaluate the value at the point (a, b)
198
void Test::TwoDimensionsInterpolation()
double z[7][7] = { { { 0.0001, 0.0007, 0.0034, 0.0468, -0.1301, -1.3327, -0.4808, 0.0797, 0.0053, -0.0299, -0.5921, 1.8559, -1.6523, 0.2289, 2.0967, 0.1099, -0.2450, -4.7596, -0.7239, 0.9810, 3.6886, 5.8591, 0.2999, -0.1100, -2.1024, -0.2729, 2.9369, 2.4338, 2.2099, 0.1107, -0.0043, -0.0616, 0.4996, 1.4122, 0.5805, 0.1328, 0.0057, 0.0000 0.0004 0.0130 0.0331 0.0125 0.0013 0.0000 } } } } } } } ,\ ,\ ,\ ,\ ,\ ,\ };
/* step 1 : declare mxArray variables */ mxArray *mx_a mxArray *mx_b mxArray *mx_interp2z = NULL ; = NULL ; = NULL ;
199
mxArray *mx_method ;
; ; ;
mx_vectorstepx mx_vectorstepy
; ;
/* step 3 : convert C/C++ double to mxArray */ double2mxArray_scalarReal (db_a, mx_a) ; double2mxArray_scalarReal (db_b, mx_b) ;
/* same for two step-vectors */ double2mxArray_vectorReal (db_vectorstep, mx_vectorstepx) ; double2mxArray_vectorReal (db_vectorstep, mx_vectorstepy) ;
/* step 4 : call implemental functions */ /* create values for the matrix x and matrix y */ mlfMymeshgrid(2, &mx_x, &mx_y, mx_vectorstepx, mx_vectorstepy) ;
const char
interp2method[] = "cubic";
mx_method = mxCreateString(interp2method);
*/
200
db_x[i] = new double[col] ; db_y[i] = new double[col] ; }
/* print out */ cout << "Matrix x : " << endl ; printMatrix(db_x, row, col) ; printMatrix(db_y, row, col) ;
cout << "Matriy y : " << endl ; for (i=0; i<row; i++) { {
double db_interp2z = mxArray2double_scalarReal(mx_interp2z) ; cout << "Interpolation in two dimensions with cubic method " << endl; cout << " z = " << db_interp2z << endl ;
*/ ; ; ) ;
; ; ;
mxDestroyArray(mx_vectorstepx); mxDestroyArray(mx_vectorstepy);
mxDestroyArray(mx_method);
201
delete[] db_x ; delete[] db_y ;
end code
void Test::TwoDimensionsInterpolation()
double z[7][7] = { { { 0.0001, 0.0007, 0.0034, 0.0468, -0.1301, -1.3327, -0.4808, 0.0797, 0.0053, -0.0299, -0.5921, 1.8559, -1.6523, 0.2289, 2.0967, 0.1099, -0.2450, -4.7596, -0.7239, 0.9810, 3.6886, 5.8591, 0.2999, -0.1100, -2.1024, -0.2729, 2.9369, 2.4338, 2.2099, 0.1107, -0.0043, -0.0616, 0.4996, 1.4122, 0.5805, 0.1328, 0.0057, 0.0000 0.0004 0.0130 0.0331 0.0125 0.0013 0.0000 } } } } } } } ,\ ,\ ,\ ,\ ,\ ,\ };
int nSize = 7
202
mwArray mw_vectorstepx(1, 3, mxDOUBLE_CLASS) ; mwArray mw_vectorstepy(1, 3, mxDOUBLE_CLASS) ;
/* call implemental functions */ /* create values for the matrix x and matrix y */ mymeshgrid(2, mw_x, mw_y, mw_vectorstepx, mw_vectorstepy) ;
/* print out to see */ cout << "Matriy x : " << endl ; std::cout << mw_x << std::endl ;
cout << "Matriy y : " << endl ; std::cout << mw_y << std::endl ;
*/
double db_interp2z = (double) mw_interp2z(1,1) ; cout << "Interpolation in two dimensions with cubic method " << endl; cout << " z = " << db_interp2z << endl ;
1. The M-le mymeshgrid.m uses the MATLAB function meshgrid(..) which assigns values for matrixes x and y. These values are assigned from left to right, and from top to bottom. In Fig. 12.1, the y axis direction is from top to bottom, therefore pay attention when
203 assigning your data into the matrix y. 2. The matrix z values will be assigned according to the same rules (left to right, top to bottom) as matrixes x and y (Fig. 12.2), therefore to avoid mistakes and to have the convenience of visibility, you can set up your matrix data as in Fig. 12.1 (y axis direction from top to bottom).
a11 a21 a31 a41 a41 a61 a71 a12 a22 a32 a42 a42 a62 a72 a13 a23 a33 a43 a43 a63 a73 a14 a24 a34 a44 a54 a64 a74 a15 a25 a35 a45 a55 a65 a75 a16 a26 a36 a46 a56 a66 a76 a17 a27 a37 a47 a57 a67 a77
3. To receive a better solution in the two-dimensional interpolation you can create a ne grid by using the function mygriddata(..) (see the M-le mygriddata.m in the beginning of this chapter). This functions uses the MATLAB griddata(..) function which ts a surface of the form z = f (x, y) to the data in the spaced vectors (x, y, z). For more information on this function, refer to the MATLAB manual [6]. The following code nds the ne solution of Problem 3 by using the function mygriddata(..).
204
double z[7][7] = { { { 0.0001, 0.0007, 0.0034, 0.0468, -0.1301, -1.3327, -0.4808, 0.0797, 0.0053, -0.0299, -0.5921, 1.8559, -1.6523, 0.2289, 2.0967, 0.1099, -0.2450, -4.7596, -0.7239, 0.9810, 3.6886, 5.8591, 0.2999, -0.1100, -2.1024, -0.2729, 2.9369, 2.4338, 2.2099, 0.1107, -0.0043, -0.0616, 0.4996, 1.4122, 0.5805, 0.1328, 0.0057, 0.0000 0.0004 0.0130 0.0331 0.0125 0.0013 0.0000 } } } } } } } ,\ ,\ ,\ ,\ ,\ ,\ };
int nSize = 7
double db_vectorstep[3]
= {-3,
1 , 3} ;
/* step 1 : declare mxArray variables */ mxArray *mx_a mxArray *mx_b mxArray *mx_interp2z = NULL ; = NULL ; = NULL ;
mxArray *mx_method ;
/* step 1a : declare mxArray variables */ mxArray *mx_finex mxArray *mx_finey mxArray *mx_finez = NULL ; = NULL ; = NULL ;
mxArray *mx_finerow
= NULL ;
205
mxArray *mx_finecol = NULL ;
; ; ;
mx_vectorstepx mx_vectorstepy
; ;
mx_finevectorstepx mx_finevectorstepy
; ;
/* step 3 : convert C/C++ double to mxArray */ double2mxArray_scalarReal (db_a, mx_a) ; double2mxArray_scalarReal (db_b, mx_b) ;
/* same for two step-vectors */ double2mxArray_vectorReal (db_vectorstep, mx_vectorstepx) ; double2mxArray_vectorReal (db_vectorstep, mx_vectorstepy) ;
/* step 3a : convert C/C++ double to mxArray */ double2mxArray_vectorReal (db_finevectorstep, mx_finevectorstepx) ; double2mxArray_vectorReal (db_finevectorstep, mx_finevectorstepy) ;
206
mlfMyfinemeshgrid(2, &mx_finerow, &mx_finecol, mx_finevectorstepx, mx_finevectorstepy) ;
cout << " fine row = " << finerow << endl ; cout << " fine col = " << finecol << endl ;
; ; ;
/* step 4 : call implemental functions */ /* create values for the matrix x and matrix y */ mlfMymeshgrid(2, &mx_x, &mx_y, mx_vectorstepx, mx_vectorstepy) ;
/* create values for the fine matrix x and fine matrix y */ mlfMymeshgrid(2, &mx_finex, &mx_finey, mx_finevectorstepx, mx_finevectorstepy) ;
/* get a fine matrix mx_finez from mx_z */ mlfMygriddata(1, &mx_finez, mx_x, mx_y, mx_z, mx_finex, mx_finey);
const char
interp2method[] = "cubic"
; ;
mx_method = mxCreateString(interp2method)
*/
double db_interp2finez = mxArray2double_scalarReal(mx_interp2z) ; cout << "Interpolation in two dimensions " ; cout << "with cubic method and a fine grid " << endl; cout << " z = " << db_interp2finez << endl ;
*/ ; ; ) ;
207
mxDestroyArray(mx_x ) mxDestroyArray(mx_y ) mxDestroyArray(mx_z ) ; ; ;
mxDestroyArray(mx_vectorstepx); mxDestroyArray(mx_vectorstepy);
mxDestroyArray(mx_method);
mxDestroyArray(mx_finerow) ; mxDestroyArray(mx_finecol) ;
mxDestroyArray(mx_finevectorstepx) ; mxDestroyArray(mx_finevectorstepy) ;
end code
void Test::TwoDimensionsInterpolationFineSolution()
double z[7][7] = { { { 0.0001, 0.0007, 0.0034, 0.0468, -0.1301, -1.3327, -0.4808, 0.0797, 0.0053, -0.0299, -0.5921, 1.8559, -1.6523, 0.2289, 2.0967, 0.1099, -0.2450, -4.7596, -0.7239, 0.9810, 3.6886, 5.8591, 0.2999, -0.1100, -2.1024, -0.2729, 2.9369, 2.4338, 2.2099, 0.1107, -0.0043, -0.0616, 0.4996, 1.4122, 0.5805, 0.1328, 0.0057, 0.0000 0.0004 0.0130 0.0331 0.0125 0.0013 0.0000 } } } } } } } ,\ ,\ ,\ ,\ ,\ ,\ };
208
int nSize = 7 ;
double db_vectorstep[3]
= {-3,
1 , 3} ;
/* declare mwArray variables */ mwArray mw_vectorstepx(1, 3, mxDOUBLE_CLASS) ; mwArray mw_vectorstepy(1, 3, mxDOUBLE_CLASS) ; /* same for two step-vectors */ mw_vectorstepx.SetData(db_vectorstep, 3) ; mw_vectorstepy.SetData(db_vectorstep, 3) ;
mwArray mw_finevectorstepx(1, 3, mxDOUBLE_CLASS) ; mwArray mw_finevectorstepy(1, 3, mxDOUBLE_CLASS) ; /* same for two fine step-vectors */ mw_finevectorstepx.SetData(db_finevectorstep, 3) ; mw_finevectorstepy.SetData(db_finevectorstep, 3) ;
/* get size for fine matrixes */ myfinemeshgrid(2, mw_finerow, mw_finecol, mw_finevectorstepx, mw_finevectorstepy) ;
209
cout << " fine row = " << finerow << endl ; cout << " fine col = " << finecol << endl ;
mwArray mw_finex(finerow, finecol, mxDOUBLE_CLASS) ; mwArray mw_finey(finerow, finecol, mxDOUBLE_CLASS) ; mwArray mw_finez(finerow, finecol, mxDOUBLE_CLASS) ;
/* step 4 : call implemental functions */ /* create values for the matrix x and matrix y */ mwArray mw_x(row, col, mxDOUBLE_CLASS) ; mwArray mw_y(row, col, mxDOUBLE_CLASS) ;
/* create values for the fine matrix x and fine matrix y */ mymeshgrid(2, mw_finex, mw_finey, mw_finevectorstepx, mw_finevectorstepy) ;
/* get a fine matrix mw_finez from mw_z */ mygriddata(1, mw_finez, mw_x, mw_y, mw_z, mw_finex, mw_finey);
*/
double db_interp2finez = (double) mw_interp2z(1,1) ; cout << "Interpolation in two dimensions " ; cout << "with cubic method and a fine grid " << endl; cout << " z = " << db_interp2finez << endl ;
end code
210
12.4
This section describe how to use the function mlfMyinterp2(..) in the generated curvettinglib library or myinterp2(..) in the generated cppcurvettinglib library to solve the problem of two-dimensional polynomial interpolation for non-grid points.
Problem 4 input Suppose that you have values of experimental data that are not in grid points. At each value xi there are the corresponding values of array yi [ ] and array zi [ ] as shown in Fig. 12.3. The values x, y, and z of non-grid points are in Table 12.1 output Finding the interpolation function value at a particular point (a, b), where a = 2.3, b = 0.7
-3 -2 -1 0 1 2 3
x1 = -2.19736 x2 = -1.75720 x3 = -1.24647 x4 = 0.77271 x5 = 1.09999 x6 = 2.03543 x7 = 2.60028
-3
-2
-1
The following is the code to solve Problem 4 by using the cubic method in the MATLAB function interp2(..) through the M-le myinterp2.m. See the MATLAB manual [6] for using other methods in the function interp2(..).
The steps of the procedure for the programming code are: 1. Assign values (from Table 12.1) for matrixes y and z by assigning values in column arrays.
211
at x1 = -2.19736 y1 -2.92946 -2.18089 -1.80517 -1.29355 -0.20766 0.96866 2.86339 z1 0.0001 0.0007 -0.0088 -0.0365 -0.0137 0.0080 0.0000
at x2 = -1.75720 y2 -2.61131 -0.99629 -0.45902 -0.18465 0.09307 0.49675 2.93001 z2 0.0034 0.0468 -0.1301 -1.3327 -0.4808 0.0797 0.0053
at x3 = -1.24647 y3 -2.74558 -1.64430 -0.40256 0.17894 0.47884 0.84316 2.96219 z3 -0.0299 -0.5921 1.8559 -1.6523 0.2289 2.0967 0.1099
at x4 = 0.77271 y4 -2.64490 -0.72109 -0.23343 0.40697 1.08507 1.69997 2.76526 z4 -0.2450 -4.7596 -0.7239 0.9810 3.6886 5.8591 0.2999
at x5 = 1.09999 y5 -2.90994 -2.69839 -1.17001 -0.50775 0.61721 1.60770 2.84620 z5 -0.1100 -2.1024 -0.2729 2.9369 2.4338 2.2099 0.1107
at x6 = 2.03543 y6 -2.71622 -0.36805 -0.01013 0.86095 1.73317 2.82507 2.94050 z6 -0.0043 -0.0616 0.4996 1.4122 0.5805 0.1328 0.0057
at x7 = 2.60028 y7 -2.76059 -1.07979 -0.52828 -0.36045 1.35979 1.46739 2.39232 z7 -0.0000 0.0004 0.0130 0.0331 0.0125 0.0013 0.0000
212 2. Choose new grid points as in Fig. 12.4, in which new values of new grid points in the x direction are the same of old grids, as follows: x1 = 2.19736 x5 = 1.09999 x2 = 1.75720 x6 = 2.03543 x3 = 1.24647 x7 = 2.60028 x4 = 0.77271
2.2
-3
-2
-1
and new values of new grid points in y direction are : y1 = 2.5 y5 = 0.5 y9 = 1.5 y2 = 2.0 y6 = y10 = 0.0 2.0 y3 = 1.5 y7 = y11 = 0.5 2.2 y4 = 1.0 y8 = 1.0
Note that these new 11 y values (ynew ) are not outside the original values in each y array in Table 12.1 (yi,min ynew yi,max , i = 1, 2, . . . , 7). There are 77(= 7 11) new grid points that were created. 3. Calculate z values for the 77 new grid points by calculating its columns via the onedimensional interpolation function. This calculation is the one-dimension interpolation of y and z in each column (note x is constant at each current calculating column y and z). At each column, for each number in row {
213 Actually, this calculation is the calculation of the new z from the old pair array (y, z). The gure 12.5 shows the new z from (y, z) of the rst pair (y1 , z1 ) of Table 12.1.
0.005 z 0 0.005 0.01 0.015 0.02 0.025 0.03 0.035 0.04 3 2 1 0 1 2 3 old data interpolation data
4. Generate x and y values for the 77 new grid points (assign values for new matrixes x and y). The new matrixes are matrixes with 7 rows 11 columns. 5. Call the two dimensional interpolation function to evaluate the value at a point (a, b). The following is the code to solve Problem 4 by using the functions in the libraries with the cubic method.
/* solving a problem in which experimental data are not in grids */ /* and we would like to get interpolation at a point (xi,yj) */
/* step 1 in the procedure */ /* from the experiment data in the table, fill the matrix */
double db_yoldmatrix[7][7] = { {-2.92946, {-2.18089, {-1.80517, {-1.29355, {-0.20766, -2.61131, -0.99629, -0.45902, -0.18465, 0.09307, -2.74558, -1.64430, -0.40256, 0.17894, 0.47884, -2.64490, -0.72109, -0.23343, 0.40697, 1.08507, -2.90994, -2.69839, -1.17001, -0.50775, 0.61721, -2.71622, -0.36805, -0.01013, 0.86095, 1.73317, -2.76059 } -1.07979 } -0.52828 } -0.36045 } 1.35979 } ,\ ,\ ,\ ,\ ,\
214
{ 0.96866, { 2.86339, 0.49675, 2.93001, 0.84316, 2.96219, 1.69997, 2.76526, 1.60770, 2.84620, 2.82507, 2.94050, 1.46739 } 2.39232 } ,\ };
double db_zoldmatrix[7][7] = { { { 0.00010, 0.00070, 0.00340, 0.04680, -0.13010, -1.33270, -0.48080, 0.07970, 0.00530, -0.02990, -0.59210, 1.85590, -1.65230, 0.22890, 2.09670, 0.10990, -0.24500, -4.75960, -0.72390, 0.98100, 3.68860, 5.85910, 0.29990, -0.11000, -2.10240, -0.27290, 2.93690, 2.43380, 2.20990, 0.11070, -0.00430, -0.06160, 0.49960, 1.41220, 0.58050, 0.13280, 0.00570, 0.00000 } 0.00040 } 0.01300 } 0.03310 } 0.01250 } 0.00130 } 0.00000 } ,\ ,\ ,\ ,\ ,\ ,\ };
/* step 2 in the procedure */ double db_oldvectorx[7] = {-2.19736, -1.75720, -1.24647, 0.77271, 1.09999, 2.03543, 2.60028}; double db_newcoly[11] = {-2.5, -2.0, -1.5, -1.0, -0.5, 0.0, 0.5, 1.0, 1.5, 2.0, 2.2 } ;
= 7 = 7 = 11
; ; ;
= NULL ; = NULL ;
= NULL ; = NULL ;
215
mxArray *mx_method ;
mx_oldcoly mx_oldcolz
mx_newcoly mx_newcolz
; ;
; ; ;
; ;
/* *********** */ /* *********** */ /* ***** using 1D interpolation to establish a new matrix z *** */ /* run for 7(=col) columns */
double *db_oldcoly = new double [col] ; double *db_oldcolz = new double [col] ;
// 11=newrow is the number of elements in the new column double *db_newcolz = new double [11] ;
double **db_newmatrixz = new double* [newrow] ; for (i=0; i<newrow; i++) { db_newmatrixz[i] = new double [col] ; }
216
for (i=0; i<row; i++) {
/* convert to use the function mlfMyinterp1(..) */ double2mxArray_vectorReal(db_oldcoly, mx_oldcoly) ; double2mxArray_vectorReal(db_oldcolz, mx_oldcolz) ; double2mxArray_vectorReal(db_newcoly, mx_newcoly) ;
/* fill the each column result to a new matrix z /* each column includes 11(=newcol) elements for(k=0; k<11; k++) {
*/ */
db_newmatrixz[k][j] = db_newcolz[k] ;
cout << "New matrix z : " << endl ; for (i=0; i<newrow; i++) for (j=0; j<col; j++) { {
217
/* create the matrix x from vectorx, all rows are same */ double **db_newmatrixx = new double* [newrow] ; for (i=0; i<newrow; i++) {
cout << "New matrix x : " << endl ; for (i=0; i<newrow; i++) for (j=0; j<col; j++) { {
/* create the matrix y from db_oldcoly, all columns are same */ double **db_newmatrixy = new double* [newrow] ; for (i=0; i<newrow; i++) {
db_newmatrixy[i][j] = db_newcoly[i] ; } }
cout << "New matrix y : " << endl ; for (i=0; i<newrow; i++) for (j=0; j<col; j++) { {
218
/* step 5 in the procedure */ const char interp2method[] = "cubic" ; ;
mx_method = mxCreateString(interp2method)
cout << "Interpolation in two dimensions " ; cout << "and experimental data are not in grids " << endl; cout << " z = " << db_interp2z << endl ;
mxDestroyArray(mx_newcoly) mxDestroyArray(mx_newcolz)
; ;
; ; ;
mxDestroyArray(mx_method);
219
delete [] db_oldcoly ; delete [] db_oldcolz ;
delete [] db_newcolz ;
end code
Remark: We also add here the M-le to solve Problem 4 for reference. function interp2z = Interp2InNonGrid(a, b)
%step 1 db_yoldmatrix = [ -2.9295 -2.1809 -1.8052 -1.2936 -0.2077 0.9687 2.8634 -2.6113 -0.9963 -0.4590 -0.1847 0.0931 0.4968 2.9300 -2.7456 -1.6443 -0.4026 0.1789 0.4788 0.8432 2.9622 -2.6449 -0.7211 -0.2334 0.4070 1.0851 1.7000 2.7653 -2.9099 -2.6984 -1.1700 -0.5078 0.6172 1.6077 2.8462 -2.7162 -0.3680 -0.0101 0.8609 1.7332 2.8251 2.9405 -2.7606 -1.0798 -0.5283 -0.3604 1.3598 1.4674 2.3923 ] ;
db_zoldmatrix = [ 0.00010 0.00070 -0.00880 -0.03650 -0.01370 0.00080 0.00000 0.00340 0.04680 -0.13010 -1.33270 -0.48080 0.07970 0.00530 -0.02990 -0.59210 1.85590 -1.65230 0.22890 2.09670 0.10990 -0.24500 -4.75960 -0.72390 0.98100 3.68860 5.85910 0.29990 -0.11000 -2.10240 -0.27290 2.93690 2.43380 2.20990 0.11070 -0.00430 -0.06160 0.49960 1.41220 0.58050 0.13280 0.00570 0.00000 ; ... 0.00040 ; ... 0.01300 ; ... 0.03310 ; ... 0.01250 ; ... 0.00130 ; ... 0.00000 ] ;
%step 2 db_newcoly = [-2.5, -2.0, -1.5, -1.0, -0.5, 0.0, 0.5, 1.0, 1.5, 2.0, 2.2 ] ;
220
for i = 1:7 db_newmatrixz(:,i) = interp1( db_yoldmatrix(:,i), db_zoldmatrix(:,i), db_newcoly ) ; db_newmatrixy(:,i) = db_newcoly ; end
end code
/* solving a problem in which experimental data are not in grids */ /* and we would like to get interpolation at a point (xi,yj) */
/* step 1 in the procedure */ /* from the experiment data in the table, fill the matrix */
double db_yoldmatrix[7][7] = { {-2.92946, {-2.18089, {-1.80517, {-1.29355, {-0.20766, { 0.96866, { 2.86339, -2.61131, -0.99629, -0.45902, -0.18465, 0.09307, 0.49675, 2.93001, -2.74558, -1.64430, -0.40256, 0.17894, 0.47884, 0.84316, 2.96219, -2.64490, -0.72109, -0.23343, 0.40697, 1.08507, 1.69997, 2.76526, -2.90994, -2.69839, -1.17001, -0.50775, 0.61721, 1.60770, 2.84620, -2.71622, -0.36805, -0.01013, 0.86095, 1.73317, 2.82507, 2.94050, -2.76059 } -1.07979 } -0.52828 } -0.36045 } 1.35979 } 1.46739 } 2.39232 } ,\ ,\ ,\ ,\ ,\ ,\ };
221
double db_zoldmatrix[7][7] = { { { 0.00010, 0.00070, 0.00340, 0.04680, -0.13010, -1.33270, -0.48080, 0.07970, 0.00530, -0.02990, -0.59210, 1.85590, -1.65230, 0.22890, 2.09670, 0.10990, -0.24500, -4.75960, -0.72390, 0.98100, 3.68860, 5.85910, 0.29990, -0.11000, -2.10240, -0.27290, 2.93690, 2.43380, 2.20990, 0.11070, -0.00430, -0.06160, 0.49960, 1.41220, 0.58050, 0.13280, 0.00570, 0.00000 } 0.00040 } 0.01300 } 0.03310 } 0.01250 } 0.00130 } 0.00000 } ,\ ,\ ,\ ,\ ,\ ,\ };
/* step 2 in the procedure */ double db_oldvectorx[7] = {-2.19736, -1.75720, -1.24647, 0.77271, 1.09999, 2.03543, 2.60028}; double db_newcoly[11] = {-2.5, -2.0, -1.5, -1.0, -0.5, 0.0, 0.5, 1.0, 1.5, 2.0, 2.2 } ;
= 7 = 7 = 11
; ; ;
/* *********** */ /* *********** */ /* ***** using 1D interpolation to establish a new matrix z *** */ /* run for 7(=col) columns */
double *db_oldcoly = new double [col] ; double *db_oldcolz = new double [col] ;
222
double *db_newcolz = new double [11] ;
double **db_newmatrixz = new double* [newrow] ; for (i=0; i<newrow; i++) { db_newmatrixz[i] = new double [col] ; }
/* get a current column for calculation of 1D inperpolation */ for (i=0; i<row; i++) {
/* convert to use the function myinterp1(..) */ mwArray mw_oldcoly(row, 1, mxDOUBLE_CLASS) ; mw_oldcoly.SetData(db_oldcoly, row) ;
/* calculate 1D inperpolation */ mwArray mw_newcolz(newrow, 1, mxDOUBLE_CLASS) ; myinterp1(1, mw_newcolz, mw_oldcoly, mw_oldcolz, mw_newcoly);
/* fill the each column result to a new matrix z /* each column includes 11(=newcol) elements for(k=0; k<11; k++) {
*/ */
db_newmatrixz[k][j] = db_newcolz[k] ;
223
} /* ***** finished establishing the new matrix z *** */ /* *********** */ /* *********** */
cout << "New matrix z : " << endl ; for (i=0; i<newrow; i++) for (j=0; j<col; j++) { {
/* create the matrix x from vectorx, all rows are same */ double **db_newmatrixx = new double* [newrow] ; for (i=0; i<newrow; i++) {
cout << "New matrix x : " << endl ; for (i=0; i<newrow; i++) for (j=0; j<col; j++) { {
/* create the matrix y from db_oldcoly, all columns are same */ double **db_newmatrixy = new double* [newrow] ; for (i=0; i<newrow; i++) {
224
db_newmatrixy[i] = new double [col] ; }
db_newmatrixy[i][j] = db_newcoly[i] ; } }
cout << "New matrix y : " << endl ; for (i=0; i<newrow; i++) for (j=0; j<col; j++) { {
/* step 5 in the procedure */ mwArray mw_newmatrixx = double2mwArray_matrixReal(db_newmatrixx, newrow, col) ; mwArray mw_newmatrixy = double2mwArray_matrixReal(db_newmatrixy, newrow, col) ; mwArray mw_newmatrixz = double2mwArray_matrixReal(db_newmatrixz, newrow, col) ;
cout << "Interpolation in two dimensions " ; cout << "and experimental data are not in grids " << endl; cout << " z = " << db_interp2z << endl ;
delete [] db_newcolz ;
225
delete [] db_newmatrixx ; delete [] db_newmatrixy ; delete [] db_newmatrixz ;
end code
226
Chapter 13
Roots of Equations
In this chapter well generate a C shared library rootslib and a C++ shared library cpprootslib from common M-les to nd the roots of a polynomial function and a nonlinear function. The generated functions of these library will be used in a MSVC .Net project to nd the roots of functions.
Following are steps to create a C shared library rootslib.dll and a C++ shared library cpprootslib.dll which will be used in the next sections.
We will write the M-les as shown below. These functions will be used to generate the C and C++ shared libraries. myfzero.m and myroots.m
228
1. Write the command in Windows Command Prompt as follows to create a C shared library rootslib : mcc -B csharedlib:rootslib myfzero.m myroots.m 2. MATLAB Compiler 4 will create eight les for this C shared library: rootslib.c rootslib.exp rootslib.lib rootslib.ctf rootslib.exports rootslib.dll rootslib.h
rootslib_mcc_component_data.c
Add and set these les to the MSVC .Net project as described in Chapter 6. 3. In the following sections, well use the following implemental functions in this library to solve the problems(open the le rootslib.h to see the names of these functions): void mlfMyroots(int nargout, mxArray** r, mxArray* c); void mlfMyfzero(int nargout, mxArray** x, mxArray* strfunc, mxArray* x0);
cpprootslib_mcc_component_data.c
Add and set these les to the MSVC .Net project as described in Chapter 6. 3. In the following sections, well use the following implemental functions in this library to solve the problems(open the le rootslib.h to see the names of these functions): void myfzero(int nargout, mwArray& x , const mwArray& strfunc, const mwArray& x0);
229
13.1
Roots of Polynomials
This section describe how to use the functions in the generated libraries to nd the roots of a polynomial function.
Problem 1
In calculation, the values of these coecients will be assigned to an array c[ ] in the function (pay attention to its order): f (x) = c1 x3 + c2 x2 + c3 x + c4 where: c1 = 1 , c2 = 7.2 , c3 = 21 , and c4 = 5
Listing code
/* Example.cpp */
int main() {
cout << "Roots of a polynormial function." << endl ; Test obj ; obj.FindingRootsPolynormial() ;
return 0 ; }
230
/* Example.h */
class Test {
Test ()
mclInitializeApplication(NULL,0); rootslibInitialize(); }
~Test ()
rootslibTerminate(); mclTerminateApplication(); }
} ;
/* **************************** */
void Test::FindingRootsPolynormial()
/* Find the solutions of polynomial function: f(x) = -x^3 + 7.2x^2 -21x -5 */ int order = 3 ; double db_coefs[] = { -1, 7.2, -21, -5 } ;
/* step 1 : declare mxArray variables */ mxArray *mx_coefs = NULL ; mxArray *mx_x = NULL ;
231
mx_coefs mx_x = mxCreateDoubleMatrix(vectorSize, 1, mxREAL) = mxCreateDoubleMatrix( order , 1, mxCOMPLEX) ; ;
/* step 5 : convert back to C/C++ double double *db_xReal = new double [order] ; double *db_xImag = new double [order] ;
*/
/* print out */ cout << "Solutions of the polynomial function : " << endl ; int i ; for (i=0; i<3; i++) {
cout << db_xReal[i] << " + " ; cout << db_xImag[i] << "i" << endl ; }
*/ ; ;
; ;
end code Note The roots of a polynomial function may have imaginary terms. Therefore we need to use the function mxArray2double_vectorComplex(..) in converting to double type.
232
Listing code
/* Example.cpp */
int main() {
return 0 ; }
class Test {
Test ()
mclInitializeApplication(NULL,0); cpprootslibInitialize(); }
~Test ()
cpprootslibTerminate(); mclTerminateApplication(); } } ;
233
/* **************************** */ void Test::FindingRootsPolynormial() /* Find the solutions of polynomial function: f(x) = -x^3 + 7.2x^2 -21x -5 */ int order = 3 ; double db_coefs[] = { -1, 7.2, -21, -5 } ; int vectorSize = order + 1 ; {
/* call an implemental function */ mwArray mw_x(order, 1, mxDOUBLE_CLASS, mxCOMPLEX) ; myroots(1, mw_x, mw_coefs);
*/
double *db_xReal = new double [order] ; double *db_xImag = new double [order] ;
/* print out */ cout << "Solutions of the polynomial function : " << endl ; int i ; for (i=0; i<3; i++) {
cout << db_xReal[i] << " + " ; cout << db_xImag[i] << "i" << endl ; }
*/
end code
234
13.2
This section describe how to use the function mlfMyfzero(..) in the generated rootslib library or myfzero(..) in the generated cpprootslib library to nd a root of a nonlinear function. These functions use a MATLAB function fzero(..) which returns ONLY ONE SOLUTION near the initial guess value. For more information of the fzero(..) function, refer to MATLAB manual [6].
Problem 2
Listing code
void Test::FindingZeroFunction ( ) { /* Find the solution of the function f(x) = sin(2*x) + cos(x) + 1 fzero(..) returns ONLY ONE SOLUTION near a initial guess value If your problem is complicated, please look at functions in Optimization Tool Box */ double db_initialGuess = 0.9 ; const char strfunc[] = "sin(2*x) + cos(x) + 1" ;
/* step 1 : declare mxArray variables */ mxArray *mx_initialGuess = NULL ; mxArray *mx_x mxArray *mx_strfunc ; = NULL ;
235
mx_strfunc = mxCreateString(strfunc) ;
*/
db_xReal = mxArray2double_scalarReal(mx_x) ;
/* print out */ cout << "Solutions of the given function : " << endl ; cout << db_xReal << endl ;
*/ ; ; ;
end code
Listing code
void Test::FindingZeroFunction ()
/* Find the solution of the function f(x) = sin(2*x) + cos(x) + 1 fzero(..) returns ONLY ONE SOLUTION near a initial guess value If your problem is complicated, please look at functions in Optimization Tool Box */
236
/* call an implemental function */ mwArray mw_x(1, 1, mxDOUBLE_CLASS) ; myfzero(1, mw_x, mw_strfunc, mw_initialGuess);
*/ ;
/* print out */ cout << "Solutions of the given function : " << endl ; cout << db_xReal << endl ;
end code Note The generated function myfzero(..) or myfzero(..) is a function-function and has an argument as an expression string. The form of this expression string follows the rule of a MATLAB expression string.
Chapter 14
F (s) =
f (x)ei(2s)x dx
(14.1a)
f (x) =
F (s)e
i(2x)s
ds
(14.1b)
The Fast Fourier Transform (FFT) is an ecient algorithm for computing the discrete Fourier transform [1]. MATLAB provides functions for working on the Fast Fourier Transform and its inverse. These functions will be used to generate shared libraries in the following sections.
In this chapter well generate a C shared library tlib and a C++ shared library cpptlib from common M-les working on FFT problems. The generated functions of these libraries will be used in a MSVC .Net project to solve the integral problems.
Following are steps to create a C shared library tlib.dll and a C++ shared library cpptlib.dll which will be used to solve FFT problems in the next sections.
We will write the M-les as shown below. These functions will be used to generate the C and C++ shared libraries. myfft.m, myifft.m, myfft2.m, and myifft2.m
238
function Y = myfft(X)
Y = fft(X) ;
function Y = myifft(X)
Y = ifft(X) ;
function Y = myfft2(X)
Y = fft2(X) ;
function Y = myifft2(X)
Y = ifft2(X) ;
fftlib_mcc_component_data.c
Add and set these les to the MSVC .Net project as described in Chapter 6. 3. In the following sections, well use the following implemental functions in this library to solve the common problems in the fast Fourier transform (open the le tlib.h to see the names of these functions): void mlfMyfft (int nargout, mxArray** Y, mxArray* X);
void mlfMyifft (int nargout, mxArray** Y, mxArray* X); void mlfMyfft2 (int nargout, mxArray** Y, mxArray* X); void mlfMyifft2(int nargout, mxArray** Y, mxArray* X);
239
1. Write the command in Windows Command Prompt as follows to create a C++ shared library cppfttlib : mcc -W cpplib:cpptlib -T link:lib myt.m myit.m myt2.m myit2.m 2. MATLAB Compiler 4 will create eight les for this C++ shared library: cppfftlib.cpp cppfftlib.exp cppfftlib.lib cppfftlib.ctf cppfftlib.exports cppfftlib.dll cppfftlib.h
cppfftlib_mcc_component_data.c
Add and set these les to the MSVC .Net project as described in Chapter 6. 3. In the following sections, well use the following implemental functions in this library to solve the common problems in the fast Fourier transform (open the le cpptlib.h to see the names of these functions): void myfft (int nargout, mwArray& Y, const mwArray& X);
void myifft (int nargout, mwArray& Y, const mwArray& X); void myfft2 (int nargout, mwArray& Y, const mwArray& X); void myifft2(int nargout, mwArray& Y, const mwArray& X);
14.1
The MATLAB functions implement FFT and its inverse for a vector X with length N as follows:
N
Yk =
n=1
Xn e(i2)
n1 N
(k1)
1kN
(14.2a)
and its inverse Xn = Remarks 1. The vectors X and Y in equation 14.2 are represented by functions f (x) and F (s) in equation 14.1, respectively. 2. The rst index of a vector in MATLAB starts at 1, therefore in the equation 14.2 we have term (n1) and (k 1). This produces identical results as the traditional Fourier equations from 0 to (N 1). 1 N
N
Yk e(i2)
k=1
k1 N
(n1)
1nN
(14.2b)
240
Problem 1
input
output
/* Example.cpp */
int main() {
cout << "Fast Fourier Transform." << endl ; Test obj ; obj.FastFourierTrans1D() ;
return 0 ; }
/* Example.h */
class Test {
241
Test () {
mclInitializeApplication(NULL,0); fftlibInitialize(); }
~Test ()
fftlibTerminate(); mclTerminateApplication(); }
} ;
/* **************************** */
void Test::FastFourierTrans1D()
double
int vectorSize = 8 ;
/* step 1 : declare mxArray variables */ mxArray *mx_X = NULL ; mxArray *mx_Y = NULL ;
*/
double *db_YReal = new double [vectorSize] ; double *db_YImag = new double [vectorSize] ;
/* print out */
242
cout << "Fast Fourier Transform of X : " << endl ; int i ; for (i=0; i<vectorSize; i++) {
cout << db_YReal[i] << " + " ; cout << db_YImag[i] << "i" << endl ; } cout << endl ;
*/
; ;
end code
int main() {
cout << "Fast Fourier Transform." << endl ; Test obj ; obj.FastFourierTrans1D() ;
return 0 ;
243
/* Example.h */
class Test {
Test ()
mclInitializeApplication(NULL,0); cppfftlibInitialize(); }
~Test ()
cppfftlibTerminate(); mclTerminateApplication(); }
double
int vectorSize = 8 ;
/* call an implemental function */ mwArray mw_Y(vectorSize, 1, mxDOUBLE_CLASS) ; myfft (1, mw_Y, mw_X);
*/
double *db_YReal = new double [vectorSize] ; double *db_YImag = new double [vectorSize] ;
244
mwArray2double_vectorComplex(mw_Y, db_YReal, db_YImag) ;
/* print out */ cout << "Fast Fourier Transform of X : " << endl ; int i ; for (i=0; i<vectorSize; i++) {
cout << db_YReal[i] << " + " ; cout << db_YImag[i] << "i" << endl ; } cout << endl ;
; ;
input
a vector Y,
Y real numbers = {9.00, 13.0711, 1.0, -1.0711, 13.0, -1.0711 , 1.0 , 13.0711 } Y imaginary numbers = {0, -1.9289, -14.0, 16.0711, 0, -16.0711, 14.0, 1.9289 }
output
Listing code
void Test::InverseFastFourierTrans1D() {
double double
int vectorSize = 8 ;
245
mxArray *mx_Y = NULL ;
*/
double *db_XReal = new double [vectorSize] ; double *db_XImag = new double [vectorSize] ;
/* print out */ cout << "Inverse Fast Fourier Transform of Y : " << endl ; int i ; for (i=0; i<vectorSize; i++) {
cout << db_XReal[i] << " + " ; cout << db_XImag[i] << "i" << endl ; } cout << endl ;
*/
; ;
end code
void Test::InverseFastFourierTrans1D()
double double
int vectorSize = 8 ;
/* declare mxArray variables */ mwArray mw_Y(vectorSize, 1, mxDOUBLE_CLASS, mxCOMPLEX) ; mw_Y.Real().SetData(db_YReal, vectorSize) ; mw_Y.Imag().SetData(db_YImag, vectorSize) ;
/* call an implemental function */ mwArray mw_X(vectorSize, 1, mxDOUBLE_CLASS, mxCOMPLEX) ; myifft(1, mw_X, mw_Y);
*/
double *db_XReal = new double [vectorSize] ; double *db_XImag = new double [vectorSize] ;
/* print out */ cout << "Inverse Fast Fourier Transform of Y : " << endl ; int i ; for (i=0; i<vectorSize; i++) {
cout << db_XReal[i] << " + " ; cout << db_XImag[i] << "i" << endl ; } cout << endl ;
*/ ; ;
end code
247
14.2
The MATLAB function t2(..) (Y = f f t2(X)) computes the one-dimensional FFT of each column of a matrix X, and the size of the result matrix Y is the same size of X. If you want to get a dierent size, use the function Y = f f t2(X, m, n), refer to the MATLAB manual [6].
Problem 2
input
X = 4 2.2 output
5.4 12
void Test::FastFourierTrans2D()
double X[3][4] = { {4
3.2, 1.2,
6.8, 4.3, 8 ,
9.1 5.4
} }
,\ ,\ } ;
{-4 ,
12.2 }
/*
*/
248
/* step 1 : declare mxArray variables */ mxArray *mx_X = NULL ; mxArray *mx_Y = NULL ;
/* step 2 : assign memory */ mx_X mx_Y = mxCreateDoubleMatrix(row, col, mxREAL) = mxCreateDoubleMatrix(row, col, mxCOMPLEX) ; ;
/* step 5 : convert back to C/C++ double double **db_YReal = new double* [row] ; double **db_YImag = new double* [col] ;
*/
/* print out */ cout << "2-D Fast Fourier Transform of X : " << endl ;
for (j=0; j<col; j++ ) { cout << db_YReal[i][j] << " + " ; cout << db_YImag[i][j] << "i" << "\t\t" ; } cout << endl ; } cout << endl ;
*/
249
mxDestroyArray(mx_Y) ;
; ; ;
end code
void Test::FastFourierTrans2D()
double X[3][4] = { {4
3.2, 1.2,
6.8, 4.3, 8 ,
9.1 5.4
} }
,\ ,\ } ;
{-4 ,
12.2 }
/* step 1 : declare mxArray variables */ mxArray *mx_X = NULL ; mxArray *mx_Y = NULL ;
/* step 2 : assign memory */ mx_X mx_Y = mxCreateDoubleMatrix(row, col, mxREAL) = mxCreateDoubleMatrix(row, col, mxCOMPLEX) ; ;
/* step 3 : convert C/C++ double to mxArray */ mwArray mw_X = double2mwArray_matrixReal(&X[0][0], row, col) ;
/* step 4 : call an implemental function */ mwArray mw_Y(row, col, mxDOUBLE_CLASS, mxCOMPLEX) ; myfft2 (1, mw_Y, mw_X);
250
/* step 5 : convert back to C/C++ double double **db_YReal = new double* [row] ; double **db_YImag = new double* [col] ; */
/* print out */ cout << "2-D Fast Fourier Transform of X : " << endl ;
for (j=0; j<col; j++ ) { cout << db_YReal[i][j] << " + " ; cout << db_YImag[i][j] << "i" << "\t\t" ; } cout << endl ; } cout << endl ;
*/
input
Matrix Y,
(45.70 + 0i)
(16.9000 29.0000i)
output
251
void Test::InverseFastFourierTrans2D()
,\ ,\ };
double YImag[3][4] =
{{ 0, { 7.6210, {-7.6210,
0, 9.5263, -9.5263,
/*
*/
double** db_YReal = new double* [row] ; double** db_YImag = new double* [row] ;
/* step 1 : declare mxArray variables */ mxArray *mx_X = NULL ; mxArray *mx_Y = NULL ;
252
mx_Y mx_X = mxCreateDoubleMatrix(row, col, mxCOMPLEX) ; = mxCreateDoubleMatrix(row, col, mxCOMPLEX) ;
/* step 5 : convert back to C/C++ double double **db_XReal = new double* [row] ; double **db_XImag = new double* [row] ;
*/
/* print out */ cout << "Inverse 2-D Fast Fourier Transform of Y : " << endl ;
for (j=0; j<col; j++ ) { cout << db_XReal[i][j] << " + " ; cout << db_XImag[i][j] << "i" << "\t\t" ; } cout << endl ; } cout << endl ;
*/
; ;
end code
253
void Test::InverseFastFourierTrans2D()
,\ ,\ };
double YImag[3][4] =
{{ 0, { 7.6210, {-7.6210,
0, 9.5263, -9.5263,
/* convert C/C++ double to mxArray */ mwArray mw_Y = double2mwArray_matrixComplex (&YReal[0][0], &YImag[0][0], row, col) ;
/* call an implemental function */ mwArray mw_X(row, col, mxDOUBLE_CLASS, mxCOMPLEX) ; myifft2 (1, mw_X, mw_Y);
*/
double **db_XReal = new double* [row] ; double **db_XImag = new double* [row] ;
/* print out */ cout << "Inverse 2-D Fast Fourier Transform of Y : " << endl ;
254
for (i=0; i<row; i++) {
for (j=0; j<col; j++ ) { cout << db_XReal[i][j] << " + " ; cout << db_XImag[i][j] << "i" << "\t\t" ; } cout << endl ; } cout << endl ;
*/ ; ;
end code
Chapter 15
Following are steps to create a C shared library eigenlib.dll and a C++ shared library cppeigenlib.dll which will be used to solve problems in the next sections.
We will write the M-le myeig.m as shown below. These functions will be used to generate the C and C++ shared libraries. function [V,D] = myeig(A)
[V,D] = eig(A) ;
eigenlib_mcc_component_data.c
Add and set these les to the MSVC .Net project as described in Chapter 6.
256 3. In the next sections, well use the following implemental function in this library to solve the common eigen-problems (open the le eigenlib.h to see the name of this function): void mlfMyeig(int nargout, mxArray** V, mxArray** D, mxArray* A);
cppeigenlib_mcc_component_data.c
Add and set these les to the MSVC .Net project as described in Chapter 6. 3. In the next sections, well use the following implemental function in this library to solve the common eigen-problems (open the le cppeigenlib.h to see the name of this function): void myeig(int nargout, mwArray& V, mwArray& D, const mwArray& A);
15.1
Problem 1
input
a square matrix A 0 6 2 20 1
A= 6 5 output
16 10
int main() {
cout << "Eigenvalues and eigenvectors" << endl ; Test obj ; obj.EigValueVector() ;
return 0 ; }
/* Example.h */
Test ()
mclInitializeApplication(NULL,0); eigenlibInitialize(); }
~Test ()
eigenlibTerminate(); mclTerminateApplication(); }
258
int row = 3 ; int col = 3 ; int i, j ;
*/
double **db_eigenvectorsReal = new double* [col] ; double **db_eigenvectorsImag = new double* [col] ;
double **db_eigenvaluesReal = new double* [row] ; double **db_eigenvaluesImag = new double* [row] ;
/* print out */
259
cout << "Eigenvalues of the matrix A : " << endl ;
cout << db_eigenvaluesReal[i][i] << " + " ; cout << db_eigenvaluesImag[i][i] << "i" << endl ; } cout << endl ;
for (j=0; j<col; j++ ) { cout << "Eigenvector " << (j+1) << " is : " << endl ;
cout << db_eigenvectorsReal[i][j] << " + " ; cout << db_eigenvectorsImag[i][j] << "i" << endl ; }
*/
mxDestroyArray(mx_eigenvalues ) ; mxDestroyArray(mx_eigenvectors) ;
end code
260
Listing code
/* Example.cpp */
int main() {
cout << "Eigenvalues and eigenvectors" << endl ; Test obj ; obj.EigValueVector() ;
return 0 ; }
/* Example.h */
class Test {
Test ()
mclInitializeApplication(NULL,0); cppeigenlibInitialize(); }
~Test ()
cppeigenlibTerminate(); mclTerminateApplication();
261
}
} ;
/* call an implemental function */ mwArray mw_eigenvectors(row, col, mxDOUBLE_CLASS, mxCOMPLEX) ; mwArray mw_eigenvalues (row, col, mxDOUBLE_CLASS, mxCOMPLEX) ;
*/
double **db_eigenvectorsReal = new double* [col] ; double **db_eigenvectorsImag = new double* [col] ;
double **db_eigenvaluesReal = new double* [row] ; double **db_eigenvaluesImag = new double* [row] ;
262
/* print out */ cout << "Eigenvalues of the matrix A : " << endl ;
cout << db_eigenvaluesReal[i][i] << " + " ; cout << db_eigenvaluesImag[i][i] << "i" << endl ; } cout << endl ;
for (j=0; j<col; j++ ) { cout << "Eigenvector " << (j+1) << " is : " << endl ;
cout << db_eigenvectorsReal[i][j] << " + " ; cout << db_eigenvectorsImag[i][j] << "i" << endl ; }
*/
end code See the code of the les mxUtilityCompilerVer4.h and mwUtilityCompilerVer4.h in Chapter 7. Remarks 1. The MATLAB function [V,D] = eig(A) assigns eigenvalues D as a diagonal matrix, in which the eigenvalues are diagonal terms. The above programming gives the eigenvalues in
0.00000 0.00000
0.24645 1.76008i
then the eigenvalues of the matrix A are: eigenvalue1 = 0.30710 eigenvalue2 = 0.24645 + 1.76008i eigenvalue3 = 0.24645 1.76008i
2. The MATLAB function [V,D] = eig(A) also assigns eigenvectors V as a matrix, in which the eigenvectors are matrix columns. The above programming gives the value as follows: 0.83261 0.20027 0.13936i 0.20027 + 0.13936i eigenvectors = 0.35534 0.21104 0.64472i 0.21104 + 0.64472i 0.42485 0.69301 0.69301 then the eigenvectors of the matrix A are: 0.83261 0.20027 0.13936i eigenvector1 = 0.35534 , eigenvector2 = 0.21104 0.64472i 0.42485 0.69301 0.20027 + 0.13936i , and eigenvector3 = 0.21104 + 0.64472i 0.69301
264
Chapter 16
Random Numbers
In this chapter well generate a C shared library randomlib and a C++ shared library cpprandomlib from common M-les working on random problems. The generated functions of these libraries will be used in a MSVC .Net project to solve the typical random problems.
Following are steps to create a C shared library randomlib.dll and a C++ shared library cpprandomlib.dll which will be used to solve problems in the next sections.
We will write the M-les, myrand.m and myrandn.m, as shown below. These functions will be used to generate the C and C++ shared libraries.
function Y = myrand(m,n)
Y = rand(m,n) ;
function Y = myrandn(m,n)
Y = randn(m,n) ; %
266 2. MATLAB Compiler 4 will create eight les for this C shared library: randomlib.c randomlib.exp randomlib.lib randomlib.ctf randomlib.exports randomlib.dll randomlib.h
randomlib_mcc_component_data.c
Add and set these les to the MSVC .Net project as described in Chapter 6. 3. In the following sections, well use the following implemental functions in this library to solve the common random problems (open the le randomlib.h to see the names of these functions): void mlfMyrand (int nargout, mxArray** Y, mxArray* m, mxArray* n); void mlfMyrandn(int nargout, mxArray** Y, mxArray* m, mxArray* n);
cpprandomlib_mcc_component_data.c
Add and set these les to the MSVC .Net project as described in Chapter 6. 3. In the following sections, well use the following implemental functions in this library to solve the common random problems (open the le cpprandomlib.h to see the names of these functions): void myrand(int nargout, mwArray& Y, const mwArray& m, const mwArray& n); void myrandn(int nargout, mwArray& Y, const mwArray& m, const mwArray& n);
16.1
This section describe how to use the function mlfMyrand(..) in the generated randomlib library or myrand(..) in the generated cpprandomlib library to nd uniform random numbers. Uniform random numbers are random numbers that lie within a specied range. This section describe
267 how to use the function mlfMyrand(..) or myrand(..) in the generated libraries to solve uniform random number problems. These functions use the MATLAB function rand(m, n) to generate a matrix (size m n) of random numbers. These random numbers are uniformly distributed in the interval (0,1). For more information of this function rand(m, n), refer to the MATLAB manual [7].
16.1.1
Problem 1
input
A number, N = 5
output
/* Example.cpp */
int main() {
return 0 ; }
/* Example.h */
268
#include "randomlib.h" #include "mxUtilityCompilerVer4.h"
class Test {
Test ()
mclInitializeApplication(NULL,0); randomlibInitialize(); }
~Test ()
randomlibTerminate(); mclTerminateApplication(); }
/* step 1 : declare mxArray variables */ mxArray *mx_row = NULL ; mxArray *mx_col = NULL ; mxArray *mx_uniformRandVector = NULL ;
/* step 3 : convert C/C++ double to mxArray */ double2mxArray_scalarReal (row, mx_row) ; double2mxArray_scalarReal (col, mx_col) ;
269
/* step 4 : call an implemental function */ mlfMyrand(1, &mx_uniformRandVector, mx_row, mx_col);
*/
/* print out */ cout << endl ; cout << "Uniform random numbers from 0 to 1 :" << endl ; for (i=0; i<col; i++) {
*/
mxDestroyArray(mx_uniformRandVector) ;
delete [] db_uniformRandVector ;
end code
/* Example.cpp */
int main() {
270
Test obj ;
obj.uniformRandom_vector () ;
return 0 ; }
/* Example.h */
class Test {
Test ()
mclInitializeApplication(NULL,0); cpprandomlibInitialize(); }
~Test ()
cpprandomlibTerminate(); mclTerminateApplication(); }
271
mwArray mw_col(1, 1, mxDOUBLE_CLASS) ; mw_col(1,1) = col ;
/* call an implemental function */ mwArray mw_uniformRandVector(row, col, mxDOUBLE_CLASS) ; myrand(1, mw_uniformRandVector, mw_row, mw_col);
*/
/* print out */ cout << endl ; cout << "Uniform random numbers from 0 to 1 :" << endl ; for (i=0; i<col; i++) {
/* free memories
*/
delete [] db_uniformRandVector ;
end code
16.1.2
Problem 2 input A number, N = 6 A range [a, b], where a=2 and b=18
output
272
double a = 2.0
double b = 18.0 ;
/* step 1 : declare mxArray variables */ mxArray *mx_row = NULL ; mxArray *mx_col = NULL ; mxArray *mx_uniformRandVector = NULL ;
/* step 3 : convert C/C++ double to mxArray */ double2mxArray_scalarReal (row, mx_row) ; double2mxArray_scalarReal (col, mx_col) ;
/* step 4 : call an implemental function */ /* random numbers in [0, 1] */ mlfMyrand(1, &mx_uniformRandVector, mx_row, mx_col);
*/
/* print out */ cout << endl ; cout << "Uniform random numbers from a=2 to b=18 : " << endl ; for (i=0; i<col; i++) {
273
} cout << endl ;
*/
mxDestroyArray(mx_uniformRandVector) ;
delete [] db_uniformRandVector ;
end code
void Test::uniformRandom_vector2 ()
double a = 2.0
double b = 18.0 ;
/* call an implemental function */ /* random numbers in [0, 1] */ mwArray mw_uniformRandVector(row, col, mxDOUBLE_CLASS) ; myrand(1, mw_uniformRandVector, mw_row, mw_col);
274
/* convert back to C/C++ double */
/* print out */ cout << endl ; cout << "Uniform random numbers from a=2 to b=18 : " << endl ; for (i=0; i<col; i++) {
/* free memories
*/
delete [] db_uniformRandVector ;
end code
16.1.3
output Generating a matrix (size m n) of uniform random numbers in the range [0,1]
void Test::uniformRandom_matrix ()
275
mxArray *mx_row = NULL ; mxArray *mx_col = NULL ; mxArray *mx_uniformRandMatrix = NULL ;
/* step 3 : convert C/C++ double to mxArray */ double2mxArray_scalarReal (row, mx_row) ; double2mxArray_scalarReal (col, mx_col) ;
*/
mxArray2double_matrixReal(mx_uniformRandMatrix, db_uniformRandMatrix) ;
/* print out */ cout << endl ; cout << "The matrix of uniform random numbers from 0 to 1 :" << endl ; for (i=0; i<row; i++) { {
cout << db_uniformRandMatrix[i][j] << "\t" ; } cout << endl ; } cout << endl ;
*/
mxDestroyArray(mx_uniformRandMatrix) ;
276
delete [] db_uniformRandMatrix ;
end code
/* call an implemental function */ mwArray mw_uniformRandMatrix(row, col, mxDOUBLE_CLASS) ; myrand(1, mw_uniformRandMatrix, mw_row, mw_col);
*/
mwArray2double_matrixReal(mw_uniformRandMatrix, db_uniformRandMatrix) ;
/* print out */ cout << endl ; cout << "The matrix of uniform random numbers from 0 to 1 :" << endl ; printMatrix(db_uniformRandMatrix, row, col) ; cout << endl ;
277
delete [] db_uniformRandMatrix ; }
end code
16.1.4
Problem 4
input
A row number m = 8 and a column number n = 5 Range [a, b], where a=4 and b=17
output Generating a matrix (size m n) of uniform random numbers in the range [a, b]
void Test::uniformRandom_matrix ()
/* step 1 : declare mxArray variables */ mxArray *mx_row = NULL ; mxArray *mx_col = NULL ; mxArray *mx_uniformRandMatrix = NULL ;
278
double2mxArray_scalarReal (col, mx_col) ;
*/
mxArray2double_matrixReal(mx_uniformRandMatrix, db_uniformRandMatrix) ;
/* print out */ cout << endl ; cout << "The matrix of uniform random numbers from 0 to 1 :" << endl ; for (i=0; i<row; i++) { {
cout << db_uniformRandMatrix[i][j] << "\t" ; } cout << endl ; } cout << endl ;
*/
mxDestroyArray(mx_uniformRandMatrix) ;
delete [] db_uniformRandMatrix ;
end code
double a = 4.0
double b = 17.0 ;
/* call an implemental function */ mwArray mw_uniformRandMatrix(row, col, mxDOUBLE_CLASS) ; myrand(1, mw_uniformRandMatrix, mw_row, mw_col);
*/
mwArray2double_matrixReal(mw_uniformRandMatrix, db_uniformRandMatrix) ;
/* print out */ cout << endl ; cout << "The matrix of uniform random numbers from a to b :" << endl ; for (i=0; i<row; i++) { {
cout << a + (b-a)*db_uniformRandMatrix[i][j] << "\t" ; } cout << endl ; } cout << endl ;
280
/* free memories */
delete [] db_uniformRandMatrix ;
end code
16.2
Normal random numbers are random numbers that establish the normal distribution (Gaussian distribution). This section describe how to use the function mlfMyrandn(..) in the generated randomlib library or myrandn(..) in the generated randomlib to solve normal random number problems. These functions use the MATLAB function randn(m, n) to generate a matrix (size n by m) of random numbers. These random numbers are normally distributed with specied properties, = 0, variance 2 = 1.
16.2.1
Problem 5
input
A number, N = 5
output
void Test::normalRandom_vector ()
281
/* step 1 : declare mxArray variables */ mxArray *mx_row = NULL ; mxArray *mx_col = NULL ; mxArray *mx_normalRandVector = NULL ;
/* step 3 : convert C/C++ double to mxArray */ double2mxArray_scalarReal (row, mx_row) ; double2mxArray_scalarReal (col, mx_col) ;
*/
/* print out */ cout << endl ; cout << "Normal random numbers :" << endl ; for (i=0; i<col; i++) {
*/
mxDestroyArray(mx_normalRandVector) ;
delete [] db_normalRandVector ; }
end code
282
Listing code
void Test::normalRandom_vector ()
/* call an implemental function */ mwArray mw_normalRandVector(row, col, mxDOUBLE_CLASS) ; myrandn(1, mw_normalRandVector, mw_row, mw_col);
*/
/* print out */ cout << endl ; cout << "Normal random numbers :" << endl ; for (i=0; i<col; i++) {
/* free memories
*/
delete [] db_normalRandVector ;
end code
283
16.2.2
output
Generating N normal random numbers with specied properties: mean = 0.56 variance 2 = 0.12
Listing code
void Test::normalRandom_vector2 ()
double mean_mu
= 0.56 ;
/* step 1 : declare mxArray variables */ mxArray *mx_row = NULL ; mxArray *mx_col = NULL ; mxArray *mx_normalRandVector = NULL ;
284
double2mxArray_scalarReal (col, mx_col) ;
*/
cout << "Normal random numbers :" << endl ; for (i=0; i<col; i++) {
*/
mxDestroyArray(mx_normalRandVector) ;
delete [] db_normalRandVector ;
end code
Listing code
void Test::normalRandom_vector2 ()
285
int row = 1 ; int col = 5 ;
double mean_mu
= 0.56 ;
/* call an implemental function */ mwArray mw_normalRandVector (row, col, mxDOUBLE_CLASS) ; myrandn(1, mw_normalRandVector, mw_row, mw_col);
*/
cout << "Normal random numbers :" << endl ; for (i=0; i<col; i++) {
/* free memories
*/
delete [] db_normalRandVector ;
end code
286
16.2.3
Problem 7
input
output
Generating a matrix (size m n) of normal random numbers with specied properties: mean = 0 variance 2 = 1
Listing code
void Test::normalRandom_matrix ()
/* step 1 : declare mxArray variables */ mxArray *mx_row = NULL ; mxArray *mx_col = NULL ; mxArray *mx_normalRandMatrix = NULL ;
/* step 3 : convert C/C++ double to mxArray */ double2mxArray_scalarReal (row, mx_row) ; double2mxArray_scalarReal (col, mx_col) ;
287
/* step 5 : convert back to C/C++ double */
mxArray2double_matrixReal(mx_normalRandMatrix, db_normalRandMatrix) ;
/* print out */ cout << endl ; cout << "The matrix of normal random numbers from 0 to 1 :" << endl ; printMatrix(db_normalRandMatrix, row, col) ; cout << endl ;
*/
mxDestroyArray(mx_normalRandMatrix) ;
delete [] db_normalRandMatrix ;
end code
Listing code
void Test::normalRandom_matrix ()
288
mwArray mw_col(1, 1, mxDOUBLE_CLASS) ; mw_col(1,1) = col ;
/* call an implemental function */ mwArray mw_normalRandMatrix(row, col, mxDOUBLE_CLASS) ; myrandn(1, mw_normalRandMatrix, mw_row, mw_col);
*/
mwArray2double_matrixReal(mw_normalRandMatrix, db_normalRandMatrix) ;
/* print out */ cout << endl ; cout << "The matrix of normal random numbers from 0 to 1 :" << endl ; printMatrix(db_normalRandMatrix, row, col) ; cout << endl ;
/* free memories
*/
delete [] db_normalRandMatrix ;
end code
16.2.4
Problem 8
input
output
Generating a matrix (size m n) of normal random numbers with specied properties: mean = 0.56 variance 2 = 0.12
289
Listing code
void Test::normalRandom_matrix2 ()
double mean_mu
= 0.56 ;
/* step 1 : declare mxArray variables */ mxArray *mx_row = NULL ; mxArray *mx_col = NULL ; mxArray *mx_normalRandMatrix = NULL ;
/* step 3 : convert C/C++ double to mxArray */ double2mxArray_scalarReal (row, mx_row) ; double2mxArray_scalarReal (col, mx_col) ;
*/
290
mxArray2double_matrixReal(mx_normalRandMatrix, db_normalRandMatrix) ;
/* print out */ cout << endl ; cout << "The matrix of normal random numbers " ; cout << "at specified mean and variance" << endl for (i=0; i<row; i++) { { ;
cout << mean_mu + standard_deviation*db_normalRandMatrix[i][j] << "\t" ; } cout << endl ; } cout << endl ;
*/
mxDestroyArray(mx_normalRandMatrix) ;
delete [] db_normalRandMatrix ;
end code
Listing code
void Test::normalRandom_matrix2 ()
double mean_mu
= 0.56 ;
291
double standard_deviation = sqrt (variance) ;
/* call an implemental function */ mwArray mw_normalRandMatrix(row, col, mxDOUBLE_CLASS) ; myrandn(1, mw_normalRandMatrix, mw_row, mw_col);
*/
mwArray2double_matrixReal(mw_normalRandMatrix, db_normalRandMatrix) ;
/* print out */ cout << endl ; cout << "The matrix of normal random numbers " ; cout << "at specified mean and variance" << endl for (i=0; i<row; i++) { { ;
cout << mean_mu + standard_deviation*db_normalRandMatrix[i][j] << "\t" ; } cout << endl ; } cout << endl ;
*/
delete [] db_normalRandMatrix ;
end code
292
Chapter 17
17.1
output . Call the MATLAB workspace to perform the task, c = a + b, in a C++ function . Get the result c in the MATLAB workspace and transfer back to the C++ function
The following is the code to solve Problem 1 by using MATLAB Engine in MATLAB Compiler 4.
Listing code
/* Example.cpp */ /* showing the purpose of calling the MATLAB workspace to perform a particular task.
296
Procedure: 1. convert C/C++ double to mxArray 2. convert an mwArray name to a new name for using in the MATLAB workspace 3. ask the MATLAB workspace performing the particular tasks 4. get results in the MATLAB workspace then convert back to mxArray 5. convert the results mxArray to C++ double */
int main()
cout << "Using MATLAB Engine to call the MATLAB workspace " << endl ; cout << "performing the particular tasks " << endl ;
Test obj ;
obj.SimplePlus() ; obj.LinearSolve() ;
return 0 ;
/* Example.h */
class Test {
public:
void SimplePlus () ;
297
Engine *ep ;
Test ()
~Test ()
engClose(ep) ;
} ;
// step1: declare mxArray variables to use mxArray *mx_a = NULL ; mxArray *mx_b = NULL ; mxArray *mx_c = NULL ;
// step2: create mxArray variables mx_a = mxCreateDoubleMatrix(1, 1, mxREAL) ; mx_b = mxCreateDoubleMatrix(1, 1, mxREAL) ; mx_c = mxCreateDoubleMatrix(1, 1, mxREAL) ;
298
// step4: convert name mx_a to ml_a to use in the MATLAB workspace engPutVariable(ep, "ml_a", mx_a) engPutVariable(ep, "ml_b", mx_b) ; ;
// step5: perform a task in MATLAB workspace engEvalString( ep, "ml_c = ml_a + ml_b ;" ) ;
// step6: get result in Matlab workspace then convert to mxArray mx_c = engGetVariable(ep, "ml_c" ) ;
// step9: print out cout << "The result from the simple plus:"<< endl ; cout << db_c << endl ;
17.2
Problem 2
input
1.1 5.6 3.3 A = 4.4 12.3 6.6 7.7 8.8 9.9 output
. Call the MATLAB workspace to perform the tasks as following in a C++ function:
299 a) Finding the solution x of linear system equations Ax = b b) Calculating the upper matrix U in the LU decompression method c) Getting results in the MATLAB workspace and converting to C++ double
The following is the code to solve Problem 2 by using MATLAB Engine in MATLAB Compiler 4.
Listing code
void Test::LinearSolve()
int i ;
double A[3][3] = { {1.1, 5.6, 3.3}, double b[3] = { 12.5, 32.2 , 45.6 } ;
; ;
/* assign memory */ mx_A mx_U mx_b = mxCreateDoubleMatrix(row, col, mxREAL) = mxCreateDoubleMatrix(row, col, mxREAL) = mxCreateDoubleMatrix(row, 1 , mxREAL) 1 , mxREAL) ; ; ; ;
mx_vectorX = mxCreateDoubleMatrix(row,
300
/* convert C/C++ double to mxArray */ double2mxArray_vectorReal(b, mx_b) ; double2mxArray_matrixReal(&A[0][0], mx_A) ;
Matlab workspace tasks : Solve Ax = b, and get a vector x and an upper matrix U. The problem Ax = b is solved here as the purpose of showing the use of the MATLAB workspace in a C/C++ function. */
; ;
engEvalString(ep, " ml_vectorX = mldivide(ml_A, ml_b) ; " engEvalString(ep, " [ml_L, ml_U, ml_P] = lu( ml_A ) ; "
) ; ) ;
/* get results in the Matlab workspace then convert to mxArray */ mx_vectorX = engGetVariable(ep, "ml_vectorX" ) mx_U = engGetVariable(ep, "ml_U" ) ; ;
*/
/* print out */ cout << "Solution of the equation Ax = b is: " << endl ;
cout<< "The upper matrix U :" << endl; printMatrix(db_matrixU, row, col) ;
301
/* free memory */ mxDestroyArray(mx_A) ; mxDestroyArray(mx_b) ; mxDestroyArray(mx_vectorX) ; mxDestroyArray(mx_U) ;
end code
17.3
++
Func-
In this section we will directly create a MATLAB graphic by performing a plotting task in the MATLAB workspace. The steps are: 1. Set up a project in Microsoft Visual C++ .Net (MSVC.Net) for working with MATLAB Compiler 4 as described in Chapter 5. 2. Write a code to call the MATLAB workspace to perform the plotting task in a C++ function. Following is the code to create the graphic. The gure is created as shown in Fig. 17.1. Listing code
double X[3] = { 12.5, 32.2 , 45.6 } ; double Y[3] = { 12.5, 32.2 , 45.6 } ;
int vectorSize = 3 ;
/* step1: declare mxArray variables */ mxArray *mx_X mxArray *mx_Y = NULL ; = NULL ;
/* step2: create mxArray vectors and mxArray matrixes */ mx_X mx_Y = mxCreateDoubleMatrix(vectorSize, 1 , mxREAL) = mxCreateDoubleMatrix(vectorSize, 1 , mxREAL) ; ;
302
// step3: Convert C/C++ double to mxArray double2mxArray_vectorReal(X, mx_X) ; double2mxArray_vectorReal(Y, mx_Y) ;
/* step4:
/* step5: perform tasks in the Matlab workspace */ engEvalString(ep, " plot(ml_X, ml_Y ,r); " ) ;
/* keep the figure */ cout << "Hit return to close the figure and continue" ; cout << endl ; fgetc(stdin);
end code
303 Remark With directly creating the graphic by MATLAB Engine, you can use the graphic features as Insert, Tool, etc. You need to hit Enter key to close the gure as shown in the line of the code fgetc(stdin).
304
Chapter 18
18.1
Suppose that there is an existing C function mysquare(..) in the le mysquare.c (see the code below and note that the function name and the le name are the same). To call this function in the MATLAB workspace, we write the command, mex mysquare.c ;, MATLAB will generate a le mysquare.dll that contains the function mysquare(..). This function mysquare(..) will be called into the MATLAB workspace to calculate the square value. The example code in the MATLAB workspace is: >> mex mysquare.c ; >> x = 1.2 ; >> square = mysquare(x) ;
square =
1.4400
>> The following is the MEX-function code of the function double mysquare(double x).
Listing code
/* mysquare.c */
#include "mex.h" double mysquare(double x) { double y = x*x ; return y ; } /* ************************* */ void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
end code
307
18.2
In this section, we will show how to write a MEX-function for an existing C function that has the input/output as vectors. This function will then be called into the MATLAB workspace.
Suppose that there is an existing C function vectortimes(..) in the vectortimes.c le (note that the C function name and the C le name are the same). To call this function in the MATLAB workspace we write the command, mex vectortimes.c ;, MATLAB will generate a le vectortimes.dll that contains the function vectortimes(..). This function vectortimes(..) will be called into the MATLAB workspace to calculate the value. The example code in the MATLAB workspace is: >> mex vectortimes.c ; >> X = [1.1 2.2 3.3 4.4 ] ; >> factor = 3.4 ; >> Y = vectortimes(X, factor) ; >> Y
Y =
3.7400
7.4800
11.2200
14.9600
Listing code
/* vectortimes.c */ #include "mex.h"
*/
308
/* ****************************** */ void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
/* step 1
double int
db_factor ; vectorSize ;
/* step 2
/* step 3
/* step 4
/* step 5
end code
18.3
In this section, we will show how to write a MEX-function for a C function that has the input/output as matrixes. This function then will be called into MATLAB workspace.
Suppose that there is an existing C function, matrixtimes(..), in the le matrixtimes.c (note that C function name and C le name are the same). To call this function in the MATLAB workspace we write the command, mex matrixtimes.c ;, MATLAB will generate a le matrixtimes.dll that contains the function matrixtimes(..). This function matrixtimes(..) will be called from the MATLAB workspace to calculate the value. The example code in the MATLAB
309 workspace is: >> mex matrixtimes.c ; >> >> A = [ 1.1 2.2 3.3 ; 4.4 5.5 6.6 ]
A =
1.1000 4.4000
2.2000 5.5000
3.3000 6.6000
B =
6.0500 24.2000
12.1000 30.2500
18.1500 36.3000
>> The following is the MEX-function code of the function void matrixtimes(..).
Listing code
/* matrixtimes.c */
output */
: . matrix
310
void DoubleMatrixToDoubleVector(double **db_matrix, int row, int col, double *db_vector) { int i, j, index ; for(j=0; j<col; j++) { {
db_vector[index]
= db_matrix [i][j] ;
} } } /* **************************** */ void DoubleVectorToDoubleMatrix(double **db_matrix, int row, int col, double *db_vector) { int i, j, index ;
{ {
= j*row + i ;
} } }
/* **************************** */ /* **************************** */
void matrixtimes( double **x, int row, int col, double factor, double **y ) { /* calculate y = x*k */ int i, j ; for (i=0; i<row; i++) { {
y[i][j] = x[i][j]*factor ;
} } } /* **************************** */
311
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { /* procedure : input prhs[0] --> vectorX --> matrixX --> call matrixtimes(..) get matrixY --> matrixY --> vectorY --> output plhs[0] */
/* step 1
double
db_factor ;
/* step 2 :
db_factor
= mxGetScalar( prhs[1] ) ;
/* step 3 :
db_matrixX = (double **)malloc( row * sizeof(double *) ); db_matrixY = (double **)malloc( row * sizeof(double *) );
for(i = 0; i < row; i++) { db_matrixX[i] = (double *)malloc( col * sizeof(double) ); db_matrixY[i] = (double *)malloc( col * sizeof(double) ); }
/* step 4 : transfer C vector to C matrix. The matrix is used as a buffer input in calling function matrixtimes */
312
/* step 5 : assign memory for return value */ plhs[0] = mxCreateDoubleMatrix(row, col, mxREAL) ;
/* step 6
/* step 7
/* free memory */
free(db_matrixX); free(db_matrixY);
/* Note: Matrixes db_matrixX and db_matrixY are used as the buffer variables. Therefore their memories are need to free after using. */
end code
313
18.4
MEX-Function Analysis
Going through three above examples will give us a general understanding of writing a MEXfunction. This section is a more in-depth analysis of the MEX-function. The MEX-function features are: 1. The MEX-function has the form,
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]);
2. The arguments in this void mexFunction(..) are: nlhs *plhs[] is number of the left hand side representing number of outputs is pointer to the left hand side representing the outputs, plhs[0] is rst output plhs[1] is second output ... plhs[n] is (n + 1)th output nrhs *prhs[] is number of the right hand side representing number of inputs is pointer to the right hand side representing the inputs, prhs[0] is rst input prhs[1] is second input ... prhs[n] is (n + 1)th input
outputs
MEX-function
inputs
3. The matrix input prhs[i] and matrix output plhs[i] of mexFunction(..) has the relationship with C programming in a vector form (double* ), not in a matrix form (double** ). Therefore, we need to transfer the matrix form to the vector form before assigning the matrix to the input or output of mexFunction(..). For example in Section 18.3, the code to perform this task is: /* step 2 : assign value for input */
314
/* step 6
db_vectorY = mxGetPr( plhs[0] ) ; 4. The function, void matrixtimes( double **x, int row, int col, double factor, double **y ) , in the le matrixtimes.c is dierent from the function that we called in the MATLAB workspace, matrixtimes(A, k). The dierence is in the arguments. And note that, the function that we call in the MATLAB workspace, matrixtimes(A, k), is established from mexFuncion(..). In mexFuncion(..), we determine the inputs and outputs for the function, B = matrixtimes(A, k), that is called in the MATLAB workspace.
Chapter 19
Stand-Alone Applications
MATLAB Compiler 4 provides the method to create stand-alone applications from either entirely of M-les or some combination of M-les, MEX-les, and C/C++ source code les. These standalone applications then can be used in the target machine which doesnt have the MATLAB software. This chapter describes how to generated and use the stand-alone applications. These applications are executable les and generated from the M-les.
19.1
To use the stand-alone applications, we need to install MATLAB Component Runtime (MCR) to the target machine. The steps of installing are: 1. Copy the le MCRInstaller.exe to your target machine (MCRInstaller.exe is located in the directory ..\MATLAB7\toolbox\compiler\deploy\win32\). Double-click it to install.
316
3. Click Next, Fig. 19.3 appears. You can choose any directory to install. In here we choose a default directory.
317
In Fig. 19.3 choosing Everyone or Just me is not eective on your stand-alone application. The following sections well write several examples of M-les for stand-alone applications. Well compile its to get the executable les, then use in Command Prompt. There are some limitations for M-les in generating stand-alone applications, we refer to page 1-19 of [2].
19.2
In this section well write a stand-alone application for a simple addition operator. The steps of this procedure are: 1. Writing the M-le myaddition.m to generate the executable le myaddition.exe as follows: function myaddition(a, b)
c = a + b 2. Write the command in Command Prompt (see Fig. 19.5): mcc -m myaddition.m
318
3. MATLAB Compiler will create ve les: myaddition.ctf myaddition.exe myaddition.m myaddition_main.c myaddition_mcc_component_data.c 4. Copy two les myaddition.ctf and myaddition.exe to your target machine. 5. Execute this stand-alone application by write the command in Command Prompt (see Fig. 19.6): myaddition.exe 1.2 3.4
Note You will receive a warning as following, but this is a bug and MathWorks Inc. will x it. This warning does not aect the compilation or execution of the program. See this warning at: www.mathworks.com/support/solutions/data/1-ON0NN.html?solution=1-ON0NN. The warning is:
319 Warning: C:\Program Files\MathWorks\MATLAB Component Runtime\v70\toolbox\ local\pathdef.m not found. Toolbox Path Cache is not being used. Type help toolbox_path_cache for more info. The inputs of the executable le are separated by the space(s). The inputs of the executable le are recognized as characters not a number. Therefore in the M-le myaddition.m we wrote lines of code to convert these characters to a number before using. The procedure for examples in the next sections is similar in this section.
19.3
In this subsection, well write a stand-alone application for the linear equations to solve the solution x from Ax = b. The input values of a vector b and a matrix A are stored in the les vectorb.dat and matrixa.dat, respectively. The output solution x is written to the le mysolution.dat.
In this example, we have the input les as follows. vectorb.dat 12.5 32.2 45.6 matrixa.dat 1.1 4.4 7.7 5.6 12.3 8.8 3.3 6.6 9.9
The steps to create this stand-alone application are: 1. Writing the M-le mylinear.m to generate the executable le mylinear.exe as follows: function mylinear(matrixsize)
% MATLAB reads a matrix in column major % we need A as a matrix in row major (C/C++ format) A = A ;
x = A\b ;
2. Write the command in Command Prompt to generated les of the stand-alone application: mcc -m mylinear.m 3. Copy two les mylinear.ctf and mylinear.exe to the target machine. 4. Execute this stand-alone application by write the command in Command Prompt: mylinear.exe 3 5. MATLAB Component Runtime in the target machine will execute the le mylinear.exe and create the le mysolution.dat which includes the solution x.
19.4
In this subsection, well write a stand-alone application for using MATLAB plots. The input values of x and y are stored in les xdata.dat and ydata.dat, respectively. In this example, we have the input les as follows. xdata.dat 1 2 3 4
321 ydata.dat 21.1 22.2 23.3 24.4 41.1 42.2 43.3 44.4
The steps to create this stand-alone application are: 1. Writing the M-le myplot.m to generate the executable le myplot.exe. function myplot(xdatafile, ydatafile)
hold on ;
plot(x,y1) ; plot(x,y2) ;
hold off
2. Write the command in Command Prompt to generate les for the stand-alone application: mcc -m myplot.m 3. Copy two les myplot.ctf and myplot.exe to the target machine. 4. Execute this stand-alone application by write the command in Command Prompt: myplot.exe xdata.dat ydata.dat 5. MATLAB Component Runtime in the target machine will execute the le myplot.exe and create the gure as shown in Fig. 19.7.
322
19.5
In this subsection, well write a stand-alone application for calculating an integration. The steps to create this stand-alone application are: 1. Writing the M-le myquad.m to generate the executable le myquad.exe.
y = quad(F, a, b) 2. Write the command in Command Prompt to generated les of the stand-alone application: mcc -m myquad.m 3. Copy two les myquad.ctf and myquad.exe to the target machine.
I=
0.1
To calculate I, execute the stand-alone application by write the command in Command Prompt: myquad.exe cos(2*x)+power(x,2)+1.2*x-0.24 0.1 1.2
MATLAB Component Runtime in the target machine will execute the le myquad.exe (Fig. 19.8) to calculate I.
Note First input variable of myquad.exe is an expression string. This expression string does not include the space, because the executable le recognizes the space as the symbol of separation of input variables. In the expression string, try to use the function name instead of a special character. For example, use the function name power(..) instead of the character .
324
Bibliography
[1] William H. Press, Saul A. Teukolsky, William T. Vetterling, and Brian P. Flannery. Numerical Recipes in C. Cambridge University Press, 1992. [2] Inc. The MathWork. Extermal Interfaces References. Version 7 The Language of Technical Computing. URL address: www.mathworks.com/access/helpdesk/help/pdf_doc/matlab/apiref.pdf. [3] Inc. The MathWork. MATLAB compiler version 4, user guide. The Language of Technical Computing. URL address: www.mathworks.com/access/helpdesk/help/pdf_doc/compiler/compiler4.pdf. [4] Inc. The MathWork. MATLAB C++ math library version 2.1, The Language of Technical Computing. [5] Inc. The MathWork. MATLAB Function Reference. Volume 1, The Language of Technical Computing. URL address: www.mathworks.com/access/helpdesk/help/pdf_doc/matlab/refbook.pdf. [6] Inc. The MathWork. MATLAB Function Reference. Volume 2, The Language of Technical Computing. URL address: www.mathworks.com/access/helpdesk/help/pdf_doc/matlab/refbook2.pdf. [7] Inc. The MathWork. MATLAB Function Reference. Volume 3, The Language of Technical Computing. URL address: www.mathworks.com/access/helpdesk/help/pdf_doc/matlab/refbook3.pdf.
325
326
Index
Band diagonal matrix, 140 call MATLAB workspace, 273 curve tting, 161 dierences between C/C++ and MATLAB C/C++, 5 eigenvalues, 233 eigenvectors, 233 experimental data, 188 Fast Fourier Transform, 215 features, 3 function-function, 157, 214 fzero, 212 Gaussian distribution, 258 generate a C function, 4 C functions, 47 dll-le, 48 inline function, 151 integrations, 151 interpolation, 170, 173, 188 least-squares, 165 linear system equations, 115, 119, 123, 277 LU decompression method, 277 manuals, 5 MATLAB Engine, 273 327 uniform random numbers, 244 matrix addition, 93, 96 determinant, 102, 103 inversion, 104, 105 multiplication, 99 subtraction, 98 transpose, 106 MEX-le, 283 MEX-function, 4 Microsoft Visual C++ 6.0, 21 mwArray, 61 to C++ double type, 69 mwUtilityCompilerVer4.h, 83 mxArray, 61 mxUtilityCompilerVer4.h, 72 normal random numbers, 258 projects, 3 random, 243 random numbers, 258 roots of a nonlinear, 212 of a polynomial, 205, 207 set up, 17, 33 sparse matrix, 126 stand-alone application, 293 tested, 4 testing the project setting, 20 tridiagonal, 131