Fuzzy Labs1and2
Fuzzy Labs1and2
Laboratory Sessions
Matlab is a very sophisticated, widely used (in industry and universities) package that is
very flexible and has a number of tool boxes. The Fuzzy Logic Toolbox (FLT) is a set of
tools available in Matlab for carrying out fuzzy logic. You are able to get a copy of
Matlab (including the Fuzzy Logic Toolbox) from the student support centre in the James
Went.
The FLT has a GUI for developing fuzzy logic systems but so you understand what is
going on we will use the command line interface and programming language for our
purposes. You will gain exposure to the GUI later.
You will frequently be asked to enter unfamiliar Matlab code into the command window.
Many of these commands will be useful to you – you should try your best to understand
how they work and what they do before moving on.
Lab 1
Learning outcomes
After this lab you will be able to use Matlab to do simple matrix calculations and
you will be able to save data to a file and load matrices from file. You will know
some of the simpler capabilities of the Matlab package and will be familiar with the
Matlab help function. You will know how to draw simple plots using Matlab.
Double click the Matlab icon to get Matlab up and running. We will make much use of
the Matlab program and its Fuzzy Logic toolbox (and its general functions also).
You can read the help at a later date – for now we just want to use the command window
and get Matlab to do some calculations for us.
All the instructions below for some time refer to the command window. Just type at the
>> prompt.
In what follows bold italic is what you should type at the command prompt. There may or
may not be smaller non-bold italic following these input commands which shows the
Matlab output. Sometimes the output you get will be different to what appears here (e.g.
Matlab and the Fuzzy Logic Toolbox 2
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).
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
c=[1 2 3; 3 4 5]
Recall that we can't always multiply matrices if they are incompatible in size. Lets try to
multiply c*a
c * a
a*c
a^2
Matlab and the Fuzzy Logic Toolbox 3
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.
y=a*c
y+[1 2 3;1 2 5]
y'
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.
z=[y' [1;2;3]]
a=a+b
Matlab and the Fuzzy Logic Toolbox 4
The matrix a is not a big matrix – but it might be and we might want to save it to a file
We can load files as well – be careful with the quote marks. They are essential.
z=load('data.txt')
There are also commands which read and write delimited text if you want other than
space delimiters
dlmwrite('data2.txt',a,':')
Colon delimited files are used on some unix systems – it is more usual to use commas or
tabs on windows systems.
There is a similar command dlmread which you can find out about using the Matlab help
help dlmread
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
Matlab and the Fuzzy Logic Toolbox 5
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')
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
Data(:,2);
Data(2,:);
Set the vector a to be the first column of Data but don't bother to display again
a=Data(:,1);
for i=1:2
f=0.05*randn(size(a));
Data(:,i+1)=Data(:,i+1)+f;
end
This piece of code has altered the 2nd and 3rd columns of Data by adding random elements
to them. There are also if statements etc. which you can use – see the help for syntax.
Before you proceed save your new version of Data in data.txt – you will be using this
data later on.
We can plot all the columns against the x values on one graph like this
plot (Data(:,1),Data)
Matlab and the Fuzzy Logic Toolbox 7
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 m-
files.
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:
All of these membership functions take various parameters. To find out more type help
followed by the function name. Type in the following code:
Matlab and the Fuzzy Logic Toolbox 8
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