Académique Documents
Professionnel Documents
Culture Documents
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)
VI. Let x = [3 2 6 8]' and y = [4 1 3 5]' (NB. x and y should be column vectors).
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, ...
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
population)
a)
b)
c)
d)
e)
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)
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)
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)
b)
c)
d)
e)
XXII. Write a script that will use the random-number generator rand to determine
the following: