## Fuzzy logic in Scilab (sciFLT). Part 1 — Mamdani

Scilab is a free alternative to Matlab.

It has a fuzzy logic toolbox called sciFLT but there are not many tutorials for it in English.

Here, I’ll take some examples from the Matlab’s tutorial and implement them in Scilab.

The first example is the tipping problem:

Given two sets of numbers between 0 and 10 (where 10 is excellent) that respectively represent the quality of the service and the quality of the food at a restaurant, what should the tip be?

In the non-fuzzy approach, the model consists of long formulas, and the surface of the solution doesn’t look nice.

```clc
clf
function tip=f(food,service)
servRatio=0.8;
if (service<3) then
tip=((0.10/3)*service+0.05)*servRatio + ...
(1-servRatio)*(0.20/10*food+0.05);
elseif (service<7) then
tip=(0.15)*servRatio + ...
(1-servRatio)*(0.20/10*food+0.05);
else
tip=((0.10/3)*(service-7)+0.15)*servRatio + ...
(1-servRatio)*(0.20/10*food+0.05);
end
endfunction

food=linspace(0,10);
service=linspace(0,10);
tip=feval(food,service,f)';

surf(food,service,tip)
a=get("current_axes");
a.x_label.text="food";
a.y_label.text="service";
a.z_label.text="tip";
```

In this code we assume that we give a 15% tip in general, but will depart from this if the service is exceptionally good or bad.

Also, we give 80% of the tip for the quality of service 20% for the quality of food.

***

Now, let’s solve this problem “the fuzzy way”.

sciFLT works with Mamdani and Sugeno fuzzy logic systems (FLS).

The sciFLT is located in

ATOMS Module manager –> Modeling and Control Tools –> Fuzzy Logic Toolbox

Start the sciFLT graphical user interface with the command

```editfls
```

In the menu of the FLS editor, go to

File –>New FLS –>

and choose which fuzzy inference method (Mamdani or Sugeno) you want to work with.

We choose Mamdani.

Click the ‘Description’ link and fill out the form (click the picture to enlarge).

Then specify the input and output variables as well as the rules.

We have two input variables

food, service

and one output variable

tip

The food variable takes two fuzzy values: rancid and delicious.

The service variable takes three fuzzy values: poor, good, excellent.

The tip variable takes three fuzzy values: cheap, average, generous.

I model them with triangular membership functions.

Triangular functions in sciFLT have bugs in the ends of the variables’ universes. I work around these bugs by using trapezoidal functions (click the pictures to enlarge them and see the details).

Food:

Service:

Tip:

To view the variables,

View –> Plot Current Var

Our fuzzy rules are:

1. If service is poor or the food is rancid, then tip is cheap
2. If service is good, then tip is average
3. If service is excellent or food is delicious, then tip is generous

To save the FLS into the workspace,

File –> Export –> to workspace

It will be available till the end of your session in Scilab.

To save the FLS on a hard drive,

File –> Export –> to fls file (scilab)

I saved it into the workspace under the name `tipfls`

Typing into the console

`plotsurf(tipfls);`

gives a good-looking, smooth, non-linear surface of the solution.

```tipfls=loadfls("tipfls.fls");
```

where `tipfls` is a name of the variable where I save my FLS from the file tipfls.fls

Published in: on 26/09/2013 at 19:17  Comments (15)

1. Thank for your post. with this help i understood fuzzy tool box .
can you help me how to evaluate the same graphically as we do in Matlab

and how to use fls file in xcos

• I might write about it when I have time. Read my blog some time later.

2. thanks for your help in this toolbox my friend, but i have a question… I have some problem with Matlab because when my fuzzy project is done i can export this to use in other software. Like i do with normal Matlab proyect (.m) using deploy toolbox when i export to C# or .dll proyect. I can export the fuzzy project to other software with Scilab

3. –>plotsurf(forcefls);
!–error 999
Invalid size of input.at line 152 of function plotsurf called by :
plotsurf(fls,[1 2],[1],[{-10 10} {-10 10}],[100 100],1);
while executing a callback
at line 95 of function plotsurf called by :
plotsurf(forcefls);

I got this error. Can you help me? Thanks 🙂

• It looks like input/output linguistic variables are not specified correctly. Double check them.

Scilab has bugs when you define membership functions in the ends of their ranges. See carefully how I did it in this example: I used trapezoidal functions in the ends of the ranges (and even went beyond the ranges a little) even though I wanted to define triangular membership functions.

Hope this helps.

4. Solved. I tried to re-install the scilab with full installation setup, change the center membership functions to triangular (trimf) and the edge membership functions to trapezoidal (trapmf) just as you did in the example. Can I ask one more question? What if the scilab is installed with FLT 0.4.5, how can I change it back to FLT 0.4.4? Because the 0.4.5 can’t be run and still got some errors. Thanks alot

5. Warning !!!
Scilab has found a critical error (EXCEPTION_ACCESS_VIOLATION)
with “sciFLT_gateway_c” function.
Save your data and restart Scilab.

6. Is there a way to get the Defuzzified surface function as a z=ax^m+by^n+c format?

I know there will be different function for different range of x and y.

7. good, i could start my intro to fuzzy
thanks

8. total area is cero at line 152 of function plotsurf called…
…..
while executing a call back……….
At line …… 94 of function plot surface called by
plotsurf(checkfls);

9. Just after clicking on ATOMS Module manager icon, the following error message is displayed:

addinter: The shared file was not loaded: A startup routine of the
dynamic link library ( DLL ) failed”.

And SciLab shuts down.

Any idea? Didi it happen with anyone here?

10. In matlab I tryed build a ANFIS structure with 10 input and have OUT OF MEMORY ERROR .How many rules I can create in Scilab?

I am having 02 issues in installation and using scilab.

1. PC1 : Ubuntu 14.04
a) installed scilab (apt-get install scilab -> installed Scilab 5.5.0)
b) –>atomsInstall(“sciFLT”)
– file : ‘/usr/share/scilab//contrib/sciFLT/sciFLT_0.4.7-1.bin.x86_64.linux.tar.gz’
– MD5SUM expected : ‘acbf9b836dc5187a3614765dd1ba1704′
– MD5SUM watched : ’81c43eb809602d14557806c89002eac8’
!–error 10000

at line 313 of function atomsInstall called by :
atomsInstall(“sciFLT”)

Issue : sciFLT not able to install

2. PC2 : Ubuntu 14.04
a) installed scilab (apt-get install scilab -> installed Scilab 5.5.0)
b) installed sciFLT ( –>atomsInstall(“sciFLT”)

Issue : File -> NewFLs -> Takagi-Sugeno (or File -> NewFLs -> Mamdani), shows nothing in Editor fls (like image 3 shown above in your blog).