CSCI3006 Fuzzy Logic and Knowledge Based Systems Laboratory Sessions The Matlab Package and The Fuzzy Logic Toolbox
CSCI3006 Fuzzy Logic and Knowledge Based Systems Laboratory Sessions The Matlab Package and The Fuzzy Logic Toolbox
CSCI3006 Fuzzy Logic and Knowledge Based Systems Laboratory Sessions The Matlab Package and The Fuzzy Logic Toolbox
if there is some random process). Even if the output is given you should still try the input
command yourself (you may need this particular output later).
We can enter a matrix as follows and Matlab will respond.
a=[1 2; 1 2]
Now enter
b= [1 3;1 4]
We want to check that Matlab knows how to multiply and add matrices correctly enter
the following commands and check the answers.
a+b
a * b
Recall that we can't always multiply matrices if they are incompatible in size. Lets try to
multiply c*a
c * a
We can get a * c though
a*c
If we want we can use algebra with the matrices
a^2
The usual interpretation of powers is for matrix multiplication. There is also an array
multiplication which does usual arithmetic on the array values this has '.' in front of
operators.
Try
a.*a
and
a.^3
for example. Make sure you know what the difference between .* and * is it is
extremely important.
If I want a name for the answer I can set
y=a*c
So the transpose flips rows and columns. This is useful in situations where the parameters
to a Matlab function require a column vector when our data is a row vector etc.
If we want to add an additional column to an existing matrix we can do this
z=[y' [1;2;3]]
Matlab is a programming language so assignment is possible in the usual way:
a=a+b
Matlab help will give you information about any command just type help followed by
the command. Try
help help
If you want to keep track of what you are doing try the diary command (use the help to
find out about it);
If you want to keep all your working you can save the workspace as a .mat file again
see help on save for the details.
Entering data by hand can be tedious so Matlab allows creating uniformly spaced data
points
a=0:0.1:pi
I might want a hundred data points to use for something I don't want them to show and
fill the screen,
b=0:0.01:1;
The effect of the semicolon at the end of a command line input is to suppress the output
display.
c=a.^2
Matlab comes with built in graphics capability (useful for investigating stuff a picture
tells a thousand etc.
plot(a,c)
See the help on plot to see the various variants of plotting. For example, see the effect of
the following:
plot(a,c,'+:')
xlabel('The x axis')
title('The title of the graph')
Lab 2
Learning outcomes
You will learn how to access columns and rows of data, how to do multiple plots, and
how to use some of the Matlab control structures. You will be able to draw some
membership functions and you will also meet the idea of an m-file.
Use the load command to get the data from the file data.txt that you created last time into
a matrix called Data. Check what is in Data
Get the second column of Data
Data(:,2);
or we can use the figure command to get a new figure and plot on that
for i=1:3
figure
plot(data(:,1),data(:,i))
end
You have come across membership functions which fully define fuzzy sets.
m-files
An m-file allows you to repeat a series of commands that you have created without
having to type them all in again. This is particularly useful for conducting experimenst
where you want to repeat tests with only minor changes. All Matlab commands are mfiles.
Create a new m-file by choosing File|New from the menu and put the following
commands in:
a = 0:0.1:pi;
c = a.^2;
plot(a,c,'+:')
Save this as myplot.m. You will now be able to enter myplot at the command line and see
the effect (note that this file must be in the specified path).
Now let us draw some membership functions. Matlab has a number of membership
functions available:
dsigmf - the difference between 2 sigmoidal membership functions (a hat shape)
gauss2mf gaussian combination membership function
gaussmf gaussian curve membership function (this is a popular membership function)
gbellmf generalised bell-shaped function
psigmf the product of two sigmoid functions
smf s shaped membership function
trapmf trapezoidal shaped membership function (this is a popular membership function)
trimf triangular shaped membership function (popular)
zmf z shaped
All of these membership functions take various parameters. To find out more type help
followed by the function name. Type in the following code:
x = 0:0.1:10;
y = trimf(x,[3 6 8]);
plot(x,y);
xlabel(An example triangular membership function)
The three parameters (3,6,8) determine the points that at which the triangle joins the x
axis (3,8) and the peak (6). Experiment with some others especially gaussmf and
trapmf.
Another useful Matlab commands is evalmf which evaluates any membership function.
Use the help system to find out how this works