Examples of Programming in Matlab (2001) WW, Matlab

[ Pobierz całość w formacie PDF ]
TECHNICAL BRIEF
Examples of Programming
in M
ATLAB
®
The M
ATLAB
®
Environment
M
ATLAB
integrates mathematical computing, visualization, and a powerful language to provide a flexible environment
for technical computing. The open architecture makes it easy to use M
ATLAB
and its companion products to explore
data, create algorithms, and create custom tools that provide early insights and competitive advantages.
TABLE OF CONTENTS
Introduction to the M
ATLAB
Language of
Technical Computing
3
Comparing M
ATLAB
to C: Three Programming
Approaches to Quadratic Minimization
4
Application Development in M
ATLAB
: Tuning M-files
with the M
ATLAB
Performance Profiler
9
2
TECHNICAL BRIEF
Introduction to the M
ATLAB
Language of Technical Computing
The M
ATLAB
language is particularly well-suited to designing predictive mathematical models and developing
application-specific algorithms.
It provides easy access to a range of both elementary and advanced algorithms for numeric computing. These algorithms
include operations for linear algebra, matrix manipulation, differential equation solving, basic statistics, linear data
fitting, data reduction, and Fourier analysis. M
ATLAB
Toolboxes are add-ons that extend M
ATLAB
with specialized
functions and easy-to-use graphical user interfaces. Toolboxes are accessible directly from the M
ATLAB
interactive
programming environment.
M
ATLAB
employs the same language for both interactive computing and structured programming. The intuitive math
notation and syntax allow you to express technical ideas just as you would write them mathematically. This familiar
style and flexibility make exploration and development with M
ATLAB
very efficient.
The built-in math algorithms, optimized for matrix and vector calculations, allow you to increase efficiency in two areas:
more productive programming and optimized code performance. The resulting time savings give you fast development
cycles as well as execution speeds from within the M
ATLAB
environment that are comparable to compiled code.
The two examples on the following pages illustrate M
ATLAB
in use:
1) The first example compares M
ATLAB
to C using three approaches to a quadratic minimization problem.
2) The second example describes one user’s application of the M-file performance profiler to increase
M-file code performance.
These examples demonstrate how M
ATLAB
’s straightforward syntax and built-in math algorithms enable development
of programs that are shorter, easier to read and maintain, and quicker to develop. The embedded code samples, developed
by M
ATLAB
users in the M
ATLAB
language, illustrate how M
ATLAB
application specific toolbox functionality can be
easily accessed from the M
ATLAB
language.
Examples of Programming in M
ATLAB
3
COMPARING M
ATLAB
TO C:
THREE PROGRAMMING APPROACHES TO QUADRATIC MINIMIZATION
Introduction
Quadratic minimization is a specific form of nonlinear minimization. When a problem has a quadratic objective function
instead of a general nonlinear function (such as in standard linear least squares), we can find a minimizer more accurately
and efficiently by taking advantage of the quadratic form. This is particularly true when the quadratic problem is convex.
Some examples of these types of applications include:
Solving contact and friction problems in rigid body mechanics, typical of the kind encountered in aerospace applications
Solving journal bearing lubrication problems, useful for machine design and maintenance planning
Computing flow through a porous medium, a common application in chemistry
Selecting the best portfolio of financial investments
In this example we will use quadratic programming to solve a minimization problem. This example demonstrates the use
of M
ATLAB
to simplify an optimization task and compares that solution to the alternative C programming approach. The
three following code examples compare three approaches to the minimization problem:
M
ATLAB
The M
ATLAB
Optimization Toolbox
C code including calls to LAPACK, the library that makes up much of the mathematical core of M
ATLAB
Problem Statement
Minimize a quadratic equation such as
y = 2x
2
+
20x
2
+
6x
1
x
2
+
5x
1
with the constraint that
x
1
–
x
2
= –
2
Solution
Express the original equation in standard quadratic form. First, transform the original equation into matrix notation
x
1
x
2
T
4
6
6
40
x
1
x
2
5
0
T
x
1
x
2
y
*
*
*
+
*
such that
1
–
1
*
x
1
x
2
= –2
Second, substitute variable names for the vectors and matrices
H
4
6
6
40
, f
5
0
, A
1
–
1
, b
–2
and
x
x
1
x
2
4
TECHNICAL BRIEF
Third, rewrite the quadratic equation as
y 5 * x T * H * x 1f T* x
and the constraint equation as
A * x =b.
The surface is the graphical repre-
sentation of the quadratic function
that we are minimizing, subject to
the constraint that x1 and x2 lie
on the line x1 – x2 = –2 (repre-
sented by the blue line in the small
graphic and the dashed black line
in the large graphic). This blue line
represents the null space of the
equality constraint. The black curve
represents the values on the qua-
dratic surface where the equality
constraint is satisfied. We start
from a point on the surface (the
upper cyan circle) and the quadrat-
ic minimization takes us to the
solution (the lower cyan circle).
The quadratic form of the equation is easier to understand and to solve using M
ATLAB
’s matrix-oriented computing language.
Having transformed the original equation, we’re ready to compare the three programming approaches.
Example 1: Quadratic Minimization with M
ATLAB
M code
M
ATLAB
’s matrix manipulation and equation solving capabilities make it particularly well-suited to quadratic program-
ming problems. The constraint stated above is that
A * x =b.
In the example below we specify four values of b for which x
must be solved, corresponding to the four rows of matrix A. In general, the data is determined by the known parameters
of the problem. For example, to solve a financial optimization problem, such as portfolio optimization, the data would be
determined by the expected return of the investments in the portfolio and the covariance of the returns. Then we would
solve for the optimal weighting of the investments by minimizing the variance in the portfolio while requiring the sum of
the weights to be unity.
In this example, we use synthetic data for
H, f, A,
and
b
. Once we set up these variables, the next step is to solve for the
unknown, x. The code below roughly follows these steps:
1) Find a point in six-dimensional space that satisfies the equality constraints
A * x =b
(6 is the number of columns in
H
and in
A
).
Examples of Programming in M
ATLAB
5
[ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • materaceopole.pev.pl






  • Formularz

    POst

    Post*

    **Add some explanations if needed