st_demo.m

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

M
214
字号
echo off
% st_demo.m - simple temperature control example with the fuzzy-toolbox.
%
% Script file relating to fuzdemo2 in the fuzzy-toolbox.
% 
%
% FSTB - Fuzzy Systems Toolbox for MATLAB
% Copyright (c) 1993-1996 by Olaf Wolkenhauer
% Control Systems Centre at UMIST
% Manchester M60 1QD, UK
%
% 22-April-1994
 
clc
echo on

% A fuzzy-systems can be described as follows:
%
% 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.
%
% In the first step we determine the membership to which the input belongs 
% (or fits) to the sets of the input-variable:
%       aj1=match(x1,Aj1), aj2=match(x2,Aj2), ... , aji=match(xi,Aji)
% ... , aji=match(xn,Ajn)
% The function match(xi,Aji) gives for a (crisp) input value the row-vector
% A back. The membership is calculated for all linguistic terms of the 
% variable. So Xi is applied to all Aji. 


pause % Press any key to continue...
clc
% In the next step we have to aggregate the aj's from the premise. This 
% realisation of the "AND" above can be done by using the min-operator 
% ('t3mi') for the aggregation-operator aggop. If the connections between 
% the facts is more an "OR", we could use max-operator ('s3ma') or others.
% In case of two inputs we can visualize the bank of FAM-rules in a matrix 
% form and use the function aggregat():
% A = aggregat(Ae,Ade,RuleBank,aggop)
% A = [a1, a2,...,aj,...,am] with aj=aggop(aj1,aj2,...,aji,...,ajn).
%
% We can now apply a certainty factor cj between 0 and 1 to each rule. This
% can be done by a multiplication or: aj*=min(aj,cj). An adaptive fuzzy-
% system could use cj, to change the systems structure. 

pause % Press any key to continue..
clc

% The next step is to determine the result Bj* for each rule using Bj and aj
% Bj*=infop(aj,Bj). The function inferenc() gives us the fuzzy conclusion:
% all_Bj*=inferenc(A,B,RuleBank,infop).
% Using the min-operator ('t3mi') for infop is called Min-Inference and 
% using the algebraic-product ('t2ap') is called Product-Inference. 
%
% The following step is done to summarize all Bj* to one B*: 
% B*=accop(B1*,B2*,...,Bj*,...,Bm*). 
% The accumulation operator could be an OR-operator like the max-operator 
% ('s3ma')=or the algebraic-product operator ('t2ap').
% In 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. In this case we use 'sumop' for the accumulation
% operator accop:
% B*=accumulu(all_Bj*,accop);

pause % Press any key to continue..
clc

% To determine a crisp output value from B*, the Centre Of Gravity method is
% usually used: b*=cog(B*)=MOMENT(B*)/AREA(B*) = sum(Y*B*')/sum(B*)
% with MOMEMT=sum(xi*m(B*)) and AREA(B*)=sum(m(B*)). 
% m() are the membership-values. 
% Depending on the operators used before, various optimisations are
% possible (or needed! - For a control simulation, the steps implemented
% as described before gives unacceptable times on a PC)

% In a simple example we have one variable T(temperature) and P(power).
% Modelling the libraries of sets for linguistic variables can be
% realised by the functions modlrset(), modsuset() or modtrset(). In this 
% simple case we can do this "by hand". Using the compact trapezoidal form:
% (Four characteristic points of a trapezoid in each row)
% The example and values are taken from the book T.Tilli: 'Automatisierungs-
% technik mit Fuzzy-Logik', Franzis Verlag 1993.

global T TEMP P POWER

% TEMP=[-inf 8  23 ;       POWER=[-100 -50  0  ;
%         0  18 30 ;              -100 -20 50  ;
%         8  23 35 ;               -50  20 100 ;
%        18  30 45];                 0  50 100]; 
%  

TEMP=[-inf 8 23; 0 18 30; 8 23 35; 18 30 45];
POWER=[-100 -50 0; -100 -20 50; -50 20 100; 0 50 100];  

% Converting the (continous) trapezoidal representation to a discrete form:
[T,TEMP]=con2dis(TEMP,200);
[P,POWER]=con2dis(POWER,200);

% Plotting the sets with the function plot_set() :

subplot(2,1,1),plot_set(T,TEMP);title('TEMP'); 
subplot(2,1,2),plot_set(P,POWER);title('POWER');pause


pause % Press any key to continue..
clc

% In this example we have the following rules:

% R1: IF TEMP=cold   THEN POWER=heat
% R2: IF TEMP=normal THEN POWER=zero
% R3: IF TEMP=hot    THEN POWER=cool

% A rule bank can be coded in a "matrix" form:
% RuleBank=[heat zero cool];
% (In case of two input variable we get a matrix/table which is more 
%  illustrative)

% Given a crisp input (temerature) we step through the procedure described
% above:

% Fuzzification:
% aT=match(T,15,TEMP);

% Aggregation:(in case of one variable we do not need the function aggregat)
% A=aT;

% Determine the result Bj* (= fuzzy conclusion to the given fact) 
% for each rule using Bj and aj.
% Min-Inference :
% infop='t3mi';
% B=inferenc(A,POWER,RuleBank,infop);

pause % Press any key to continue...
clc

% The next step is to summarize all Bj* to one B*: 
% B*=accop(B1*,B2*,...,Bj*,...,Bm*). 
% The operator accop can be an OR-operator. Another method
% typical for Koskos approach, is to sum up all the outputs. 
% accop='s3ma';
% B=accumulu(B,accop);

% Plotting the output library and the resulting output set:

% plot_set(P,POWER), title('POWER: "cool" "zero" "heat" ');
% plot_set(P,B), title('B*');

% Centre of Gravity method for a crisp output: 
% b*=cog(B*)=MOMENT(B*)/AREA(B*) with MOMENT(B*)=sum(xi*m(B*)) and AREA(B*)=
% sum(m(B*)) where m() are the membership values.
 
% output=sum(P*B')/sum(B);


pause % Press any key to continue...
clc

% The steps explained before are summarized in the function
% st_dsub(temperature,infop,accop)
% We now use this function for various inputs and different operators:

% crisp input temperature = 20 Celsius.

% This value "fits" exactly in the set "normal", therefore the second rule
% is fully satisfied and we expect the fuzzy output "zero" and a crisp out-
% put of 0.

% Min-Inference:
infop='t3mi';
accop='s3ma';
% st_dsub(20,infop,accop)

% By pressing any key a sequence of examples is plotted. 
% After each plot press any key to see the next.

pause % Press any key after plot...

st_dsub(20,infop,accop);
pause

st_dsub(25,infop,accop);
pause

infop='t2ap';
st_dsub(25,infop,accop);
pause

infop='t1bd';
st_dsub(25,infop,accop);
pause

infop='t2ap';
accop='sumop';
st_dsub(25,infop,accop);
pause

clc

% The algebraic-product for the inference and a summation of the fuzzy
% outputs gives not only a good result for the given input, it also allows
% simplifications, we need to speed up the simulation.

pause % Press any key to continue...


⌨️ 快捷键说明

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