⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sga__suspension_flc_std_2in1out_sfunction.m

📁 这是一个经典的遗传算法标准源程序
💻 M
字号:
function [sys,x0,str,ts] = SGA__suspension_flc_std_2in1out_sfunction(t,x,u,flag,ke,kec,ku,m,k)

% /*M-FILE Function SGA__suspension_flc_std_2in1out_sfunction MMM SSTSLAB */
% /*==================================================================================================
%  Simple Genetic Algorithm Laboratory Toolbox for Matlab 7.x
%
%  Copyright 2007 The SxLAB Family - Yi Chen - leo.chen.yi@gmail.com
% ====================================================================================================
%File description:
%  this sfunction is to realize a 2-in-1-out stand fuzzy logic controller
% 
%Input:
%
%   t -- time                                         [Not modify]
%   x -- state of s funciton                          [Not modify]
%   u -- inputs vector , where                        [by user]
%                             u(1)= sprung velocity
%                             u(2)= body accelerator
%   flag -- flat status                               [No modify]
%
%Output:
%   sys -- outputs vector        [by user]
%                       sys(1)=u
%   x0  -- sfunction std output  [Not modify]
%   str -- sfunction std output  [Not modify]
%   ts  -- sfunction std output  [Not modify]
%
% Appendix comments:
%  follow the example of timestwo.m, provide by matlab
%
% Usage:
%
%===================================================================================================
%  See Also:         SGA__suspension_flc_std_2in1out_sfunction
%                    
%
%===================================================================================================
%
%===================================================================================================
%Revision -
%Date         Name       Description of Change    email                 Location
%17-Jul-2003  Yi Chen    Initial version          leo.chen.yi@gmail.com Chongqing
%09-Jan-2007  Yi Chen    Update it as SGALAB demo leo.chen.yi@gmail.com Glasgow
%HISTORY$
%==================================================================================================*/

% SGA__suspension_flc_std_2in1out_sfunction Begin

% The following outlines the general structure of an S-function.
%
switch flag,
    case 0,
        [sys,x0,str,ts]=mdlInitializeSizes;
    case 2,
        sys=mdlUpdate(u,m,k,ke,kec,ku);
    case 3,
        sys=mdlOutputs(x);
    case{1,4,9},
        sys=[];
    otherwise
        error(['Unhandled flag = ',num2str(flag)]);
end

function [sys,x0,str,ts]=mdlInitializeSizes
sizes = simsizes;
sizes.NumContStates  = 0;
sizes.NumDiscStates  = 1;  % x
sizes.NumOutputs     = 1;  % sys
sizes.NumInputs      = 2;  % u
sizes.DirFeedthrough = 0;
sizes.NumSampleTimes = 1;  % at least one sample time is needed
sys = simsizes(sizes);

%
% initialize the initial conditions
%
x0  = [0];

%
% str is always an empty matrix
%
str = [];

%
% initialize the array of sample times
%
ts  = [-1  0];  

% end mdlInitializeSizes

function sys = mdlUpdate(u,m,k,ke,kec,ku);

[sys(1)] = auto_tuning_flc(ke,kec,ku,u(1),u(2),k,m);
%      u(1)= sprung velocity
%      u(2)= body accelerator
% end mdlUpdate

function sys = mdlOutputs(x)
sys=x;

%sys(1)=u_control;
% end mdlOutputs

function  [control_force] = auto_tuning_flc( ke,kec,ku,e,ec,k,m )
    
control_force = standard_flc(ke,kec,ku,e,ec) + m*(k*addition_FLC(ke,kec,ku,e,ec));

function [u] = standard_flc(ke,kec,ku,e,ec)
    
%%%%%%%%%%%%%%%%%%%% Part_I :Membership Functions %%%%%%%%%%%%%%%%%%%%

% Mamdani's fuzzy inference method is the most commonly seen fuzzy methodology.
% Mamdani's method was among the first control systems built using fuzzy set
% theory. It was proposed in 1975 by Ebrahim Mamdani [Mam75] as an attempt 
% to control a steam engine and boiler combination by synthesizing a set of
% linguistic control rules obtained from experienced human operators. 
% Mamdani's effort was based on Lotfi Zadeh's 1973 paper on fuzzy algorithms
% for complex systems and decision processes [Zad73].

% Although the inference process we describe in the next few sections differs 
% somewhat from the methods described in the original paper, the basic idea
% is much the same.

% Mamdani-type inference, as we have defined it for the Fuzzy Logic Toolbox,
% expects the output membership functions to be fuzzy sets. After the 
% aggregation process, there is a fuzzy set for each output variable that 
% needs defuzzification. it is possible, and in many cases much more 
% efficient, to use a single spike as the output membership function 
% rather than a distributed fuzzy set. This is sometimes known as a singleton
% output membership function, and it can be thought of as a pre-defuzzified 
% fuzzy set. It enhances the efficiency of the defuzzification process 
% because it greatly simplifies the computation required by the more general
% Mamdani method, which finds the centroid of a two-dimensional function. 
% Rather than integrating across the two-dimensional function to find the 
% centroid, we use the weighted average of a few data points. 

% Sugeno-type systems support this type of model. In general, Sugeno-type 
% systems can be used to model any inference system in which the output 
% membership functions are either linear or constant.

%Step0 - Creates a new Mamdani-style FIS structure
a = newfis('flc_std');

%%% Step1 - to add input parameter of e into FIS

% Step1-1
% divide into 5 levels
a = addvar( a,'input','e',[-5 5]);

% Step1-2 fuzzify e to E
a=addmf(a,'input',1,'NB','trapmf',[-5 -5 -4 -2]);
a=addmf(a,'input',1,'NS','trimf', [-4 -2  0]);
a=addmf(a,'input',1,'ZE','trimf', [-2  0  2]);
a=addmf(a,'input',1,'PS','trimf', [ 0  2  4]);
a=addmf(a,'input',1,'PB','trapmf',[ 2  4  5  5]);

% Step2- to add input parameter of ec into FIS
% Step2-1
a=addvar(a,'input','ec',[-6.25 6.25]);

%%%Step2-2 fuzzify ec to EC
a=addmf(a,'input',2,'NB','trapmf',[-6.25 -6.25 -5 -2.5]);
a=addmf(a,'input',2,'NS','trimf', [-5    -2.5   0]);
a=addmf(a,'input',2,'ZE','trimf', [-2.5   0     2.5]);
a=addmf(a,'input',2,'PS','trimf', [ 0     2.5   5]);
a=addmf(a,'input',2,'PB','trapmf',[ 2.5   5     6.25 6.25]);

%%%Step3 to add output parameter of u into FIS
% Step3-1
a=addvar(a,'output','Fd',[-1.165 1.165]);

%%%Step3-2 fuzzify u to U
a=addmf(a,'output',1,'NB','trapmf',[-2 -2 -1 -0.67]);
a=addmf(a,'output',1,'NM','trimf', [-1 -0.67 -0.33]);
a=addmf(a,'output',1,'NS','trimf', [-0.67 -0.33 0]);
a=addmf(a,'output',1,'ZE','trimf', [-0.33 0 0.33]);
a=addmf(a,'output',1,'PS','trimf', [ 0 0.33 0.67]);
a=addmf(a,'output',1,'PM','trimf', [ 0.33 0.67 1]);
a=addmf(a,'output',1,'PB','trapmf',[ 0.67 1 2 2]);


%%%%%%%%%%%%%%%%%%%%%%% Part II: Rule-bases building %%%%%%%%%%%%%%%%%%%%%%%
% Step 1 - define rules table
[ rule_base ] =...
[   1 1 7 1 1
    1 2 6 1 1
    1 3 5 1 1
    1 4 4 1 1
    1 5 4 1 1
    2 1 6 1 1
    2 2 5 1 1
    2 3 4 1 1
    2 4 4 1 1
    2 5 4 1 1
    3 1 5 1 1
    3 2 4 1 1
    3 3 4 1 1
    3 5 3 1 1
    4 1 4 1 1
    4 2 4 1 1
    4 3 4 1 1
    4 4 3 1 1
    4 5 2 1 1
    5 1 4 1 1
    5 2 4 1 1
    5 3 3 1 1
    5 4 2 1 1
    5 5 1 1 1 ];

   %Step2 -  add rule_base into FIS
    a=addrule( a,rule_base);
% to plot the rules
%   gensurf(a)

%%%%%%%%%%%%%%%%%%%%%%%%%% Part III Fuzzify inputs %%%%%%%%%%%%%%%%%%%%%%%%%% 

% from e to E , 
E = ke*e;

%%%confine E
if E > 4
    
    E = 4;

elseif E < -4
    
    E = -4;
    
end

% from ec to EC
EC=kec*ec;

 %%% confine EC
 if EC > 5
     
     EC = 5;
     
 elseif EC < -5
     
     EC = -5;
     
 end

%%%%%%%%%%%%%% Part IV Fuzzy Inference %%%%%%%%%%%%%%

  flc_input = [ E , EC ];
  % get output U from E and EC according the rule table
  U = evalfis( flc_input , a );
  
%%%%%%%%%%%%%% Part V Defuzzify %%%%%%%%%%%%%% 

% from U to u
  u = ku * U;
  
 function [p] = addition_FLC(ke,kec,ku,e,ec)
     
%%%%%%%%%%%%%%%%%% Part_I :Member-ship Functions %%%%%%%%%%%%%%%%%% 
%Step0 - Creates a new Mamdani-style FIS structure
 b=newfis('flc_add');
 
%Step1 - e
%Step1-1 to add input parameter of e into FIS
b=addvar(b,'input','e',[-5 5]);

%Step1-2 fuzzify e to E
b=addmf(b,'input',1,'NB','trapmf',[-5 -5 -4 -2]);
b=addmf(b,'input',1,'NS','trimf',[-4 -2 0]);
b=addmf(b,'input',1,'ZE','trimf',[-2 0 2]);
b=addmf(b,'input',1,'PS','trimf',[0 2 4]);
b=addmf(b,'input',1,'PB','trapmf',[2 4 5 5]);

%Step2 -ec
%Step2-1 to add input parameter of ec into FIS
b=addvar(b,'input','ec',[-6.25 6.25]);

%Step2-2 fuzzify ec to EC
b=addmf(b,'input',2,'NB','trapmf',[-6.25 -6.25 -5 -2.5]);
b=addmf(b,'input',2,'NS','trimf',[-5 -2.5 0]);
b=addmf(b,'input',2,'ZE','trimf',[-2.5 0 2.5]);
b=addmf(b,'input',2,'PS','trimf',[0 2.5 5]);
b=addmf(b,'input',2,'PB','trapmf',[2.5 5 6.25 6.25]);

%Step3 - output of p
%Step3-1 to add input parameter of u into FIS
b=addvar(b,'output','p',[-116.5 116.5]);

%%Step3-2 fuzzify p to P
b=addmf(b,'output',1,'NB','trapmf',[-116.5  -116.5  -100 -67]);
b=addmf(b,'output',1,'NM','trimf',[-100 -67 -33]);
b=addmf(b,'output',1,'NS','trimf',[-67 -33 0]);
b=addmf(b,'output',1,'ZE','trimf',[-33 0 33]);
b=addmf(b,'output',1,'PS','trimf',[0 33 67]);
b=addmf(b,'output',1,'PM','trimf',[33 67 100]);
b=addmf(b,'output',1,'PB','trapmf',[67 100 116.5 116.5]);

%Part II: Rule-bases
% [rule_base_1]=...
%   [ 1 1 7 1 1
%     1 2 7 1 1
%     1 3 4 1 1
%     1 4 4 1 1
%     1 5 4 1 1
%     2 1 6 1 1
%     2 2 7 1 1
%     2 3 4 1 1
%     2 4 3 1 1
%     2 5 4 1 1
%     3 1 6 1 1
%     3 2 5 1 1
%     3 3 4 1 1
%     3 5 2 1 1
%     4 1 4 1 1
%     4 2 5 1 1
%     4 3 3 1 1
%     4 4 2 1 1
%     4 5 2 1 1
%     5 1 4 1 1
%     5 2 4 1 1
%     5 3 3 1 1
%     5 4 1 1 1
%     5 5 1 1 1 ];

[rule_base_2]=...
  [ 1 1 4 1 1
    1 2 4 1 1
    1 3 4 1 1
    1 4 4 1 1
    1 5 4 1 1
    2 1 4 1 1
    2 2 5 1 1
    2 3 4 1 1
    2 4 3 1 1
    2 5 4 1 1
    3 1 6 1 1
    3 2 5 1 1
    3 3 4 1 1
    3 5 2 1 1
    4 1 6 1 1
    4 2 6 1 1
    4 3 3 1 1
    4 4 2 1 1
    4 5 2 1 1
    5 1 5 1 1
    5 2 5 1 1
    5 3 3 1 1
    5 4 1 1 1
    5 5 1 1 1 ];

   %%% add Rule_base into FIS
    b = addrule(b,rule_base_2);


 %%%%%%%%%%%%%%%%%%% Part III Fuzzify  %%%%%%%%%%%%%%%%%%

%%% from e to E , ec to EC
E=ke*e;
EC=kec*ec;

%%%confine E
 if E >4
    E=4;
 elseif E<-4
          E=-4;
 end   
 %%% confine EC
  if EC >5
    EC=5;
 elseif EC<-5
          EC=-5;
 end  
                  
 %%%%%%%%%%%%%%%%%% Part IV Fuzzy Inference  %%%%%%%%%%%%%%%%%%
  flc_add_input = [E,EC];
  P = evalfis(flc_add_input,b);
  
 %%%%%%%%%%%%%%%%%% Part V Defuzzify  %%%%%%%%%%%%%%%%%%
  p=ku*P;
 

⌨️ 快捷键说明

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