fuzzy c

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

Upload: anggara-t-nugraha

Post on 02-Oct-2015

222 views

Category:

Documents


2 download

DESCRIPTION

fdbvb

TRANSCRIPT

Fuzzy C-Means Clustering June 12, 2012 By: amir Category: Fuzzy Logic, Pemrograman Kode MATLAB dalam bentuk file *.txtDOWNLOAD dan ubah ekstension menjadi *.m========================================function [U,V,O]=fcm2(x,c)[n,m]=size(x);%% initialization%% matriks data: n baris x m kolomw=2;teta=1e-5;P0=0;Pt=0;OB=[];%step 1 : Initilize UU=rand(n,c);Q=sum(U)for i=1:nU(i,:)=U(i,:)/Q(i);endUpauseITER=1;Obj=1e5;while (Obj>teta)%step 2 : update VQ=sum(U.^w);V=(U.^w*x);for i=1:cV(i,:)=V(i,:)/Q(i);end;disp(NILAI V), Vpause%step 3 : update U from Vfor i=1:nS=0;for k=1:cS1=0;for j=1:mS1=S1+(x(i,j)-V(k,j))^2;endS1=S1^(-1/(w-1));U(i,k)=S1;S=S+S1;endfor k=1:cU(i,k)=U(i,k)/S;endenddisp(NILAI U);Upause%step 4 : menghitung FP0=Pt;Pt=0;for i=1:nfor k=1:cfor j=1:mPt=Pt+( x(i,j)-V(k,j))^2*U(i,k)^w;endendendObj=abs(Pt-P0)ITER=ITER+1pauseOB=[OB Obj];endO=OB;% end of function%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%Cara penggunaan :Siapkan data dalam variabel x : dalam format n x mn=cacah objekm=dimensi objekmisalnya dalam prompt MATLAB :>> x=[1 3; 0 4; 4 1; 3 0]x =1 30 44 13 0>>>> fcm2(x,2)Q =0.25230.81161.18521.2837U =0.3107 0.68930.7896 0.21040.1611 0.83890.6574 0.3426NILAI VV =1.2706 2.38462.7171 1.7204NILAI UU =0.9103 0.08970.7486 0.25140.1877 0.81230.2594 0.7406Obj =7.5831ITER =2NILAI VV =0.7854 3.19313.3549 0.7321NILAI UU =0.9923 0.00770.9454 0.05460.0312 0.96880.0420 0.9580Obj =5.1949ITER =3NILAI VV =0.5283 3.47133.4999 0.5113NILAI UU =0.9655 0.03450.9776 0.02240.0262 0.97380.0274 0.9726Obj =0.4393ITER =4NILAI VV =0.4960 3.50393.4981 0.5031NILAI UU =0.9609 0.03910.9803 0.01970.0262 0.97380.0263 0.9737Obj =0.0046ITER =5NILAI VV =0.4922 3.50783.4973 0.5028NILAI UU =0.9603 0.03970.9806 0.01940.0262 0.97380.0262 0.9738Obj =6.3781e-005ITER =6NILAI VV =0.4917 3.50833.4972 0.5028NILAI UU =0.9602 0.03980.9806 0.01940.0262 0.97380.0262 0.9738Obj =9.2658e-007ITER =7ans =0.9602 0.03980.9806 0.01940.0262 0.97380.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, Gd 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 negatiffunction y=setiga_kr(x,a,b); y=max(min(1,(b-x)/(b-a)),0);Fungsi untuk fuzzifikasi himpunan error/delta error zerofunction 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 positiffunction y=setiga_kn(x,a,b); y=max(min(1,((x-a))/(b-a)),0);Fungsi untuk pengendali fuzzyfunction o=fuzz_satelit(x1,x2)%fuzzifikasi masukan errorE_N=setiga_kr(x1,-1,0);E_Z=setiga_tg(x1,-1,0,1);E_P=setiga_kn(x1,0,1);%fuzzifikasi masukan perubahan errorCe_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 fuzzyf1=min(E_N,Ce_N);%-1f2=min(E_N,Ce_Z);%-1f3=min(E_Z,Ce_N);%-1f4=min(E_N,Ce_P);%0f5=min(E_Z,Ce_Z);%0f6=min(E_P,Ce_N);%0f7=min(E_Z,Ce_P);%1f8=min(E_P,Ce_Z);%1f9=min(E_P,Ce_P);%1f=[f1 f2 f3 f4 f5 f6 f7 f8 f9];%data titik tengah membership keluaran untuk tiap ruley=[-1 -1 -1 0 0 0 1 1 1]; %lebar membershipnya adalah 2%menghitung y_Fuzzy1num=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));endo=num/den;Fungsi untuk plant satelitfunction [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.5e(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);endt=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 =1Program 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 Gp, Gd dan Go yang diubah. Grafik bertanda 1 adalah untuk Gp=0.5, Gd=1 dan Go=1, grafik bertanda 2 adalah untuk Gp=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 keluaranGambar 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) denganm-filePosted 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 12 2 2 2 1 13 3 3 3 1 14 4 4 4 1 15 5 5 5 1 16 6 6 6 1 17 7 7 7 1 1];n=addrule(n,ruleList);

keterangan :newfis = membuat FIS Editor baru.addvar = menambahkan variabel input atau ouputaddmf = 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 ControllerThis 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.OverviewA 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 StructureThe 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(k-1), 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 ControllerDesigning 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. Design a conventional linear PID controller2. Design an equivalent linear fuzzy PID controller3. Adjust the fuzzy inference system to achieve nonlinear control surface4. Fine-tune the nonlinear fuzzy PID controllerStep 1: Design a Conventional PID ControllerThe 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 structureC = pidtune(Plant,C0) %#ok % design PID[Kp, Ki, Kd] = piddata(C); % obtain PID gainsC = 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 secondsDiscrete-time PID controller in parallel form.

Step 2a: Design an Equivalent Linear Fuzzy PID ControllerBy 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 = addvar(FIS,'input','E',[-10 10]);FIS = addmf(FIS,'input',1,'Negative','trimf',[-20 -10 0]);FIS = addmf(FIS,'input',1,'Zero','trimf',[-10 0 10]);FIS = addmf(FIS,'input',1,'Positive','trimf',[0 10 20]);Define input CE:FIS = addvar(FIS,'input','CE',[-10 10]);FIS = addmf(FIS,'input',2,'Negative','trimf',[-20 -10 0]);FIS = addmf(FIS,'input',2,'Zero','trimf',[-10 0 10]);FIS = addmf(FIS,'input',2,'Positive','trimf',[0 10 20]);Define output u:FIS = addvar(FIS,'output','u',[-20 20]);FIS = addmf(FIS,'output',1,'LargeNegative','trimf',[-20 -20 -20]);FIS = addmf(FIS,'output',1,'SmallNegative','trimf',[-10 -10 -10]);FIS = addmf(FIS,'output',1,'Zero','trimf',[0 0 0]);FIS = addmf(FIS,'output',1,'SmallPositive','trimf',[10 10 10]);FIS = addmf(FIS,'output',1,'LargePositive','trimf',[20 20 20]);Define the rules:1. If E is Negative and CE is Negative then u is -202. If E is Negative and CE is Zero then u is -103. If E is Negative and CE is Positive then u is 04. If E is Zero and CE is Negative then u is -105. If E is Zero and CE is Zero then u is 06. If E is Zero and CE is Positive then u is 107. If E is Positive and CE is Negative then u is 08. If E is Positive and CE is Zero then u is 109. If E is Positive and CE is Positive then u is 20ruleList = [1 1 1 1 1;... % Rule 1 1 2 2 1 1;... % Rule 2 1 3 3 1 1;... % Rule 3 2 1 2 1 1;... % Rule 4 2 2 3 1 1;... % Rule 5 2 3 4 1 1;... % Rule 6 3 1 3 1 1;... % Rule 7 3 2 4 1 1;... % Rule 8 3 3 5 1 1]; % Rule 9FIS = addrule(FIS,ruleList);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 * GCEAssume 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 TableThe 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 inputsE = -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); endendThe 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 SimulinkIn 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 closed-loop 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 SurfaceAfter 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 = addvar(FIS,'output','u',[-20 20]);FIS = addmf(FIS,'output',1,'Min','constant',-20);FIS = addmf(FIS,'output',1,'Zero','constant',0);FIS = addmf(FIS,'output',1,'Max','constant',20);Define the rules:1. If E is Negative and CE is Negative then u is -202. If E is Negative and CE is Positive then u is 03. If E is Positive and CE is Negative then u is 04. If E is Positive and CE is Positive then u is 20ruleList = [1 1 1 1 1;... % Rule 1 1 2 2 1 1;... % Rule 2 2 1 2 1 1;... % Rule 3 2 2 3 1 1]; % Rule 4FIS = addrule(FIS,ruleList);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 inputsE = -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); endendThe 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.ConclusionIn 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 Denmarkbdclose('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 ControllerThe 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 Simulationto simulate this system, we can download the functions first1. 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 belowclear;x1(1)=0;x2(1)=0;y(1)=0;dt=0.01;u(1)=1;r=0.5e(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);endt=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.