您好,欢迎访问三七文档
由于项目需要,需要模糊控制算法,之前此类知识为0,经过半个多月的研究,终于有的小进展。开始想从强大的互联网上搜点c代码来研究下,结果搜遍所有搜索引擎都搜不到,以下本人从修改的模糊控制代码,经过自己修改后可在vc6.0,运行!输入e表示输出误差,ec表示误差变化率,经过测试具有很好的控制效果,对于非线性系统和数学模型难以建立的系统来说有更好的控制效果!现将其公开供大家学习研究!#includestdio.h#includemath.h#definePMAX100#definePMIN-100#defineDMAX100#defineDMIN-100#defineFMAX100/*语言值的满幅值*/intPFF[4]={0,12,24,48};/*输入量D语言值特征点*/intDFF[4]={0,16,32,64};/*输出量U语言值特征点*/intUFF[7]={0,15,30,45,60,75,90};/*采用了调整因子的规则表,大误差时偏重误差,小误差时偏重误差变化*//*a0=0.3,a1=0.55,a2=0.74,a3=0.89*/intrule[7][7]={//误差变化率-3,-2,-1,0,1,2,3//误差{-6,-6,-6,-5,-5,-5,-4,},//-3{-5,-4,-4,-3,-2,-2,-1,},//-2{-4,-3,-2,-1,0,1,2,},//-1{-4,-3,-1,0,1,3,4,},//0{-2,-1,0,1,2,3,4,},//1{1,2,2,3,4,4,5,},//2{4,5,5,5,6,6,6}};//3/**********************************************************/intFuzzy(intP,intD)/*模糊运算引擎*/{intU;/*偏差,偏差微分以及输出值的精确量*/unsignedintPF[2],DF[2],UF[4];/*偏差,偏差微分以及输出值的隶属度*/intPn,Dn,Un[4];longtemp1,temp2;/*隶属度的确定*//*根据PD的指定语言值获得有效隶属度*/if(P-PFF[3]&&PPFF[3]){if(P=-PFF[2]){Pn=-2;PF[0]=FMAX*((float)(-PFF[2]-P)/(PFF[3]-PFF[2]));}elseif(P=-PFF[1]){Pn=-1;PF[0]=FMAX*((float)(-PFF[1]-P)/(PFF[2]-PFF[1]));}elseif(P=PFF[0]){Pn=0;PF[0]=FMAX*((float)(-PFF[0]-P)/(PFF[1]-PFF[0]));}elseif(P=PFF[1]){Pn=1;PF[0]=FMAX*((float)(PFF[1]-P)/(PFF[1]-PFF[0]));}elseif(P=PFF[2]){Pn=2;PF[0]=FMAX*((float)(PFF[2]-P)/(PFF[2]-PFF[1]));}elseif(P=PFF[3]){Pn=3;PF[0]=FMAX*((float)(PFF[3]-P)/(PFF[3]-PFF[2]));}}elseif(P=-PFF[3]){Pn=-2;PF[0]=FMAX;}elseif(P=PFF[3]){Pn=3;PF[0]=0;}PF[1]=FMAX-PF[0];if(D-DFF[3]&&DDFF[3]){if(D=-DFF[2]){Dn=-2;DF[0]=FMAX*((float)(-DFF[2]-D)/(DFF[3]-DFF[2]));}elseif(D=-DFF[1]){Dn=-1;DF[0]=FMAX*((float)(-DFF[1]-D)/(DFF[2]-DFF[1]));}elseif(D=DFF[0]){Dn=0;DF[0]=FMAX*((float)(-DFF[0]-D)/(DFF[1]-DFF[0]));}elseif(D=DFF[1]){Dn=1;DF[0]=FMAX*((float)(DFF[1]-D)/(DFF[1]-DFF[0]));}elseif(D=DFF[2]){Dn=2;DF[0]=FMAX*((float)(DFF[2]-D)/(DFF[2]-DFF[1]));}elseif(D=DFF[3]){Dn=3;DF[0]=FMAX*((float)(DFF[3]-D)/(DFF[3]-DFF[2]));}}elseif(D=-DFF[3]){Dn=-2;DF[0]=FMAX;}elseif(D=DFF[3]){Dn=3;DF[0]=0;}DF[1]=FMAX-DF[0];/*使用误差范围优化后的规则表rule[7][7]*//*输出值使用13个隶属函数,中心值由UFF[7]指定*//*一般都是四个规则有效*/Un[0]=rule[Pn-1+3][Dn-1+3];Un[1]=rule[Pn+3][Dn-1+3];Un[2]=rule[Pn-1+3][Dn+3];Un[3]=rule[Pn+3][Dn+3];if(PF[0]=DF[0])UF[0]=PF[0];elseUF[0]=DF[0];if(PF[1]=DF[0])UF[1]=PF[1];elseUF[1]=DF[0];if(PF[0]=DF[1])UF[2]=PF[0];elseUF[2]=DF[1];if(PF[1]=DF[1])UF[3]=PF[1];elseUF[3]=DF[1];/*同隶属函数输出语言值求大*/if(Un[0]==Un[1]){if(UF[0]UF[1])UF[1]=0;elseUF[0]=0;}if(Un[0]==Un[2]){if(UF[0]UF[2])UF[2]=0;elseUF[0]=0;}if(Un[0]==Un[3]){if(UF[0]UF[3])UF[3]=0;elseUF[0]=0;}if(Un[1]==Un[2]){if(UF[1]UF[2])UF[2]=0;elseUF[1]=0;}if(Un[1]==Un[3]){if(UF[1]UF[3])UF[3]=0;elseUF[1]=0;}if(Un[2]==Un[3]){if(UF[2]UF[3])UF[3]=0;elseUF[2]=0;}/*重心法反模糊*//*Un[]原值为输出隶属函数标号,转换为隶属函数值*/if(Un[0]=0)Un[0]=UFF[Un[0]];elseUn[0]=-UFF[-Un[0]];if(Un[1]=0)Un[1]=UFF[Un[1]];elseUn[1]=-UFF[-Un[1]];if(Un[2]=0)Un[2]=UFF[Un[2]];elseUn[2]=-UFF[-Un[2]];if(Un[3]=0)Un[3]=UFF[Un[3]];elseUn[3]=-UFF[-Un[3]];temp1=UF[0]*Un[0]+UF[1]*Un[1]+UF[2]*Un[2]+UF[3]*Un[3];temp2=UF[0]+UF[1]+UF[2]+UF[3];U=temp1/temp2;returnU;}voidmain(){inta=0,e,ec;/*intnowpoint,p1,p2=1;FILE*in,*out;in=fopen(in.txt,r);out=fopen(out.txt,w);*///while(!feof(in))while(1){//fscanf(in,%d,&nowpoint);//p1=nowpoint;//e=0-nowpoint;//ec=p1-p2;printf(请输入e:);scanf(%d,&e);printf(请输入ec:);scanf(%d,&ec);a=Fuzzy(e,ec);//fprintf(out,%d,a);//printf(%d:,p1);printf(e:%dec:%d,e,ec);printf(a:%d\n,a);//p2=p1;}//fclose(in);//fclose(out);}
本文标题:模糊控制算法c程序
链接地址:https://www.777doc.com/doc-4833195 .html