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u
t
a
r
e
n
o
t
g
u
a
r
a
n
t
e
e
d
f
o
r
a
n
y
p
a
r
t
i
c
u
l
a
r
p
u
r
p
o
s
e
.
T
h
e
p
u
b
l
i
s
h
e
r
d
o
e
s
n
o
t
o
f
f
e
r
a
n
y
w
a
r
r
a
n
t
i
e
s
a
n
d
d
o
e
s
n
o
t
g
u
a
r
a
n
t
e
e
t
h
e
a
c
c
u
r
a
c
y
,
a
d
e
q
u
a
c
y
,
o
r
c
o
m
p
l
e
t
e
n
e
s
s
o
f
a
n
y
i
n
f
o
r
m
a
t
i
o
n
a
n
d
i
s
n
o
t
r
e
s
p
o
n
s
i
b
l
e
f
o
r
a
n
y
e
r
r
o
r
s
o
r
o
m
i
s
s
i
o
n
s
o
r
t
h
e
r
e
s
u
l
t
s
o
b
t
a
i
n
e
d
f
r
o
m
u
s
e
o
f
s
u
c
h
i
n
f
o
r
m
a
t
i
o
n
.
I
S
B
N
0
-
9
7
2
5
7
9
4
-
4
-
3
D
i
s
c
l
a
i
m
e
r
Contents
Preface
Part I:
Creating and Using MATLAB Shared Library to Solve Mathematical
Problems In C#
1
Introduction
1.1
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.2 Computer Software and Book Features . . . . . . . . . . . . . . . . . . . . . . . .
1.3 Reference Manuals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2 Generating a C Shared Library from MATLAB M-Files To Use in Microsoft
Visual C# .Net
2.1 Generating a C Shared Library from a MATLAB M-File . . . . . . . . . . . . . .
2.2 Calling a C Shared Library Function in C# . . . . . . . . . . . . . . . . . . . .
2.3 Generated Functions from MATLAB Compiler 4 . . . . . . . . . . . . . . . . . .
v
1
3
3
4
4
5
5
7
9
2.4 Using Functions of a C shared library in C# . . . . . . . . . . . . . . . . . . . .
10
3 Transfer of Values between C# double and mxArray
3.1 Transfer of Values between C# double and mxArray . . . . . . . . . . . . . . . .
3.2 The Utility File CsharpMatlabCompilerVer4.cs . . . . . . . . . . . . . . . . . . .
4 Matrix Computations
4.1 Matrix Addition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.2 Matrix Subtraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.3 Matrix Multiplication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.4 Matrix Determinant
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.5
Inverse Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.6 Transpose Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
11
11
16
35
37
39
40
41
42
42
i
ii
5 Linear System Equations
5.1 Linear System Equations
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5.2 Sparse Linear System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5.3 Tridiagonal System Equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5.4 Band Diagonal System Equations . . . . . . . . . . . . . . . . . . . . . . . . . . .
6 Ordinary Dierential Equations
6.1 First Order ODE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
6.2 Second Order ODE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
6.2.1 Analysis of second order ODE . . . . . . . . . . . . . . . . . . . . . . . . .
6.2.2 Using a second order ODE function . . . . . . . . . . . . . . . . . . . . .
7 Integration
7.1 Single Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
7.2 Double-Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
8 Curve Fitting and Interpolations
8.1 Polynomial Curve Fitting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
8.2 One-Dimensional Polynomial Interpolation . . . . . . . . . . . . . . . . . . . . .
8.3 Two-Dimensional Polynomial Interpolation for Grid Points
. . . . . . . . . . . .
9 Roots of Equations
9.1 Roots of Polynomials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
43
45
49
52
56
61
63
69
69
70
73
74
78
81
83
87
88
97
98
9.2 The Root of a Nonlinear-Equation . . . . . . . . . . . . . . . . . . . . . . . . . .
101
10 Fast Fourier Transform
103
10.1 One-Dimensional Fast Fourier Transform . . . . . . . . . . . . . . . . . . . . . .
105
10.2 Two-Dimensional Fast Fourier Transform . . . . . . . . . . . . . . . . . . . . . .
109
11 Eigenvalues and Eigenvectors
113
11.1 Eigenvalues and Eigenvectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
114
Part II:
Calling MATLAB Workspace in C# Functions
Using MATLAB functions in C# Windows Form Applications
12 Calling MATLAB Workspace in C# Functions
119
121
12.1 Calling MATLAB Workspace with Input/Output as a Scalar
. . . . . . . . . . .
121
12.2 Calling MATLAB Workspace with Input/Output as a Vector and a Matrix . . .
123
12.3 Generating a MATLAB Graphic from a C# Function . . . . . . . . . . . . . . .
125
13 Using MATLAB Functions In C# Windows Forms Applications
127
iii
Part III:
Creating and Using COM From MATLAB COM Builder In C#
131
14 Using COM Created from MATLAB COM Builder in C# Applications
135
14.1 Creating COM From MATLAB COM Builder . . . . . . . . . . . . . . . . . . . .
135
14.2 Using COM in C# functions
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
138
15 Using COM Created from MATLAB COM Builder in C# Applications for
Matrix Manipulation & Linear System Equations
143
15.1 Creating COM From MATLAB COM Builder . . . . . . . . . . . . . . . . . . . .
143
15.2 Using COM From MATLAB COM Builder To Calculate Matrix Multiplication .
144
15.3 Using COM From MATLAB COM Builder To Solve Linear System Equations . .
146
References
Index
149
151
iv
Preface
MATLAB provides the toolbox MATLAB Compiler to handle technical problems between MATLAB
and programming languages. The popular task is to support MATLAB built-in functions for com-
puter programmers in development. In recent years, Microsoft Corporation has been developed
C# programming language in Microsoft Visual .Net. This C# language is a power language, easy
to develop, easy to maintain, and other advanced features, specially in the memory handling. C#
is one of programming languages can use the C shared library created from MATLAB M-les. In
addition, the MATLAB COM Builder toolbox provides a special feature that the user can create
Component Object Model (COM) from MATLAB M-les. The generated COM then can be use
in the other programming languages that support COM applications. C# is one of language that
supports COM, so C# programmers can use MATLAB M-les in the wrapper COM to develop
applications as stand-alone applications. This book, MATLAB C#, implements the combination
of the advances of C# and MATLAB to solve the technical problems. The features of this book
are designed to handle the following projects:
C# functions call MATLAB built-in functions in the mathematical library created from
MATLAB M-les to solve the mathematical problems.
C# functions call the MATLAB Workspace to perform particular tasks then transfer results
from the MATLAB Workspace to C# functions.
C# functions use COM that is created from MATLAB M-les by using MATLAB COM
Builder.
The book contains all C# programming codes in all chapters, that quickly help users solve their
problems. This book tries to support C# programmers, especially college students and engineers,
who use C# and MATLAB to develop applications and solutions for their projects and designs.
LePhan Publishing
December 2004
vi