Vous êtes sur la page 1sur 6

MATLAB Exercise

MATLAB is a commercial Matrix Laboratory package which operates as a very


sophisticated calculator with graphical output, as well as an interactive programming
environment. It is ideal for carrying out the kinds of calculations that are common in
the field of theoretical optics. Therefore, before starting to work through the exercises
which comprise the succeeding weekly sessions, it is important that you gain at least
some small degree of familiarity with Matlab.
The following exercises are designed to get you started. They are written on the
assumption that you have good experience with using the Windows XP operating
system, or with MATLAB itself. If you already have some competence in this area
you can go through the first exercises quickly and get to the next lab CO_lab1.

This session should take one week to complete.

CO_MATLAB tutorial
MathWorks, Inc., Matlab in Education site is a good source of information and
tutorials: http://www.mathworks.com/education/
In this lab you might like to work with the browser and MATLAB command windows
arranged like this. (Note: This picture was taken on an operating system of Windows
XP with a Professional version of MATLAB 2007b.)

If you cannot finish working through the guide within the hours of the laboratory
session, you will need to spend extra time before next weekly session.

Note: Never use a loop when you can use a matrix instead. (Potential crash on
your PC)

The following are some short problems which you ought to be able to do after going
through the guide. They by no means cover all that is in the tutorial, but concentrate
on the kind of MATLAB skills that will be important in the labs in the following
weekly sessions.
At the end of the session ask your supervisor to evaluate your answers and check how
many problems you have solved.
I.

Let the variable A be a row matrix (2, 4, 0, -1, 3), and B be a column matrix
whose five elements are 2, 5, 8, 3, -5, in that order. Calculate the quantity A *
(B+1).
ur
II. Set up the vector v1 = (0,1, 2,...,50) and calculate the length of this vector, v1 ,
uur ur
as given by the formula: v1 = v1.v1
III. Write a script to calculate J =

10000
n =1

structure.

(n 2 + 1) (n 2 1)

, using a loop
(n + 100)

IV. Create a vector of the even whole numbers between 31 and 75.
V. Let x = [2 5 1 6].

a)
b)
c)
d)

Add 16 to each element


Add 3 to just the odd-index elements
Compute the square root of each element
Compute the square of each element

VI. Let x = [3 2 6 8]' and y = [4 1 3 5]' (NB. x and y should be column vectors).

a) Add the sum of the elements in x to y


b) Raise each element of x to the power specified by the corresponding
element in y.
c) Divide each element of y by the corresponding element in x
d) Multiply each element in x by the corresponding element in y, calling
the result "z".
e) Add up the elements in z and assign the result to a variable called "w".
f) Compute x'*y - w and interpret the result
VII.

Evaluate the following MATLAB expressions by hand and use MATLAB to


check the answers

a)
b)
c)
d)
e)
f)
g)
h)
VIII.

a)
b)
c)
d)

2/2*3
6-2/5+7^2-1
10 / 2 \ 5 - 3 + 2 * 4
3^2/4
3^2^2
2 + round(6 / 9 + 3 * 2) / 2 - 3
2 + floor(6 / 9 + 3 * 2) / 2 - 3
2 + ceil(6 / 9 + 3 * 2) / 2 - 3
Create a vector x with the elements...
2, 4, 6, 8, ...
10, 8, 6, 4, 2, 0, -2, -4
1, 1/2, 1/3, 1/4, 1/5, ...
0, 1/2, 2/3, 3/4, 4/5, ...

IX. Create a vector x with the elements,

xn = (-1)n+1/(2n-1)
Add up the elements of the version of this vector that has 100 elements.

X. Given a vector, t, of length n, write down the MATLAB expressions that will
correctly compute the following:

a) ln(2 + t + t2)

b)
c)
d)
e)
f)

et(1 + cos(3t))
cos2(t) + sin2(t)
tan-1(1) (Note: this is the inverse tangent function)
cot(t)
sec2(t) + cot(t) - 1

Test that your solution works for t = 1:0.2:2


XI. Plot the expression (determined in modeling the growth of the US

population)

P(t) = 197,273,000/(1 + e-0.0313(t - 1913.25))


where t is the date, in years AD, using t = 1790 to 2000. What population is predicted
in the year 2020?
XII.
When a simple harmonic wave travels in a one dimensional medium, its
shape is a sine wave. When a simple harmonic wave travels on a two dimensional
surface, radially outwards from a central point, the corresponding wave shape is
called a Bessel function, usually denoted by the symbol J0(x). Write a script to do
the following calculation. Construct x as a row vector consisting of these values:
0, 0.001, 0.002, 0.003,, 20.000. Then calculate the row vector for y = J0(x).
You can do this with the MatLab command
y = besselj(0,x);
Plot the variable y against x and read off from the graph, accurate to 7 significant
figures, the lowest value of x for which J0(x) = 0. Note: you will need to use the
zoom and grid facilities, and the command ginput for this exercise.
XIII.

a)
b)
c)
d)
e)

Given the array A = [ 2 4 1 ; 6 7 2 ; 3 5 9], provide the commands needed to


assign the first row of A to a vector called x1
assign the last 2 rows of A to an array called y
compute the sum over the columns of A
compute the sum over the rows of A
compute the standard error of the mean of each column of A (NB. the
standard error of the mean is defined as the standard deviation divided by the
square root of the number of elements used to compute the mean.)

XIV. Given the array A = [2 7 9 7 ; 3 1 5 6 ; 8 1 2 5], explain the results of the


following commands:

a)
b)
c)
d)
e)
f)
g)
h)
i)
j)

A'
A(:,[1 4])
A([2 3],[3 1])
reshape(A,2,6)
A(:)
flipud(A)
fliplr(A)
[A A(end,:)]
A(1:3,:)
[A ; A(1:2,:)]

k)
l)
m)
n)

sum(A)
sum(A')
sum(A,2)
[ [ A ; sum(A) ] [ sum(A,2) ; sum(A(:)) ] ]

XV. Given the array A from problem 4, above, provide the command that will

a)
b)
c)
d)
e)

assign the even-numbered columns of A to an array called B


assign the odd-numbered rows to an array called C
convert A into a 4-by-3 array
compute the reciprocal of each element of A
compute the square-root of each element of A
Give the following commands to create an array called F:

XVI.

>> randn('seed',123456789)
>> F = randn(5,10);
a) Compute the mean of each column and assign the results to the elements of a
vector called avg.
b) Compute the standard deviation of each column and assign the results to the
elements of a vector called s.
c) Compute the vector of t-scores that test the hypothesis that the mean of each
column is no different from zero.
d) If Pr(|t| > 2.132 ) = 0.1 with 4 degrees of freedom, are any of the mean values
in the vector avg statistically different from 0?
XVII.

a)
b)
c)
d)
e)
f)

Given x = [3 15 9 12 -1 0 -12 9 6 1], provide the command(s) that will


set the values of x that are positive to zero
set values that are multiples of 3 to 3 (rem will help here)
multiply the values of x that are even by 5
extract the values of x that are greater than 10 into a vector called y
set the values in x that are less than the mean to zero
set the values in x that are above the mean to their difference from the mean

XVIII. Create the vector x = randperm(35) and then evaluate the following function
using only logical indexing:

y(x)= 2
y(x)= x - 4
y(x)= 36 - x

if x < 6
if 6 <= x < 20
if 20 <= x <= 35

You can check your answer by plotting y vs. x with symbols. The curve
should a triangular shape, always above zero and with a maximum of 16. It
might also be useful to try setting x to 1:35. Using multiple steps (or a simple
mfile) is recommended for solving this problem.
XIX.

Given the vector x = [1 8 3 9 0 1], create a short set of commands that will

a) Add up the values of the elements (Check with sum.)


b) Computes the running sum (for element j, the running sum is the sum of the
elements from 1 to j, inclusive. Check with cumsum.)
c) computes the sine of the given x-values (should be a vector)
XX. Create an M-by-N array of random numbers (use rand). Move through the array,
element by element, and set any value that is less than 0.2 to 0 and any value that
is greater than (or equal to) 0.2 to 1.
XXI.

a)
b)
c)
d)
e)

Given x = [4 1 6] and y = [6 2 7], compute the following arrays


aij = xiyj
bij = xi/yj
ci = xiyi, then add up the elements of c.
dij = xi/(2 + xi + yj)
eij = reciprocal of the lesser of xi and yj

XXII. Write a script that will use the random-number generator rand to determine
the following:

a) The number of random numbers it takes to add up to 20 (or more).


b) The number of random numbers it takes before a number between 0.8 and
0.85 occurs.
c) The number of random numbers it takes before the mean of those numbers is
within 0.01 of 0.5 (the mean of this random-number generator).
It will be worthwhile to run your script several times because you are dealing
with random numbers. Can you predict any of the results that are described
above?
XXIII. Write a script that asks for a temperature (in degrees Fahrenheit) and
computes the equivalent temperature in degrees Celcius. The script should keep
running until no number is provided to convert. [NB. the function isempty will
be useful here.]

Vous aimerez peut-être aussi