You are on page 1of 7

An Introduction to Matlab (EG7012)

Practical Session 1
First login into your CFS account!! Matlab v.7 is installed on all Open Access machines.
Just click on the Start button, navigate through All Programs to Matlab v.7.04 and
click on it.
1) Vectors in Matlab
This is a basic introduction to Matlab. It includes the creation of vectors together with
a few basic operations.
Almost all of Matlabs basic commands revolve around the use of vectors. To simplify
the creation of vectors, you can define a vector by specifying the first entry, an increment,
and the last entry.
For example, to create a vector whose entries are 0, 2, 4, 6, and 8, you can type in the
following line:
>> 0:2:8 <RET>
ans =
0

Matlab keeps track of the last result in a variable ans which is always created by default.
To keep track of the vectors you create, it is better to assign specific names. For example,
a row vector v can be created:
>> v = 0:2:8 <RET>
v =
0

Note: Matlab echoes the result to the screen. If instead a semicolon is included at the
of the line
>> y = 1:1:3; <RET>

The command is not confirmed. (Sometimes this is preferable - try typing 1:10000 <RET>
at the command line.)
Matlab will allow you to look at specific parts of the vector. If you want to only look at
the first three entries in a vector you can use the same notation you used to create the
vector:
>> v(1:3) <RET>
ans =
0

>> v(1:2:4) <RET>


ans =
0

2) Matrices in Matlab
Defining a matrix is similar to defining a vector. To define a matrix, you can treat it like
a column of row vectors (note that the spaces are required):
>> A = [ 1 2 3; 3 4 5; 6 7 8]
A =
1
3
6

2
4
7

3
5
8

You can also treat it like a row of column vectors:


>> B = [ [1; 2; 3] [2; 4; 7] [3; 5; 8] ]
B =
1
2
3

2
4
7

3
5
8

(Again, it is important to include the spaces ;)


To keep track of what variables you have defined, the whos command will let you know
all of the variables you have in your work space. It should appear something like this:
2

>> whos
Name
A
B
ans
v

Size
3 by 3
3 by 3
1 by 3
1 by 5

Elements
9
9
3
5

Bytes
72
72
24
40

Density
Full
Full
Full
Full

Complex
No
No
No
No

Grand total is 26 elements using 208 bytes


The notation used by Matlab for matrix manipulation is the standard linear algebra
notation.
>> x= [1; 2; 3]
x =
1
2
3
>> A*x
ans =
14
26
44
Matrix addition and matrix subtraction are also easily handled.
>> A+B
ans =
2
5
9

4
8
14

6
10
16

0
0
0

0
0
0

>> A-B
ans =
0
1
3

There is a (mathematically non-standard) operation .* which performs element by element multiplication. This is very different and not to be confused with the usual notion
of matrix multiplication.
>> A.*B
ans =
1
6
18

4
16
49

9
25
64

There is also a matrix division operator ./ which performs element by element division.
You can work with different parts of a matrix, just as you can with vectors.
>> A(1:2,2:3)
ans =
2
4

3
5

3) Plotting
Matlab has good (but relatively simple) plotting facilities. Suppose x and y are two
vectors in the workspace with the same number of components. Then
>> plot(x,y)
will plot y on the vertical axis against x on the horizontal.
For example, suppose x is a vector of a 100 equally spaced points between 0 and , and
y represents the sine of these numbers.
>> x=0:pi/100:pi;
>> y=sin(x);
>> plot(x,y)
produces the graph

0.9

0.8

0.7

0.6

0.5

0.4

0.3

0.2

0.1

0.5

1.5

2.5

3.5

0.5

1.5

2.5

3.5

whereas
>> plot(x,y,+)
produces the graph
1

0.9

0.8

0.7

0.6

0.5

0.4

0.3

0.2

0.1

More polished versions can also be created (try typing)


>>
>>
>>
>>

plot(x,y)
xlabel(x)
ylabel(sin(x))
title(A plot of x versus sin(x))

This produces the graph

A plot of x versus sin(x)


1

0.9

0.8

0.7

sin(x)

0.6

0.5

0.4

0.3

0.2

0.1

0.5

1.5

2.5

3.5

Many of the standard mathematical operators sin, cos, tan, exp, sqrt and log can work
on vector variables and produce vectors of the same size as the input (see the sin example
above).
4) Exercises
a) An object is to be hung from the end of a pole (of negligible mass) supported by a
cable as shown in Figure 1.

Figure 1: Setup
The pole is attached to the wall by a pivot and the cable is 8 metres long. A decision
needs to be made where on the pole to attach the cable. If the cable is attached at a
distance d from the end, and the mass of the object is object 20 kg, the tension in the
cable is given by
20 9.81 8 8

T =
d 64 d2
6

Plot a graph of cable tension T versus attachment point d. Use this to find the optimal
point of attachment to minimise the tension in the cable.
b) This exercise aims to plot the trajectory of a projectile launched from ground level on
flat terrain (the effect of the curvature of the earth, and any air fiction will be ignored).
If the projectile is launched with a velocity of 50m/s at an angle to the horizontal, the
horizontal and vertical positions (x, y) at time t are given by
x(t) = ut
1
y(t) = vt gt2
2
where u = 50 cos , v = 50 sin and the gravitational constant g = 9.81. The time at
which the projectile hits the ground is given by
timp =

2v
g

1. Suppose initially that = 60 . Plot the trajectory of the projectile. (Remember in


the calculations the angles must be in radians.)
2. Try different angles 0 90 and see how the range of the projectile varies.
c) Consider a two-dimensional vector
"

x=

1
0

as representing the population state of an island. The first entry (1) gives the fraction of
the population in the west half of the island, the second entry (0) gives the fraction in
the east half. The state of the population T units of time later is given by the rule
xnew = Ax
where

"

A=

0.8 0.1
0.2 0.9

This expresses the fact that an individual in the west half stays put with probability 0.8
and moves east with probability 0.2 (note 0.8 + 0.2 = 1), and the fact that in individual
in the east stays put with probability 0.9 and moves west with probability 0.1. Thus, successive population states can be predicted/computed by repeated matrix multiplication.
1. Calculate the population distribution after T units of time.
2. Calculate the population distribution after 10T units of time.
3. Plot the transition in the population over this period of time.
D.-W. Gu
October 3, 2006

You might also like