Вы находитесь на странице: 1из 7

COM907M1: Computational Intelligence

Computational Intelligence COM907M1 Laboratory Session: week 1 Introduction to MATLAB and Mathematical Preliminaries

Integrated environment of MATLAB is shown in the diagram below. You can create your own tools to customize toolbox or harness with another toolbox such as Fuzzy logic, optimisation or neural network toolbox. You can write M-files and run at command prompt. Simulink is a tool in Matlab that allows users to simulate system behaviour using block diagrams. Introduction to Toolboxes and Simulink will be given in later labs.

Toolbox

Toolbox User-written M-files Simulink MATLAB

User-written

M-files

Simulink

Toolbox User-written M-files Simulink MATLAB

MATLAB

Figure 1: Matlab environment

Exercise 1: Representing vectors

A vector in Matlab is represented by any variable e.g. x. A row vector x can be initialised as

>> x = [6 5 4 8]

It will display the vector x as

>>x =

6

5

4

8

If elements are separated by semicolon, a column vector y is initialised, e.g

>> y = [1;2;3;4]

It

will display the vector y as

y

=

1

2

3

4

COM907M1: Computational Intelligence

Individual elements of a vector can be accessed by the vector name followed by an index running from 1 to n (max number of elements) to point to the elements e.g.

>> x(4)

ans =

8

It will return the 4 th element of vector x which is 8

The transpose of a column vector results in a row vector, and vice versa. For example

>>z=y’

will produce

1 2

3

4

Vectors of the same size can be added or subtracted, where addition is performed component wise. However, for multiplication, specific rules must be followed in order to obtain the correct resulting values. The operation of multiplying a vector X with a scalar k is performed component wise. For example

>>X=[1 3 –5];

>>k=5;

>>P=k*X

produces the output

5 15 -25

The operator .* performs element by element operation. For example,

>>x.*z

results in

6 10 12 32

% x and z are both row vectors

Inner product or dot product of two vectors x and z (both are row vectors) is a scalar quantity. The inner product is given by

>>S=x*z’

yields

S= 60

COM907M1: Computational Intelligence

Various norms (measure of size) of a vector can be obtained. For example, the Euclidean norm is the square root of the inner product of the vector and itself. e.g. >> N=norm(z) produces the output ans =

5.4772

The angle between two vectors X and Y is defined by

X Y * cosθ = , where X * Y
X Y
*
cosθ =
, where
X
*
Y

X*Y is the inner product and

between the vectors is calculated as follows

X , Y
X
,
Y

are the norms of the vectors. The angle

>>theta=acos(X’*Y/(norm(X)*norm(Y)))

The zero vector is a vector with all components equal to zero

>>Z=zeros(1,4)

produces a row vector of 4 elements

Z=

0 0

0

0

Sum vector is a vector with each component equal to one. To generate a sum vector of size 4, use

>>I=ones(1,4)

results in

I=

1

1

1

1

In Matlab the colon (:) can be used to generate a row vector. For example,

>>x=1:8

generates a row vector of integers from 1 to 8

x=

1 2

3

4

5

6

7

8

For increments other than unity, any value can be used

>> z=0:pi/3:pi

results in

z=

0 1.0472

2.0944

3.1416

For negative increments >> x=5:-1:1

results in

x=

5

4

3

2

1

Exercise 2: Representing matrices

COM907M1: Computational Intelligence

A matrix is represented with a variable name e.g. w, elements in each row are separated by blanks or commas. A semicolon must be used to indicate the end of a row. If a semicolon is not used, each row must be entered in a separate line. Matrix elements can be any Matlab expression. For example, a 3x3 matrix can be initialised as

>> w = [4 5 6; 0 4 7; 3 5 1]

will display the matrix w as

w

=

4

5

6

0

4

7

3

5

1

>>w=[2*2 5 3*2; 0 2*2 7; 3 5 1] results in w=

4

5

6

0

4

7

3

5

1

Individual elements of a matrix can be accessed by the matrix name followed by a two indices running from 1 to n (max row) and 1 to m (max column) to point to the elements e.g.

>> w(2,3)

ans =

7

It returns the element on row 2 column 3 which is 7

Entire row or column of a matrix can be addressed by means of the symbol (:). For example

>>r2w=w(2,:)

returns the 2 nd row of the matrix

0

4

7

>>c2w=w(:,2)

returns the 2 nd column of the matrix

5

4

5

COM907M1: Computational Intelligence

Matrix w can be transposed (rows will convert to columns) as

>> w’

It will display the transpose of matrix w as

ans =

4

5

6

0

4

7

3

5

1

Exercise 3: Handling Mathematical functions

Sine, Cosine, Tangent, Logarithm and Exponential functions can be used in Matlab e.g.

sin(X) is the sine of the elements of X. cos(X) is the cosine of the elements of X. tan(X) is the tangent of the elements of X.

log

Natural logarithm i.e Log base e. log(X) is the natural logarithm of the

elements of X. Complex results are produced if X is not positive. log2 is Log base 2, log10 is Log base 10.

Example: How many bits are required to represent the decimal number 128 in binary number

>> log2(128)

ans =

7

Exercise 4: Reading data files, reading a column from a file

Suppose you have a data file named ‘signal.dat’ which contains 3 columns of data. To read this data file use command as

>> load d:\signal.dat;

d:\ specifies the path and semicolon stops displaying values on the screen

Data can be assigned to any other variable e.g. >>s = signal;

>> load FILENAME X >> load FILENAME X Y Z

COM907M1: Computational Intelligence

%loads only X. %loads just the specified variables.

The wildcard '*' loads variables that match a pattern (MAT-file only).

Exercise 5: Plotting, labelling, and printing graphs

>>plot

% Linear plot.

>>plot(x,y)

%plots vector x versus vector y.

If X or Y is a matrix, then the vector is plotted versus the rows or columns of the matrix, whichever line up. If X is a scalar and Y is a vector, length(Y) disconnected points are plotted.

>>plot(y)

%plots the columns of Y versus their index.

Various line types, plot symbols and colors may be obtained with PLOT(X,Y,S) where S is a character string made from one element from any or all the following 3 columns:

b

blue

.

point

v

triangle (down)

-

solid

g

green

o

circle

^

triangle (up)

:

dotted

r

red

x

x-mark

<

triangle (left)

-.

dashdot

c

cyan

+

plus

>

triangle (right)

--

dashed

m

magenta

*

star

p

pentagram

y

yellow

s

square

h

hexagram

k

black

d

diamond

For example,

>>plot(X,Y,'c+:')

>>plot(X,Y,'bd')

%plots a cyan dotted line with a plus at each data point;

%plots blue diamond at each data point but does not draw any line.

Title and Labels for x-axis and y-axis created by >>xlabel(‘x-axis’) >>ylabel(‘y-axis’) >>title(‘Plot of Gaussian MF’)

To print a graph to a file, use commands >>print –dmeta c:\pathname\<filename>

This prints plot into a file, which can be later used in a MS word document by simply inserting the picture.

Exercise 6: Writing m-files

You can write lines of Matlab codes in a file and save it as m-file. The m-files can be run from command prompt. For example

Write the following codes in a new file

COM907M1: Computational Intelligence

%This m-file creates a Gaussian function – Gauss.m

x=1:0.1:10

m=4.5;

s=1;

xm=(x-m)/s;

A=exp(-0.5*xm.^2);

plot(A)

xlabel(‘x-label’)

ylabel(‘y-label’)

grid

%creates a grid lines across the plot

Save the file as Gauss.m and run the file from command prompt

>>Gauss

Exercise 7: Writing an m-file for a Bell-shaped MF

Write an m-file for the following Bell-shaped membership function and plot the membership function.

µ

A

(

x

)

=

1 2 a x m − 1 + σ
1
2 a
x m
1 +
σ

The parameters m and σ represent the centre and width of the bell shaped MF

a , usually positive, controls the slope of the MF at crossover

respectively. Parameter

a so that the MFs are sufficiently

overlapped. Plot all three MFs on a single Figure. Use different colours for each MF. Label all axes and use a title for the Figure. Submit Exercise 7 with proper Lab cover sheet (can be found on the web). The submission should contain cover sheet with name & student no, source M-file, and the Figure with sufficient explanation.

point. Use three different values of

m

,

σ , and