% anfis_ap1.m (ANFIS training file for XOR ) % ANFIS: Adaptive Neuro-Fuzzy training of Sugeno-type FIS. data=[... 0 0 0 0 1 1 1 0 1 1 1 0]; % data = 4*3 矩陣 (本例) numpts=length(data(:,1)); % 4 (本例) trndata=data(1:1:numpts,:); % trndata = data chkdata=data(2:2:numpts,:); % chkdata=[0 1 1; 1 1 0] figure(1); plot3(trndata(:,1),trndata(:,2),trndata(:,3),'o',chkdata(:,1),chkdata(:,2),chkdata(:,3),'x'); title('train data and check data before training'); %繪 學習前:訓練點(o),驗證點(x) nummfs=2 % 單一變數所使用的 MF 數 mftype='gbellmf'; % 隸屬函數的形式 fismat=genfis1(trndata,nummfs,mftype); % 產生模糊關係矩陣 [x,mf_x]=plotmf(fismat,'input',1); [y,mf_y]=plotmf(fismat,'input',2); figure(2); subplot(311) plot(x,mf_x,'o');hold on; plot(y,mf_y,'x');hold on; % 繪 學習前:I/P MFs title('The input membership function before training') %Begin to train the membership function by ANFIS numepochs=30; % 值越大,反覆學習次數越多 [fismat1,trnerr,ss,fismat2,chkerr]=anfis(trndata,fismat,numepochs,NaN,chkdata); % ANFIS 開始訓練 [x,mf_x]=plotmf(fismat1,'input',1); [y,mf_y]=plotmf(fismat1,'input',2); subplot(312) plot(x,mf_x);hold on; title('The input1 membership function after training') subplot(313) plot(y,mf_y);hold on; % 繪 學習後:I/P MFs title('The input2 membership function after training') %Evaluate the fis matrix out=evalfis([trndata(:,1) trndata(:,2)],fismat1); % 輸入data= [trndata(:,1) trndata(:,2)]=[0 0; 0 1; 1 0;1 1]; figure(3); trndata=data(1:1:numpts,:); chkdata=data(2:2:numpts,:); plot3(trndata(:,1),trndata(:,2),trndata(:,3),'o',chkdata(:,1),chkdata(:,2),chkdata(:,3),'x');hold on; plot3(trndata(:,1),trndata(:,2),out); %繪 學習後:訓練點(o),驗證點(x) title('line represents that train data and check data after training');