: MATLAB Programming Techniques
Problem 1:
Objective: The indexing operation for the MATLAB matrix manipulation
plays an important role in terms of efficiency and elegancy.
For the function
x4 0 x 2
x3 3 x 5
f (x) = (1)
x2 6 x 8
x 9 x 10
write a function or a short program such that when given a nx1 vector x
whose components are all in [0, 10] generate a function vector y = f (x), i.e.
given x = [1 6 3 8 10], generate y = [1 36 27 64 10] according to (1).
The indexing operation is a technique that needs no for loop nor if,else
commands. shows the beauty.
1: One of the usage of indexing operation.
1
Problem 2:
Objective: This problem is to demonstrate the importance of the matrix
computation by MATLAB as compared to the usage of the for loop which
might be the first thought coming into the mind. The difference in efficiency
will be seen when this computation needs to be done for thousands of times
and when the dimension of the probelm increases as it is the common scenario
in Monte Carlo simulations.
Let X = [x1 x2 xn ] be a pxn data matrix, where p designates the dimen-
sion and n represents the sample size. We want to calculate the following.
n X
n
X 1
xj xk )0 Sn1 (xj xk )
d= exp( (2)
j=1 k=1
2
where Sn1 represents the unbiased sample covariance matrix and is expressed
by
n
X
Sn1 =n 1
(xj xn )0 (xj xn )
j=1
in which xn = n1 nj=1 xj is the sample mean vector. The data matrix X
P
can be generated as independent random samples from normal dirtribution,
e.g.
X=normrnd(0,1,p,n)
with n = 50 and p = 2.
1. Calculate d by using the technique of for loop.
2. Calculate d without using the technique of for loop.
3. Compare the time it takes for executing 10000 times of (a) and (b).
2
4. Compare the time it takes for executing 10000 times of (a) and (b) as
p increases from 2 to 3,4,5,10 and 20.
5. Compare the time it takes for executing 10000 times of (a) and (b) as
n varies from 10, 20, 50 to 100.
A demonstrated program for this problem is shown in 2. You need to add
some time-related instructions for each methd to collect the elapsed time.
2: Three methods for computing d in (2)
3
Problem 3:
Objective:The usage of the anonymous functions could be very elegant, neat
and dramatic. The programmers should be acquainted with the techniques
of applying anonymous functions in the right place. In most situations,
the usage of the anonymous functions is straightforward. However the ef-
fect of combining anonymous functions with regular function could be huge.
Suppose two independent variables X and Y follow Beta distribution with
their PDF being fX (x) = (x|a1 , b1 ), fY (x) = (x|a2 , b2 ) respectively. Let a
new variable Z be defined by
Z = XY
The variable Z does not look like following the Beta distribution. Now we
want to approximate the distribution of Z by a beta distribution (a, b) in
the following way
Z 1
min (f (z) (z|a, b))2 dz (3)
a,b 0
where f (z) is the real distribution of Z and is written by
Z 1
1
f (z) = fY (y)fX (z/y) dy
z y
Let the parameters of the Beta functions fX (x) and fY (x) be set to
a1 = b 1 = a2 = b 2 = 2
Now the problem is to solve the minimization problem (3) for the optimal
parameters a and b.
To solve a multivariate optimization problem, MATLAB provides an instruc-
tion fminsearch. The usage of fminsearch requires a function definition
(the underlying function) as its first argument. In general, an anonymous
4
function is employed for this purpose. However, for complicated functions, a
function file is necesary. 3 demonstrates the programs containing a main
program (only one instruction) and a function (fun z). This program is pretty
concise and may not be easy to understand for inexperienced programmers.
In fact, a complicated program starts from a simple one and finishes after a
series of modifications. ??? shows another way of solving (3), although it
is not condense, but works. The inexperienced programmers are suggested
to draw f (z) to get an idea of solving this problem. 4 demonstrates the
program of plotting f (z) in two ways.
Meanwhile, it is a good idea to draw the figures of the real and the estimated
distribution functions together to help check the correctness of the program
or the goodness of the approach.
3: The usage of anonymous functions and regular function.
5
4: Two approaches of plotting f (z).
Problem 4:
:
f (x) = 1 f (x|1 , 12 ) + 2 f (x|2 , 22 )
1 + 2 = 1, f (x)
1. ezplot ,
2. , 1 = 40, 1 = 10, 2 =
60, 2 = 20 , 1 n , 1 = 0.1 :
6
0.1 : 0.9, n = 50 : 50 : 300, 1 n N = 10000
, 1 n ,
3. , ,
1 n ,
4. , f (x)
, (function)
f (x) T
5. , ( function
, ), ,
6. 1 n N , ,
, Latex ,
: input, menu
( ), num2str, str2num, strcat, clock (datevec)
seed,
RandStream.setDefaultStream(RandStream(mt19937ar,seed,sum(100*clock)));