CSCI3006 Fuzzy Logic and Knowledge Based Systems Laboratory Sessions The Matlab Package and The Fuzzy Logic Toolbox

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 8
At a glance
Powered by AI
The document discusses using Matlab and the Fuzzy Logic Toolbox for fuzzy logic and introduces some basic Matlab functions and operations on matrices. It also discusses drawing membership functions and saving/loading data.

Matlab is a widely used programming package for engineering and scientific computing. The Fuzzy Logic Toolbox provides tools for developing and analyzing fuzzy logic systems within Matlab. The document introduces some basic matrix operations and functions in Matlab.

Common membership functions discussed include triangular, trapezoidal, Gaussian and generalized bell-shaped functions. Functions like trimf and trapmf take parameters that define the shape of the membership function curve.

Matlab and the Fuzzy Logic Toolbox

CSCI3006 Fuzzy Logic and Knowledge Based


Systems
Laboratory Sessions
The Matlab package and the Fuzzy Logic Toolbox.
This handout covers the work for the first 9 weeks of the lab 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

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

Lets have a new matrix c


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
We can get a * c though
a*c
If we want we can use algebra with the matrices
a^2

Matlab and the Fuzzy Logic Toolbox

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

and now I can use y in a calculation


y+[1 2 3;1 2 5]

We will need to know about transpose matrices


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.
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 and the Fuzzy Logic Toolbox

Importing and exporting data:


The matrix a is not a big matrix but it might be and we might want to save it to a file
save data.txt a -ascii
Look at this file to see what you have got.
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

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')

Matlab and the Fuzzy Logic Toolbox

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);

and the second row


Data(2,:);
Set the vector a to be the first column of Data but don't bother to display again
a=Data(:,1);
Add a column to Data.
Matlab has standard programming features like for loops:
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.
save data.txt Data -ascii
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

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:

Matlab and the Fuzzy Logic Toolbox

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

You might also like