You are on page 1of 31

Fuzzy C-Means Clustering

June 12, 2012 By: amir Category: Fuzzy Logic, Pemrograman


Kode MATLAB dalam bentuk file *.txt
DOWNLOAD dan ubah ekstension menjadi *.m
========================================
function [U,V,O]=fcm2(x,c)
[n,m]=size(x);
%% initialization
%% matriks data: n baris x m kolom
w=2;
teta=1e-5;
P0=0;
Pt=0;
OB=[];
%step 1 : Initilize U
U=rand(n,c);
Q=sum(U)
for i=1:n
U(i,:)=U(i,:)/Q(i);
end
U
pause
ITER=1;
Obj=1e5;
while (Obj>teta)
%step 2 : update V
Q=sum(U.^w);
V=(U.^w*x);
for i=1:c
V(i,:)=V(i,:)/Q(i);
end;
disp(NILAI V), V
pause
%step 3 : update U from V
for i=1:n
S=0;
for k=1:c
S1=0;
for j=1:m
S1=S1+(x(i,j)-V(k,j))^2;
end
S1=S1^(-1/(w-1));
U(i,k)=S1;
S=S+S1;
end

for k=1:c
U(i,k)=U(i,k)/S;
end
end
disp(NILAI U);U
pause
%step 4 : menghitung F
P0=Pt;
Pt=0;
for i=1:n
for k=1:c
for j=1:m
Pt=Pt+( x(i,j)-V(k,j))^2*U(i,k)^w;
end
end
end
Obj=abs(Pt-P0)
ITER=ITER+1
pause
OB=[OB Obj];
end
O=OB;
% end of function
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%
Cara penggunaan :
Siapkan data dalam variabel x : dalam format n x m
n=cacah objek
m=dimensi objek
misalnya dalam prompt MATLAB :
>> x=[1 3; 0 4; 4 1; 3 0]
x=
13
04
41
30
>>>> fcm2(x,2)
Q=
0.2523
0.8116
1.1852
1.2837

U=
0.3107 0.6893
0.7896 0.2104
0.1611 0.8389
0.6574 0.3426
NILAI V
V=
1.2706 2.3846
2.7171 1.7204
NILAI U
U=
0.9103 0.0897
0.7486 0.2514
0.1877 0.8123
0.2594 0.7406
Obj =
7.5831
ITER =
2
NILAI V
V=
0.7854 3.1931
3.3549 0.7321
NILAI U
U=
0.9923 0.0077
0.9454 0.0546
0.0312 0.9688
0.0420 0.9580
Obj =
5.1949

ITER =
3
NILAI V
V=
0.5283 3.4713
3.4999 0.5113
NILAI U
U=
0.9655 0.0345
0.9776 0.0224
0.0262 0.9738
0.0274 0.9726
Obj =
0.4393
ITER =
4
NILAI V
V=
0.4960 3.5039
3.4981 0.5031
NILAI U
U=
0.9609 0.0391
0.9803 0.0197
0.0262 0.9738
0.0263 0.9737
Obj =
0.0046
ITER =

5
NILAI V
V=
0.4922 3.5078
3.4973 0.5028
NILAI U
U=
0.9603 0.0397
0.9806 0.0194
0.0262 0.9738
0.0262 0.9738
Obj =
6.3781e-005
ITER =
6
NILAI V
V=
0.4917 3.5083
3.4972 0.5028
NILAI U
U=
0.9602 0.0398
0.9806 0.0194
0.0262 0.9738
0.0262 0.9738
Obj =
9.2658e-007
ITER =
7

ans =
0.9602 0.0398
0.9806 0.0194
0.0262 0.9738
0.0262 0.9738
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%

Simulation of Fuzzy Control System Using Matlab


M-File

Diagram blok sistem kendali fuzzy yang akan disimulasikan adalah memiliki struktur
pengendali fuzzy PD-like seperti terlihat dalam Gambar 1, yaitu sistem fuzzy yang memiliki dua
masukan proporsional dan turunan, dengan Gp adalah penguatan proporsional, G d adalah
penguatan turunan dan Go adalah penguatan keluaran.

Gambar 1. Diagram blok pengendali fuzzy PD-like.

Simulasi dimulai dengan menetapkan nilai kondisi awal dari plant, error dan pengendali.
Kemudian langkah program memasuki proses looping yang berlangsung selama waktu yang kita
inginkan (dengan mempertimbangkan waktu pencuplikan). Setiap memasuki iterasi ke-k , error(k)
dihitung menggunakan Persamaan 1. Kemudian nilai perubahan error (turunan error) dihitung
dengan Persamaan 2. Setelah nilai error dan perubahan error diperoleh, selanjutnya nilai tersebut
dimasukan ke sistem logika fuzzy sehingga diperoleh keluaran yang akan digunakan sebagai
masukan plant. Dengan masukan plant ini, maka keluaran plant dapat dihitung. Selanjutnya
menuju iterasi berikutnya. Proses ini dapat dilihat pada Gambar 2.

(1)

(2)

Gambar 2. Diagram alir simulasi sistem kendali fuzzy.

Sebelum mensimulasikan sistem kendali fuzzy menggunakan M-file Matlab secara


keseluruhan, terlebih dahulu dituliskan fungsi-fungsi yang mendukung, supaya program utama
tidak terlalu rumit. Fungsi-fungsi tersebut adalah fungsi untuk fuzzifikasi, fungsi pengendali fuzzy
dan fungsi untuk plant. Berikut ini adalah listing program untuk mendeklarasikan fungsi-fungsi
tersebut.

Fungsi untuk fuzzifikasi himpunan error/delta error negatif


function y=setiga_kr(x,a,b);
y=max(min(1,(b-x)/(b-a)),0);

Fungsi untuk fuzzifikasi himpunan error/delta error zero


function y=setiga_tg(x,a,b,c);
y=max(min((x-a)/(b-a),(c-x)/(c-b)),0);

Fungsi untuk fuzzifikasi himpunan error/delta error positif


function y=setiga_kn(x,a,b);
y=max(min(1,((x-a))/(b-a)),0);
Fungsi untuk pengendali fuzzy
function o=fuzz_satelit(x1,x2)

%fuzzifikasi masukan error


E_N=setiga_kr(x1,-1,0);
E_Z=setiga_tg(x1,-1,0,1);
E_P=setiga_kn(x1,0,1);

%fuzzifikasi masukan perubahan error


Ce_N=setiga_kr(x2,-1,0);
Ce_Z=setiga_tg(x2,-1,0,1);
Ce_P=setiga_kn(x2,0,1);

%menghitung fired weight tiap kaidah fuzzy


f1=min(E_N,Ce_N);%-1
f2=min(E_N,Ce_Z);%-1
f3=min(E_Z,Ce_N);%-1
f4=min(E_N,Ce_P);%0
f5=min(E_Z,Ce_Z);%0
f6=min(E_P,Ce_N);%0
f7=min(E_Z,Ce_P);%1
f8=min(E_P,Ce_Z);%1
f9=min(E_P,Ce_P);%1
f=[f1 f2 f3 f4 f5 f6 f7 f8 f9];

%data titik tengah membership keluaran untuk tiap rule


y=[-1 -1 -1 0 0 0 1 1 1]; %lebar membershipnya adalah 2
%menghitung y_Fuzzy1
num=0;
den=0;
for k=1:9

num=num+((2*(f(k)-(f(k)^2)/2))*y(k));
den=den+((2*(f(k)-(f(k)^2)/2))*y(k));
end
o=num/den;

Fungsi untuk plant satelit


function [x1,x2]=f_satelit(dt,u_0,x1_0,x2_0);
a1=u_0;
b1=x2_0;

a2=u_0;
b2=(x2_0+dt*(b1/2));

a3=u_0;
b3=(x2_0+dt*(b2/2));

a4=u_0;
b4=(x2_0+dt*(b3));

x1=x1_0+(dt/6)*(b1+2*b2+2*b3+b4);
x2=x2_0+(dt/6)*(a1+2*a2+2*a3+a4);

Fungsi-fungsi di atas adalah digunakan dalam program utama dari simulasi sistem kendali sudut
satelit. Setiap fungsi disimpan dengan nama file seperti nama fungsinya, sehingga ketika dipanggil
dalam program utama maka fungsi yang bersangkutan akan langsung dijalankan komputer. Listing
program utama simulasinya adalah sebagai berikut:

clear;
x1(1)=0;
x2(1)=0;
y(1)=0;
dt=0.01;
u(1)=1;
r=0.5
e(1)=-r;
gp=1;

gd=1;
go=1;
for n=1:1000
k=n+1;
e(k)=r-y(k-1);
de(k)=100*(e(k)-e(k-1));
u(k)=go*fuzz_satelit(gp*e(k),gd*de(k));
%u(k)=e(k);
[x1(k),x2(k)]=f_satelit(dt,u(k-1),x1(k-1),x2(k-1));
y(k)=x1(k);
end
t=linspace(0,10,1001);
figure;
plot(t,y);
xlabel('detik');
ylabel('rad');

Gambar 3. Tanggapan sudut satelit terhadap acuan 0,5 rad dengan Gp, Gd dan Go =1

Program utama di atas adalah mensimulasikan sistem kendali sudut satelit dengan waktu
pencuplikan sebesar 10 mdet, selama 10 detik. Dengan Gp, Gd dan Go sebesar 1, serta acuan
0,5 rad. Jika program utama tersebut dijalankan (dieksekusi) maka akan didapatkan grafik
tanggapan (dengan garis tebal) seperti terlihat dalam Gambar 4.7, sedangkan grafik tanggapan
dengan garis putus-putus adalah grafik tanggapan sistem dengan menggunakan defuzzifikasi
yang kedua(bobot tiap kaidah dikalikan dengan titik tengah fungsi keanggotaan keluaran).
Gambar 4.8 adalah grafik tanggapan sistem kendali dengan nilai G p, Gd dan Go yang diubah.
Grafik bertanda 1 adalah untuk G p=0.5, Gd=1 dan Go=1, grafik bertanda 2 adalah untuk G p=1,
Gd=0.5 dan Go=1, sedangkan grafik bertanda 3 adalah untuk Gp=1, Gd=1 dan Go=0.5.

Gambar 4. Tanggapan posisi satelit dengan nilai penguatan yang berbeda.

Dari penjelasan di atas, nilai penguatan pengendali yang berbeda, akan memberikan hasil
tanggapan sistem yang berbeda pula. Terdapat dua besaran pengutan yang dapat ditala, yaitu
penguatan pada sisi masukan dan sisi keluaran. Penguatan pada sisi masukan adalah Gp dan Gd

sedangkan penguatan pada sisi keluaran adalah Go. Jika dilihat dari bentuk fungsi keanggotaan,
perubahan nilai penguatan pengendali memiliki persamaan dengan perubahan lebar dasar dan
skala titik tengah segitiga himpunan keanggotaan masukan maupun keluarannya. Jika penguatan
masukan diperbesar, maka akan setara dengan pengecilan lebar dasar dan skala titik tengah
segitiganya dan demikian sebaliknya akan memperbesar lebar dasar dan skala titik tengah
segitiga himpunan masukannya. Sedangkan untuk penguatan keluaran, jika diperbesar maka
akan setara dengan memperbesar skala titik tengah dan lebar himpunan fuzzy keluarannya.
Proses ini dapat dilihat pada Gambar 4.9 dan 4.10.

(a)

(b)

(c)
Gambar 5. Perubahan penguatan masukan setara dengan perubahan fungsi keanggotaan fuzzy.

Gambar 5(a) adalah bentuk fungsi keanggotaan masukan dengan penguatan sebesar 1, Gambar
5(b) adalah untuk penguatan sebesar 2, sedangakan Gambar 5(c) adalah untuk penguatan 0,5.

(a)

(b)

(c)

Gambar 6. Perubahan nilai penguatan keluaran setara dengan perubahan bentuk fungsi
keanggotaan fuzzy keluaran

Gambar 6(a) adalah bentuk fungsi keanggotaan dengan penguatan sebesar 1, Gambar 6(b)
adalah untuk penguatan sebesar 0.5, sedangkan Gambar 6(c) adalah untuk penguatan sebesar 2.

Membuat FIS (Fuzzy Interference System) dengan m-file


Posted on February 15, 2011

Sistem fuzzy atau logika fuzzy adalah salah satu bahasan soft computing yang memiliki
karakteristik dan keunggulan dalam menangani permasalahan yang bersifat ketidakpastian
dan kebenaran parsial. Logika fuzzy merupakan pengembangan dari logika boolean yang
hanya memiliki nilai true (1) atau false (0). Padahal di dunia nyata terdapat banyak masalah
yang tidak bisa dilihat sebagai hitam dan putih. Terdapat hal-hal bernilai abu-abu yang jika
diperhatikan akan membantu kita untuk membuat keputusan yang, secara intuitif, lebih adil.
Berikut ini merupakan code matlab untuk membuat FIS dengan m-file,kita dapat juga
membuat dengan mengetikkan kata fuzzy pada command prompt di matlab dan mulai
membangun FIS editor. Disini saya akan memberikan contoh code dalam membuat FIS tipe
Sugeno dengan m-file.
Listing program :
n=newfis('GelombangLautJawa','sugeno','prod','probor','prod','sum','wtaver'
);
n=addvar(n,'input','Kec.Angin',[0 30]);
n=addmf(n,'input',1,'CA','gaussmf',[5 2.763]);
n=addmf(n,'input',1,'LA','gaussmf',[5 5.923]);
n=addmf(n,'input',1,'LB','gaussmf',[5 8.986]);
n=addmf(n,'input',1,'GB','gaussmf',[5 11.81]);
n=addmf(n,'input',1,'MB','gaussmf',[5 14.35]);
n=addmf(n,'input',1,'FB','gaussmf',[5 16.82]);
n=addmf(n,'input',1,'SB','gaussmf',[5 19.94]);
n=addvar(n,'input','H(t)',[0 4]);
n=addmf(n,'input',2,'Glassy','gaussmf',[0.5533 0.205]);
n=addmf(n,'input',2,'Rippled','gaussmf',[0.5533 0.5257]);
n=addmf(n,'input',2,'Wavelets','gaussmf',[0.5533 0.8686]);
n=addmf(n,'input',2,'Slight','gaussmf',[0.5533 1.1509]);
n=addmf(n,'input',2,'Moderate','gaussmf',[0.553 1.4299]);
n=addmf(n,'input',2,'Rough','gaussmf',[0.5533 1.7445]);
n=addmf(n,'input',2,'VeryRough','gaussmf',[0.5533 2.1834]);

n=addvar(n,'input','H(t-1)',[0 4]);
n=addmf(n,'input',3,'Glassy','gaussmf',[0.5533 0.205]);
n=addmf(n,'input',3,'Rippled','gaussmf',[0.5533 0.5257]);
n=addmf(n,'input',3,'Wavelets','gaussmf',[0.5533 0.8685]);
n=addmf(n,'input',3,'Slight','gaussmf',[0.5533 1.1508]);
n=addmf(n,'input',3,'Moderate','gaussmf',[0.5533 1.43]);
n=addmf(n,'input',3,'Rough','gaussmf',[0.5533 1.7446]);
n=addmf(n,'input',3,'VeryRough','gaussmf',[0.5533 2.1833]);
n=addvar(n,'output','H(t+1)',[0 1]);
n=addmf(n,'output',1,'Glassy','constant',[0]);
n=addmf(n,'output',1,'Rippled','constant',[0.4]);
n=addmf(n,'output',1,'Wavelets','constant',[0.9]);
n=addmf(n,'output',1,'Slight','constant',[1]);
n=addmf(n,'output',1,'Moderate','constant',[1.2]);
n=addmf(n,'output',1,'Rough','constant',[1.5]);
n=addmf(n,'output',1,'VeryRough','constant',[3.8]);
ruleList=[ ...
1 1 1 1 1 1
2 2 2 2 1 1
3 3 3 3 1 1
4 4 4 4 1 1
5 5 5 5 1 1
6 6 6 6 1 1
7 7 7 7 1 1
];
n=addrule(n,ruleList);
keterangan :
newfis = membuat FIS Editor baru.
addvar = menambahkan variabel input atau ouput
addmf = menambahkan membership function pada variabel input atau output.
addrule = menambahkan rule base pada fuzzy.

Code diatas dibuat untuk merancang fuzzy prediksi ketinggian gelombang dengan 3 input dan
1 output yaitu masukan berupa kecepatan angin U(t) dengan fungsi keanggotaan antara lain
CA,LA,LB,GB,MB,FB,SB. Ketinggian gelombang aktual H(t) berupa
Glassy,Rippled,Wavelets,Slight,Moderate,Rough,VeryRought. Ketinggian gelombang
sebelumnya H(t-1) dengan fungsi keanggotaan yang sama dengan H(t). Untuk keluaran
prediksi Ketinggian gelombang 1 jam kedepan H(t+1). Pada perancangan FIS Sugeno ini kita
menggunakan pendekatan kurva gaussian dengan input berupa center dan standart deviasi.
Pada salah satu rule base if then jika kecepatan angin is LA dan H(t) is Rippled dan H(t-1) is
Rippled then H(t+1) is Rippled sehingga rule list nya 2 2 2 2 1 1.

Pada matlab ketikkan code plotfis(n)


sehingga hasilnya menjadi seperti ini

Using Lookup Table in Simulink to Implement Fuzzy PID Controller

This example shows how to use a 2-D Lookup Table block to implement a fuzzy inference
system for nonlinear fuzzy PID control.
The example uses Control System Toolbox and Simulink.
Overview
A fuzzy inference system (FIS) maps given inputs to outputs using fuzzy logic. For example,
a typical mapping of a two-input one-output fuzzy controller can be depicted in a 3-D plot.
The plot is often referred to as the control surface plot such as the one shown below.

The typical FIS inputs are the signals of error (e(k)) and change of error (e(k)-e(k-1)). The
FIS output is the control action inferred from the fuzzy rules. Fuzzy Logic Toolbox
provides commands and GUI tools to design a FIS for a desired control surface. The designed
FIS can then be simulated using the Fuzzy Logic Controller block in Simulink.
Nonlinear control surfaces can often be approximated by lookup tables to simplify the
generated code and improve execution speed. For example, a Fuzzy Logic Controller block in
Simulink can be replaced by a set of Lookup Table blocks, one lookup table for each output
defined in the FIS. Fuzzy Logic Toolbox provides command such as evalfis to compute
data used in those lookup tables.
In this example, we design a nonlinear fuzzy PID controller for a plant in Simulink. The plant
is a single-input single-output system in discrete time and our design goal is simply to
achieve good reference tracking performance.
Ts = 0.1;
Plant = c2d(zpk([],[-1 -3 -5],1),Ts);

We also show how to implement the fuzzy inference system with a 2-D lookup table that
properly approximates the control surface and achieves the same control performance.
Fuzzy PID Controller Structure
The fuzzy controller in this example is in the feedback loop and computes PID-like actions
through fuzzy inference. The loop structure is displayed below in the Simulink diagram.

open_system('sllookuptable')

The fuzzy PID controller uses a parallel structure[1] as shown below. It is a combination of
fuzzy PI control and fuzzy PD control.
open_system('sllookuptable/Fuzzy PID')

We use the change of measurement -(y(k)-y(k-1)), instead of change of error e(k)-e(k1), as the second input signal to FIS to prevent the step change in reference signal from
directly triggering the derivative action. Two gain blocks, GCE and GCU in the feed forward
path from r to u, are used to ensure that the error signal e is used in proportional action when
the fuzzy PID controller is linear.
Workflow for Designing a Fuzzy PID Controller

Designing a fuzzy PID controller involves configuring the fuzzy inference system and setting
the four scaling factors: GE, GCE, GCU and GU. In this example we applied the following design
steps[2]:
1.
2.
3.
4.

Design a conventional linear PID controller


Design an equivalent linear fuzzy PID controller
Adjust the fuzzy inference system to achieve nonlinear control surface
Fine-tune the nonlinear fuzzy PID controller

Step 1: Design a Conventional PID Controller


The conventional PID controller is a discrete time PID controller with Backward Euler
numerical integration method used in both the integral and derivative actions. The controller
gains are Kp, Ki and Kd. The controller is implemented in Simulink as below:
open_system('sllookuptable/Conventional PID')

Similar to the fuzzy PID controller, the input signal to the derivative action is -y(k), instead
of e(k).
PID controller gains can be tuned either manually or using tuning formulas. In this example,
we use the pidtune command from Control System Toolbox to obtain an initial PID
design.
C0 = pid(1,1,1,'Ts',Ts,'IF','B','DF','B'); % define PID structure
C = pidtune(Plant,C0) %#ok<*NOPTS> % design PID
[Kp, Ki, Kd] = piddata(C); % obtain PID gains
C =
Ts*z
z-1
Kp + Ki * ------ + Kd * -----z-1
Ts*z
with Kp = 30, Ki = 28.6, Kd = 6.9, Ts = 0.1
Sample time: 0.1 seconds
Discrete-time PID controller in parallel form.

Step 2a: Design an Equivalent Linear Fuzzy PID Controller


By configuring the FIS and selecting four scaling factors, we obtain a linear fuzzy PID
control that reproduces the exact control performance as the conventional PID controller
does.

First, configure the fuzzy inference system so that it produces a linear control surface from
inputs E and CE to output u. The FIS settings summarized below are based on design choices
described in [2]:

Use Mamdani style fuzzy inference system.

Use algebraic product for AND connective.

The ranges of both inputs are normalized to [-10 10].

The input sets are triangular and cross neighbor sets at membership value of 0.5.

The output range is [-20 20].

Use singletons as output, determined by the sum of the peak positions of the input
sets.

Use the center of gravity method (COG) for defuzzification.

Construct fuzzy inference system:


FIS = newfis('FIS','mamdani','prod','probor','prod','sum');
Define input E:
FIS
FIS
FIS
FIS

=
=
=
=

Define
FIS
FIS
FIS
FIS

=
=
=
=

addvar(FIS,'input','E',[-10 10]);
addmf(FIS,'input',1,'Negative','trimf',[-20 -10 0]);
addmf(FIS,'input',1,'Zero','trimf',[-10 0 10]);
addmf(FIS,'input',1,'Positive','trimf',[0 10 20]);
input CE:

Define

addvar(FIS,'input','CE',[-10 10]);
addmf(FIS,'input',2,'Negative','trimf',[-20 -10 0]);
addmf(FIS,'input',2,'Zero','trimf',[-10 0 10]);
addmf(FIS,'input',2,'Positive','trimf',[0 10 20]);
output u:

FIS
FIS
FIS
FIS
FIS
FIS

addvar(FIS,'output','u',[-20 20]);
addmf(FIS,'output',1,'LargeNegative','trimf',[-20 -20 -20]);
addmf(FIS,'output',1,'SmallNegative','trimf',[-10 -10 -10]);
addmf(FIS,'output',1,'Zero','trimf',[0 0 0]);
addmf(FIS,'output',1,'SmallPositive','trimf',[10 10 10]);
addmf(FIS,'output',1,'LargePositive','trimf',[20 20 20]);

=
=
=
=
=
=

Define the rules:


1. If E is Negative and CE is Negative then u is -20
2. If E is Negative and CE is Zero then u is -10
3. If E is Negative and CE is Positive then u is 0
4. If E is Zero and CE is Negative then u is -10

5. If E is Zero and CE is Zero then u is 0


6. If E is Zero and CE is Positive then u is 10
7. If E is Positive and CE is Negative then u is 0
8. If E is Positive and CE is Zero then u is 10
9. If E is Positive and CE is Positive then u is 20
ruleList = [1 1 1 1 1;...
%
1 2 2 1 1;...
%
1 3 3 1 1;...
%
2 1 2 1 1;...
%
2 2 3 1 1;...
%
2 3 4 1 1;...
%
3 1 3 1 1;...
%
3 2 4 1 1;...
%
3 3 5 1 1];
%
FIS = addrule(FIS,ruleList);

Rule
Rule
Rule
Rule
Rule
Rule
Rule
Rule
Rule

1
2
3
4
5
6
7
8
9

In this example, we build the FIS using commands. You can also use the FIS Editor GUI tool
to design the system.
The linear control surface is plotted in 3-D below.
gensurf(FIS)

Next, we determine scaling factors GE, GCE, GCU and GU from the Kp, Ki, Kd gains used by the
conventional PID controller. By comparing the expressions of the traditional PID and the
linear fuzzy PID, the variables are related as:

Kp = GCU * GCE + GU * GE

Ki = GCU * GE

Kd = GU * GCE

Assume the maximum reference step is 1, whereby the maximum error e is 1. Since the input
range of E is [-10 10], we first fix GE at 10. GCE, GCU and GCU are then solved from the above
equations.
GE = 10;
GCE = GE*(Kp-sqrt(Kp^2-4*Ki*Kd))/2/Ki;
GCU = Ki/GE;
GU = Kd/GCE;

Step 2b: Implement Fuzzy Inference System Using a 2-D Lookup Table
The fuzzy controller block has two inputs (E and CE) and one output (u) that can be replaced
by a 2-D lookup table.
The 2-D lookup table for FIS is generated by looping through the input universe and
computing the output with the evalfis command.
Step = 10; % use 3 break points for both E and CE inputs
E = -10:Step:10;
CE = -10:Step:10;
N = length(E);
LookUpTableData = zeros(N);
for i=1:N
for j=1:N
% compute output u for each combination of break points
LookUpTableData(i,j) = evalfis([E(i) CE(j)],FIS);
end
end

The fuzzy PID controller using 2-D lookup table is shown below. Notice that we only replace
the Fuzzy Logic Controller block with a 2-D Lookup Table block in the Simulink diagram.
open_system('sllookuptable/Fuzzy PID using Lookup Table')

When the control surface is linear as we already designed above, fuzzy PID controller using
the 2-D lookup table should produce exactly the same result as the one using fuzzy logic
controller block.
Step 2c: Simulate Closed-Loop Response in Simulink
In the Simulink model, we use three different sub-systems, namely Conventional PID,
Fuzzy PID and Fuzzy PID using Lookup Table, to control the same plant. The closedloop responses to a step reference change are displayed in the scope and they are exactly the
same (three response curves overlap each other).
open_system('sllookuptable/Scope')
sim('sllookuptable')

Step 3: Design Fuzzy PID Controller With Nonlinear Control Surface

After verifying that the linear fuzzy PID controller is properly designed, adjust FIS settings
such as its style, membership functions and rule base to obtain a desired nonlinear control
surface.
In this example, we choose to design a steep control surface using Sugeno style of FIS. Each
input set has two terms (Positive and Negative) and the number of rules is reduced to four.
Construct fuzzy inference system:
FIS = newfis('FIS','sugeno');
Define input E:
FIS = addvar(FIS,'input','E',[-10 10]);
FIS = addmf(FIS,'input',1,'Negative','gaussmf',[7 -10]);
FIS = addmf(FIS,'input',1,'Positive','gaussmf',[7 10]);
Define input CE:
FIS = addvar(FIS,'input','CE',[-10 10]);
FIS = addmf(FIS,'input',2,'Negative','gaussmf',[7 -10]);
FIS = addmf(FIS,'input',2,'Positive','gaussmf',[7 10]);
Define output u:
FIS
FIS
FIS
FIS

=
=
=
=

addvar(FIS,'output','u',[-20 20]);
addmf(FIS,'output',1,'Min','constant',-20);
addmf(FIS,'output',1,'Zero','constant',0);
addmf(FIS,'output',1,'Max','constant',20);

Define the rules:


1. If E is Negative and CE is Negative then u is -20
2. If E is Negative and CE is Positive then u is 0
3. If E is Positive and CE is Negative then u is 0
4. If E is Positive and CE is Positive then u is 20
ruleList = [1 1 1 1 1;...
%
1 2 2 1 1;...
%
2 1 2 1 1;...
%
2 2 3 1 1];
%
FIS = addrule(FIS,ruleList);

Rule
Rule
Rule
Rule

1
2
3
4

The 3-D nonlinear control surface is plotted below. It has higher gain near the center of the E
and CE plane than the linear surface has, which helps reduce the error more quickly when the
error is small. When the error is large, controller becomes less aggressive so that control
action is limited to avoid possible saturation.
gensurf(FIS)

Before starting the simulation, update the lookup table with the new control surface data.
Since the surface is nonlinear, add more break points to obtain a sufficient approximation.
Step = 1; % use 21 break points for both E and CE inputs
E = -10:Step:10;
CE = -10:Step:10;
N = length(E);
LookUpTableData = zeros(N);
for i=1:N
for j=1:N
% compute output u for each combination of break points
LookUpTableData(i,j) = evalfis([E(i) CE(j)],FIS);
end
end

The closed-loop responses to a step reference change are displayed in the scope.
sim('sllookuptable')

Compared with the traditional linear PID controller (the response curve with large
overshoot), the nonlinear fuzzy PID controller reduces the over shoot by 50%, which is what
we expected. The two response curves from the nonlinear fuzzy controllers almost overlap
each other, which indicates that the 2-D lookup table approximates the fuzzy inference
system very well.
Conclusion
In this example, we design a nonlinear fuzzy PID controller and show that lookup table can
be used to approximate the fuzzy inference system. By replacing a Fuzzy Logic Controller
block with a Lookup Table block in Simulink, a fuzzy controller can be deployed with
simplified generated code and improved execution speed.
References
[1] Xu, J. X., Hang, C. C., Liu, C. (2000), Parallel structure and tuning of a fuzzy PID
controller, Automatica, Vol. 36, page 673-684
[2] Jantzen, J. (1999), Tuning of Fuzzy PID Controllers, Technical Report, Dept. of
Automation, Technical University of Denmark
bdclose('sllookuptable');

M-file Matlab Function for Fuzzy System


Diagram block of fuzzy control systems that will simulate a PD-like fuzzy control structure is shown
in Figure 1, the fuzzy system has two inputs proportional and derivative, with the Gp is the
proportional gain, Gd is the gain of the derivative and Go is a gain of the output.

Figure 1. Block Diagram for PD-like Fuzzy Controller


The simulation begins by setting the value of the initial conditions of the plant, and controller
errors. Then step into the program looping process that took place during the time that we want
(by considering the sampling time). Every time entering the iteration k, error (k) is calculated using
Equation 1. Then the changing value of error (derivative error) is calculated by equation 2. After
the error values and error change obtained, then the value is inserted into the fuzzy logic system
so that the output obtained that will be used as plant input . With this plant input, then the plant
output can be calculated. Next to the next iteration. This process can be seen in Figure 2:
(1)
(2)

Figure 2. Flowchart of Fuzzy Control Sistem Simulation

to simulate this system, we can download the functions first


1. Left membership (setiga_kr.rar)
2. Center membership (setiga_tg.rar)
3. Right membership (setiga_kn.rar)
4. Fuzzy controller (fuzz_satelit.rar)
5. Satelit plant (f_satelit.rar)
save them to work folder at matlab, and then write the script below
clear;
x1(1)=0;
x2(1)=0;
y(1)=0;
dt=0.01;
u(1)=1;
r=0.5
e(1)=-r;
gp=1;
gd=1;
go=1;
for n=1:1000
k=n+1;
e(k)=r-y(k-1);
de(k)=100*(e(k)-e(k-1));
u(k)=go*fuzz_satelit(gp*e(k),gd*de(k));
%u(k)=e(k);
[x1(k),x2(k)]=f_satelit(dt,u(k-1),x1(k-1),x2(k-1));
y(k)=x1(k);
end
t=linspace(0,10,1001);
figure;
plot(t,y);
xlabel('detik');
ylabel('rad');
after then, execute the script that resulting the figure below

we can change the value of Gp, Gd and Go for the best result.

You might also like