fuzdemo3.m

来自「FISMAT accommodates different arithmetic」· M 代码 · 共 270 行

M
270
字号
function fuzdemo3

%# call tp_dem_c t2ap

echo on
clc
% fuzdemo3.m 
%
% FSTB - Fuzzy Systems Toolbox for MATLAB
% Copyright (c) 1993-1996 by Olaf Wolkenhauer
% Control Systems Centre at UMIST
% Manchester M60 1QD, UK
%
% See the READ.ME file for more information.

pause % Strike any key to continue.
clc
% In this demo, we first develop optimisations of the methods for a
% controller with discrete sets, described in fuzdemo2 (summarized in the 
% script file st_demo.m)
% These optimisations are required for a faster simulation but they are also
% relevant for a hardware implementation.
% Optimisation means a specific choice for the inference operator 'infop' 
% and 'accop' as described elsewhere. We now fix the inference operator to 
% algebraic-product and 'accop' to 'sumop', resulting in "accumulation" of 
% all fuzzy conclusions to one set B*, by addition of the membership-values The advantages and following simplifications are docu-
% (described in various papers from Kosko/Pacini or the textbook B.Kosko: 
% 'Neural Networks and Fuzzy Systems', Prentice Hall, 1992).

% Under Kosko's approach, fuzzy-systems are associating outputs to inputs, 
% so they are working like an associative memory. In contrast to neural
% networks fuzzy-systems do not sum up the passing information; instead
% fuzzy-systems are summing the (fuzzy-)outputs. This avoids crosstalk and
% increases the modularity. 

% Under the conditions laid down, we don't need to handle sets on the right
% side of the rules. We only need certain characteristics of the sets, which
% we can calculate off-line. 

% The operator for the aggregation may still be chosen freely. For the defuz-
% zification we can choose between the HEIGHT-METHOD, AREA-METHOD and the
% Centre of Gravity COG-METHOD.

pause % Press any key to continue...
clc

% Consider the following general fuzzy system:

% R1: IF X1=A11 ... AND Xi=A1i ... AND Xn=A1n THEN Y=B1
% ...
% Rj: IF X1=Aj1 ... AND Xi=Aji ... AND Xn=Ajn THEN Y=Bj
% ...
% Rm: IF X1=Am1 ... AND Xi=Ami ... AND Xn=Amn THEN Y=Bm

% X1 ... Xn are called linguistic variables.

% The characteristic values describing all k sets of the variable Y are
% the moment, the area and the centroid of the membership-functions:

% Moments   Ml = M(Bl) = sum(xi*mi(Bl))        with l=1..k
% Areas     Al = A(Bl) = sum(mi(Bl))           with l=1..k
% Centroids Cl = cog(Bl) = M(Bl)/A(Bl) = Ml/Al with l=1..k

% Using the algebraic-product for the inference to get Bl*, we can now
% obtain the values as follows:

% Al* = aj* Al
% Ml* = aj* Ml
% Cl* = Cl

% The membership-function Bl* is mi(Bl*) = aj* x mi(Bl)

pause % Strike any key to continue...
clc
% B* is now calculated by summing up all Bl*. The characteristic values of
% the resulting fuzzy-set B* are the sum of the characteristic values of 
% each Bl:

% For all rules recursively:

% A* = A* + Aj*  with Aj* = aj* x Ai
% M* = M* + Mj*  with Mj* = aj* x Mi 

% and C* can be calculated at the end by C* = M*/A* = b* for the COG-method.

% (j descibes the jth rule. i describes the ith set of the variable Y.)

% The Height-Method for a crisp output is a weighted average of the Bj's 
% with the aj's (giving the heigth).

% b* = sum(aj*Ci/sum(aj))   j = 1..no_of_rules

% The Area-Method is a weighted average of the Bj's with the areas Bi*.

% b* = sum(Bi*Cl/sum(Bi*))  j = 1..no_of_rules

% As we have seen before: M(Bi*)=sum(xi*mi(Bi*)) and A(Bi*)=sum(mi(Bi*)).
% Where M(Bi*) is simply the product x*B in MATLAB.

pause % Press any key to continue...
clc

% Using modsuset() are modtrset() to model a library of fuzzy-sets gives
% the compact representation.

% Using afterwards con2dis(set,Nel) gives automatically the moments and 
% areas. (see help con2dis())

% Now we can design a fuzzy-controller using the methods described before.
% Designing a Fuzzy-Logic-Controller (FLC) is done in the following steps:

% - Definition of the input and output variables.
% - Definition of all fuzzy-sets for the input and output variable.
% - Drawing up the rules.
% - Commiting the inference machine.
% - Optimisation of the performance.

% We derive these steps for a fuzzy PI-type controller with incremental
% output. In the simulation we use a simple first-order plant. 

% As the input, we use the difference between reference- and current tempe-
% rature (ERROR). The second input is the error rate of change (RATE). 
% The universe of discourse for both variables are E and R respectively. 
% The output variable is the power to heat or cool (POWER), with P as
% the output universe of discourse. The inference machine gives an incre-
% mental power, which is the same as an addition from the current output to
% the previous output value. In SIMULINK we could use a (limited) linear
% integrator if we didn't performe this operation inside the controller file.

pause % Strike any key to continue...
clc

% Definition of the fuzzy-sets:

% Input range space:  E : from -12 celsius to +12 celsius
%                     R : from -6 cels./sec to +6 cels./sec
% Output range space: P : -120W/sec to +120W/sec

% We choose seven "linguistic terms" for each "linguistic variable":

% NB = "negative big"
% NM = "negative medium"
% NS = "negative small"
% NZ = "near zero"
% PS = "positive small"
% PM = "positive medium"
% PB = "positive big"

% For the shape of the membership-function we initially employ triangles
% with an overlap between 10 and 30 percent, symmetric and unimodal. 

% Using the toolbox m-file modsuset() and con2dis():

% ERROR = modsuset('y',12,7,-4,0);   
% RATE  = modsuset('y',6,7,-2,0);    
% POWER = modsuset('n',120,7,-40,0); 

% Converting the (continous) trapezoidal representation to a discrete form:
% [E,ERROR]=con2dis(ERROR,200);
% [R,RATE]=con2dis(RATE,200);
% [P,POWER,MOMENTS,AREAS]=con2dis(POWER,200);


% To make the variables and sets used in the controller-file accessible:
% global E ERROR R RATE MOMENTS AREAS

% Press any key after plot...
echo off
clc
% Executing script file doing the jobs described before:
tp_dat
subplot(3,1,1),plot(E,ERROR),title('ERROR');
subplot(3,1,2),plot(R,RATE),title('ERROR RATE');
subplot(3,1,3),plot(P,POWER),title('POWER RATE'),pause
echo on

% The following section is identical to the controller file tp_dem_c.m .

% For two inputs we can organize the rule-bank in a matrix form, which
% make changes easier and the controller design more clear. Unused
% entries in the matrix are filled with zeros.

% function do = tp_dem_c(e,x,Ts)
%
% do = tp_dem_c(e,x,Ts)
%
% Temperature control demo with the fuzzy toolbox.
%
% This controller relates to the fuzdemo3 of the fuzzy-toolbox.
% global E ERROR  R RATE P MOMENTS AREAS
%
% Fuzzification: (e(1)=error e(2)=rate of change
% Ae  = match(E,e(1),ERROR);
% Ade = match(R,e(2),RATE);
% Ae is a row vector containing fit-values to which e(1) fits to the sets.
% NB=1; NM=2; NS=3; ZE=4; PS=5; PM=6; PB=7;
%
% error ->   NB NM NS ZE PS PM PB
% FAMbank = [0  0  0  PB 0  0  0   ;  NB    | rate
%            0  0  0  PM 0  0  0   ;  NM    V
%            0  0  0  PS 0  0  0   ;  NS
%            PB PM PS ZE NS NM NB  ;  ZE
%            0  0  0  NS 0  0  0   ;  PS
%            0  0  0  NM 0  0  0   ;  PM
%            0  0  0  NB 0  0  0  ];  PB
%
% aggop='t2ap';  % algebraic-product
% A=aggregat(Ae,Ade,FAMbank,aggop);
% Using the algebraic-product for the inference and a summation of the 
% outputs. Centre of Gravity method:
% do = apsumcog(FAMbank,A,MOMENTS,AREAS);

pause % Strike any key to continue...
clc

global error rate tp_look

% Using ctr_surf() will show the controller characteristics for the given 
% sets and for the defined operators:

[error,rate,tp_look]=ctr_surf('tp_dem_c',0.1,[-12 12 -6 6],20); 
echo off
% Preparation for the "lookup-example" : 

subplot(1,2,1),surf(error,rate,tp_look),title('controller: tp_dem_c');
xlabel('inference: alg.product');
subplot(1,2,2),contour(error,rate,tp_look),title('aggop = alg.product');
xlabel('defuzz.: COG'),pause

echo on
% We can now change set-shapes, operators and other methods to optimize 
% the controller.
% An alternative defuzzification for the controller above would be through
% the height method:

% do = apsumhei(FAMbank,A,MOMENTS,AREAS);

pause % Strike any key to continue...
clc

% Other forms of optimisation are possible, in addition to the methods
% described above, but they make the toolbox less flexible. 

% Once the controller has been established and only external scalers are in
% use for controller tuning, we can use a 2D-table lookup.
% Using the function ctr_surf() and the 2-D Table-Lookup Block in Simulink
% this is a simple task.

% Based on the controller developed previously, the Simulink file tp2dem_s
% provides a ready example. Both Simulink simulations may be started after 
% this demo without recourse to any other files.

% To run the simulations under SIMULINK: 
% (directly after this demo ends)
%
% - Open: tp_dem_s
% - Simulation: Start


% To run the simulation with the same controller, now as a 2D-table look-up:
% 
% - Execute from the workspace the script tp2_dat.m
% - Open: tp2dem_s
% - Simulation: Start

pause % Strike any key to continue...

% Any constructive feedback would be most welcome.
% Please feel free to criticize and comment upon this toolbox.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?