fuzdemo2.m
来自「FISMAT accommodates different arithmetic」· M 代码 · 共 246 行
M
246 行
function fuzdemo2
%# call fuzzy_p fuzzy_pi t3mi s3ma t2ap t1bd sumop
echo on
clc
% fuzdemo2.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
% Fuzzy and Neural controller for use under SIMULINK:
% For the implementation of a fuzzy or neural net controller some SIMULINK
% s-function files are provided:
% fuzzy11 - for a general controller with one input and one output.
% fuzzy21 - for a general controller with two inputs and one output.
% fuzzy31 - for a general controller with three inputs and one output.
% All files are very similar and it is easy to create new files for specific
% needs.
% Look in the files or use help for more information.
pause % Strike any key to continue.
clc
% Continous and discrete sets are possible. Depending on the set-type,
% chosen operators and methods, many means of implementation are possible.
% In the following part there are a few simple examples using the functions of the
% toolbox for control.
% (Declare the sets as global variables to get an access in the controller
% file)
global I O
% Example:
% The sets of the input are continuous, triangles in equidistant distances:
I = [-1 -0.5 0 0.5 1];
% The elements in the vector I are the abscissa values where the membership
% to the set is equal to one.
% The sets are ordered from left to right, from "neg.big" to "pos.big".
% Assume the output sets are singletons:
O = [-1 -0.5 0 0.5 1];
% Using plot_set() :
clf
plot_set(I),title('I=[-1 -0.5 0 0.5 1]'),pause % Strike any key to continue.
clc
% The rule-base for a one-dimensional or P-controller could be described as
% follows:
% IF i=positive-big THEN o=positive-big
% IF i=positive-small THEN o=positive-small
% IF i=near-zero THEN o=near-zero
% IF i=negative-small THEN o=negative-small
% IF i=negative-big THEN o=negative-big
% This simple case gives the following controller-file:
% function o=fuzzy_p(i,x,ts)
% global I O
% rb=fuzzsiyi(I,i);
% o =O*rb';
pause % Strike any key to continue.
clc
% For a simulation under SIMULINK the s-function file fuzzy11 is used. In
% the menu of the general s-function block, fuzzy11, fuzzy_p and the
% sampling time ts are required.
% The controller with aquidistant membership functions is linear.
% By changing the position of the peak values we can implement non-
% linearities:
% NB NS NZ PS PB
% I = [-1 -0.2 0 0.2 1];
% For the characteristic function of the controller we use ctr_func():
% ctr_func('fuzzy_p',0,[-1 1 -1 1])
pause % Press any key after plot...
clf
subplot(2,2,1);plot_set(I),title('I=[-1 -0.5 0 0.5 1');
subplot(2,2,3);ctr_func('fuzzy_p',0,[-1 1 -1 1]);
ylabel('output'),xlabel('input');
I = [-1 -0.2 0 0.2 1];
subplot(2,2,2);plot_set(I),title('I=[-1 -0.2 0 0.2 1]');
subplot(2,2,4);ctr_func('fuzzy_p',0,[-1 1 -1 1]);
ylabel('output'),xlabel('input');pause
clc
% For many control problems trapezoidal and symetrical sets are used.
% (e.g. B.Kosko: 'Neural Networks and Fuzzy Systems', 1992)
% To create a symetrical library of symetrical and unimodal sets, the
% function modsuset() is provided.
% After invoking this function, it is necessary to pick two characteristic
% points from the left side of the trapezoid around x=0 with the mouse.
% These two points must lie in the range x<0. The first point is the most
% left point of a trapezoid (y=0). For the first and second point the y
% values are automatically set to 0 and 1 respectively. Therefore you should
% be only "near" to y=0 or y=1 respectively.
% c contains the local centroids of all 7 sets.
% I contains the local area of all 7 sets.
% The overlap of the sets is displayed in the title.
% In the following example, please try to set the first point at x=-2 (and
% y about 0) and the second point at x=-0.5 (y about 1).
% Used without right-hand arguments the function immediately creates the
% library.
% The function modtrset() allows us to draw arbitrary trapezoids with the
% mouse.
% Example:
% [set,c,areas]=modsuset('y',90,7,-25,-5);
pause % Press any key after plot...
clc
[set,c,areas]=modsuset('y',90,7,-25,-5); pause
% The resulting set is given in the compact trapezoidal form. To get a
% discrete universe of discourse, the function con2dis(set,n) can be used:
% [Xdom,m,moments]=con2dis(set,100);
% plot_set(Xdom,m)
% produces Xdom as a 1x100 vector with the discrete universe of discourse.
% m as a matrix with the fitvalues for each set in a row.
% The function plot_set() can be used always for a check.
pause % Press any key after plot...
clc
[Xdom,m,moments]=con2dis(set,100);
plot_set(Xdom,m),title('discrete set after conversion'),pause % Strike any key to continue...
clc
% An example for a simple controller with two inputs and continuous sets:
%
% Fuzzy PI controller. Siler,Ying 1988/1989
% Define the fuzzy sets in the workspace global:
% eset=[-1 0 1]; oset=[-1 -0.5 0 0.5 1];
% global eset oset
% Start SIMULINK. (not now!)
% Open 'pi_siy_s'
% Simulation -> Start
% (controller file: pi_siyi.m)
% The function ctr_surf() to plot the control surface of a two dimensional
% fuzzy controller is used as follows:
% There are different ways to construct the controller. If the error-
% rate is generated by an external element, the controller needs two
% inputs. The controller in the first example (pi_siy_s) gets only the error
% signal and calculates the rate internally. Therefore it is not so easy to
% plot the controller characteristics.
% For the use of ctr_surf() we assume, that all dynamic elements are
% external. Therefore the controller is static.
% A fuzzy PI-controller could be described as follows: (fuzzy_pi.m)
pause % Strike any key to continue.
clc
% function o = fuzzy_pi(e,x,T)
% global eset oset
% Fuzzification :
% enT=fuzzsiyi(eset,e(1)); % e(1) corresponds to the error
% rnT=fuzzsiyi(eset,e(2)); % e(2) corresponds to the rate of error
% In the rule table the horizontal direction corresponds to enT and
% the vertical direction corresponds to rnT.
% NB=1; NS=2; NZ=3; PS=4; PB=5
% e-> N Z P
% rt=[NB NS NZ ; N | rate
% NS NZ PS ; Z V
% NZ PS PB]; P
% Using the Zadeh-AND-minimum operator and the Lukasiewicz-OR=bounded sum:
% rb=rtinf(rt,enT,rnT,'t3mi','s1bs');
% Defuzzification:
% o=oset*rb;
% Note: The controller above utilizes a very simple description
% of fuzzy sets described by Siler & Ying 1988/1989. For more
% details see the toolbox manual.
% If invoked without lefthand arguments ctr_surf() produces a surface and
% a contour plot of the controller characteristic.
% If the controller is implemented in the error path of a unity
% feedback system (error and error rate as inputs to the fuzzy
% controller) the contour plot is the phase plane plot.
pause % Strike any key to continue.
clear all
clear global
global eset oset
eset=[-1 0 1]; oset=[-1 -0.5 0 0.5 1];
% Depending on the number of points plotted the function ctr_surf() can be
% time consuming.
% Declaring two different output-sets and comparing the effect to the
% controller characteristic:
clf
[xa,ya,za]=ctr_surf('fuzzy_pi',1,[-1 1 -1 1],10);
clear global
eset=[-1 0 1]; oset=[-1 -0.2 0 0.2 1];
[xb,yb,zb]=ctr_surf('fuzzy_pi',1,[-1 1 -1 1],10);
subplot(2,2,1),surf(xa,ya,za);grid on,xlabel('error'),ylabel('rate'),...
zlabel('output'),subplot(2,2,2),contour(xa,ya,za);...
subplot(2,2,3),surf(xb,yb,zb);grid on,xlabel('error'),ylabel('rate'),...
zlabel('output'),subplot(2,2,4),contour(xb,yb,zb);pause
clc
% In the following part we introduce discrete sets and a more structured
% introduction showing how to construct a fuzzy logic controller.
pause % Strike any key to continue.
clc
st_demo
clc
% Any constructive feedback would be most welcome.
% Please feel free to criticize and comment upon this toolbox
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?