You are on page 1of 6

#include <stdio.h> #include <math.h> #include <conio.h> /* mw= input membership width.*/ /* mwo= output membership width.

*/ const double mw=1; const double mwo=10; double a,b,c,d,e,f,g; double ao,bo,co,doo,eo,fo,go; double crispout; int mshaper=2; double x, anglerror,actualangle; //double nl, nm, ns, ze, ps, pm, pl; //double nld, nmd, nsd, zed, psd, pmd, pld; double nlo1,nlo2,nlo3, nmo, nso, zeo, pso, pmo1, pmo2, pmo3, pmo4, pmo5, pmo6, p mo7, pmo8,pmo9, plo1, plo2, plo3; double pso1, pso2,pso3,pso4,pso5,pso6,pso7,pso8,pso9,pso10,zeo1,zeo2,zeo3,zeo4,z eo5,nso1,nso2,nso3,nso4,nso5,nso6,nso7,nso8,nso9,nso10; double nmo1,nmo2,nmo3,nmo4,nmo5,nmo6,nmo7,nmo8,nmo9; int tm; double tsalle; //double fuzvect[]={nm, ns, ze, ps, pm}; double fuzvect[]={0, 0, 0, 0, 0}; //double efuzvectin[]={nl, nm, ns, ze, ps, pm, pl}; double efuzvectin[]={0, 0, 0, 0, 0}; //double defuzvectin[]={nld, nmd, nsd, zed, psd, pmd, pld}; double defuzvectin[]={0, 0, 0, 0, 0}; static double derror=0; static double detemp; //GETFUZZYVAL fuzzifies the given crisp input. double geterrfuzzyval (double , double[]); double getderrfuzzyval (double , double[]); double directu (double crisperror); //double getfuzzyval (double , double[]); //FUZZTOCRISP given the pointers fuzzy membership values vectors for error and d-error, calculates the crisp output. double fuzztocrisp (double[]); double fuzzinference (double[], double[]); double min(double , double ); double max(double , double ); double fuzzycontroller(double angleerr) //int main(double angleerr) {

a=-3*mw; b=-2*mw; c=-mw; d=0*mw; e=mw; f=2*mw; g=3*mw; ao=-3*mwo; bo=-2*mwo; co=-mwo; doo=0*mwo; eo=mwo; fo=2*mwo; go=3*mwo; //angleerr=1.3; //anglerror = angleerr; //anglerror = setangle-actualangle; //int zagaya = 0; //while (zagaya<5){ x = angleerr; double tempu = directu (angleerr);

geterrfuzzyval(5*x,efuzvectin); //efuzvectin[0]=fuzvect[0];efuzvectin[1]=fuzvect[1];efuzvectin[2]=fuzvec t[2];efuzvectin[3]=fuzvect[3]; //efuzvectin[4]=fuzvect[4];efuzvectin[5]=fuzvect[5];efuzvectin[6]=fuzvec t[6]; //derror=angleerr-detemp; derror=(angleerr-derror); //derror=x-derror; getderrfuzzyval(35*derror,defuzvectin); derror=angleerr; //defuzvectin[0]=fuzvect[0];defuzvectin[1]=fuzvect[1];defuzvectin[2]=fuz vect[2];defuzvectin[3]=fuzvect[3]; //defuzvectin[4]=fuzvect[4];defuzvectin[5]=fuzvect[5];defuzvectin[6]=fuz vect[6]; //printf("nl=%f nm=%f ns=%f ze=%f ps=%f pm=%f pl=%f",nl,nm,ns,ze,ps,pm,p l); printf("\n nm =%f ns =%f ze =%f ps =%f pm =%f ",efuzvectin[0],efuzvecti n[1],efuzvectin[2],efuzvectin[3],efuzvectin[4]); printf("\n nmd=%f nsd=%f zed=%f psd=%f pmd=%f ",defuzvectin[0],defuzvec tin[1],defuzvectin[2],defuzvectin[3],defuzvectin[4]); fuzzinference (efuzvectin, defuzvectin); printf("\n\n crispout=%f \n",crispout); //detemp=derror; //if (crispout<-200) // return -200; //else if (crispout>200) // return 200; //else //RETURN CONTROL VALUES //return max(tempu,crispout); //return tempu*crispout; //return tempu; return crispout; //return min(tempu,crispout); //zagaya++; //} } //THIS FUNCTION RETURNS FUZZY MEMBERSHIP VALUES FOR GIVEN CRISP VALUE double geterrfuzzyval (double crispval, double *fuzvect) { // INITIALISE MEMBERSHIP VALUES EVERY TIME, ELSE RULES AGGREGATION WILL GIVE FAL SE RESULT //nl=0; nm=0; ns=0; ze=0; ps=0; pm=0; pl=0; //fuzvect[0]=0;fuzvect[1]=0;fuzvect[2]=0;fuzvect[3]=0;fuzvect[4]=0; //efuzvectin[0]=0;efuzvectin[1]=0;efuzvectin[2]=0;efuzvectin[3]=0;efuzvectin[4]= 0;//efuzvectin[5]=0;efuzvectin[6]=0; //defuzvectin[0]=0;defuzvectin[1]=0;defuzvectin[2]=0;defuzvectin[3]=0;defuzvecti n[4]=0;//defuzvectin[5]=0;defuzvectin[6]=0; int k=0;

while (k == 0) { efuzvectin[0]=0;efuzvectin[1]=0;efuzvectin[2]=0;efuzvectin[3]=0;efuzvect in[4]=0; if(crispval<b) {efuzvectin[0]=1;break;} else ; if(crispval==b) {efuzvectin[0]=1;break;} else ; if(crispval<c) {{efuzvectin[0]=(c-crispval)/(c-b); efuzvectin[1]=(crispval-b)/(c-b);} break;} else ; if(crispval==c) {efuzvectin[1]=1;break;} else ; if(crispval<d) {{efuzvectin[1]=(d-crispval)/(d-c); efuzvectin[2]=(crispval-c)/(d-c);}break;}else ; if(crispval==d) {efuzvectin[2]=1;break;} else ; if(crispval<e) {{efuzvectin[2]=(e-crispval)/(e-d); efuzvectin[3]=(crispval-d)/(e-d);};break;} else; if(crispval==e) {efuzvectin[3]=1;break;} else ; if(crispval<f) {{efuzvectin[3]=(f-crispval)/(f-e); efuzvectin[4]=(crispval-e)/(f-e);};break;} else;//case3 if(crispval==f) {efuzvectin[4]=1;break;} else ; if(crispval>f) {efuzvectin[4]=1;break;} else ; k++; } return *efuzvectin; }

double getderrfuzzyval (double crispval, double *fuzvect) { int k=0; while (k == 0) { defuzvectin[0]=0;defuzvectin[1]=0;defuzvectin[2]=0;defuzvectin[3 ]=0;defuzvectin[4]=0; if(crispval<b) {defuzvectin[0]=1;break;} else ; if(crispval==b) {defuzvectin[0]=1;break;} else ; if(crispval<c) {{defuzvectin[0]=(c-crispval)/(c-b); defuzvectin[1]=(crispval-b)/(c-b);} break;} else ; if(crispval==c) {defuzvectin[1]=1;break;} else ; if(crispval<d) {{defuzvectin[1]=(d-crispval)/(d-c); defuzvectin[2]=(crispval-c)/(d-c);}break;}else ; if(crispval==d) {defuzvectin[2]=1;break;} else ; if(crispval<e) {{defuzvectin[2]=(e-crispval)/(e-d); defuzvectin[3]=(crispval-d)/(e-d);};break;} else; if(crispval==e) {defuzvectin[3]=1;break;} else ; if(crispval<f) {{defuzvectin[3]=(f-crispval)/(f-e); defuzvectin[4]=(crispval-e)/(f-e);};break;} else;//case3 if(crispval==f) {defuzvectin[4]=1;break;} else ; if(crispval>f) {defuzvectin[4]=1;break;} else ; k++; } return *defuzvectin; } //end of get fuzzyval function

//FUZZINFERENCE computes the output fuzzy sets for given inpur fuzzy sets. double fuzzinference (double *efuzvectin, double *defuzvectin) { double pmo,pso,zeo,nso,nmo; //plo1=min(efuzvectin[0],defuzvectin[0]); //plo2=min(efuzvectin[0],defuzvectin[1]); //plo3=min(efuzvectin[1],defuzvectin[0]); pmo1=min(efuzvectin[0],defuzvectin[0]); pmo2=min(efuzvectin[0],defuzvectin[1]); pmo3=min(efuzvectin[0],defuzvectin[2]); pmo4=min(efuzvectin[1],defuzvectin[0]); pmo5=min(efuzvectin[1],defuzvectin[1]); pmo6=min(efuzvectin[2],defuzvectin[0]); //pmo7=min(efuzvectin[2],defuzvectin[0] ); //pmo8=min(efuzvectin[2],defuzvectin[1] ); //pmo9=min(efuzvectin[3],defuzvectin[0]); pmo=max((max((max((max((max(pmo1,pmo2)),pmo3)),pmo4)),pmo5)),pmo6); //pmo=max((max((max((max(pmo1,pmo2)),pmo3)),pmo4)),pmo5); pso1=min(efuzvectin[0],defuzvectin[3]); pso2=min(efuzvectin[1],defuzvectin[2]); pso3=min(efuzvectin[2],defuzvectin[1] ); pso4=min(efuzvectin[3],defuzvectin[0]); //pso5=min(efuzvectin[2],defuzvectin[0]); //pso6=min(efuzvectin[2],defuzvectin[0]); //pso7=min(efuzvectin[2],defuzvectin[1]); //pso8=min(efuzvectin[3],defuzvectin[2]); //pso9=min(efuzvectin[4],defuzvectin[0]); //pso10=min(efuzvectin[4],defuzvectin[1]); pso= max((max((max(pso1,pso2)),pso3)),pso4); zeo1=min(efuzvectin[0],defuzvectin[4]); zeo2=min(efuzvectin[1],defuzvectin[3]); zeo3=min(efuzvectin[2],defuzvectin[2]); zeo4=min(efuzvectin[3],defuzvectin[1]); zeo5=min(efuzvectin[4],defuzvectin[0]); zeo=max((max((max((max(zeo1,zeo2)),zeo3)),zeo4)),zeo5); nso1=min(efuzvectin[1],defuzvectin[4]); nso2=min(efuzvectin[2],defuzvectin[3] ); nso3=min(efuzvectin[3],defuzvectin[2]); nso4=min(efuzvectin[4],defuzvectin[1]); //nso5=min(efuzvectin[4],defuzvectin[0]); //nso6=min(efuzvectin[4],defuzvectin[0]); //nso7=min(efuzvectin[4],defuzvectin[1]); nso=max((max((max(nso1,nso2)),nso3)),nso4); //nso8=min(efuzvectin[5],defuzvectin[2]); //nso9=min(efuzvectin[5],defuzvectin[3]); //nso10=min(efuzvectin[6],defuzvectin[0]); nmo1=min(efuzvectin[2],defuzvectin[4]); nmo2=min(efuzvectin[3],defuzvectin[3]); nmo3=min(efuzvectin[4],defuzvectin[2]); nmo4=min(efuzvectin[3],defuzvectin[4]); nmo5=min(efuzvectin[4],defuzvectin[3]); nmo6=min(efuzvectin[4],defuzvectin[4]); //nmo7=min(efuzvectin[6],defuzvectin[2]);

//nmo8=min(efuzvectin[6],defuzvectin[3]); //nmo9=min(efuzvectin[6],defuzvectin[4]); //nlo1=min(efuzvectin[5],defuzvectin[6]); //nlo2=min(efuzvectin[6],defuzvectin[5]); //nlo3=min(efuzvectin[6],defuzvectin[6]); nmo=max((max((max((max((max(nmo1,nmo2)),nmo3)),nmo4)),nmo5)),nmo6); //double psum1=(nmo1+nmo2+nmo3+nmo4+nmo5)*bo+(nso1+nso2+nso3+nso4+nso5+nso6+nso7 )*co; //double psum2=(zeo1)*doo+(pso1+pso2+pso3+pso4+pso5+pso6+pso7)*eo; //double psum3=(pmo1+pmo2+pmo3+pmo4+pmo5)*fo; //return crispout=(psum1+psum2+psum3)/(3*ao+9*bo+10*co+5*doo+10*eo+9*fo+3*go); //pnum=nmo*bo*mwo+nso*co*mwo+zeo*doo*mwo+pso*eo*mwo+pmo*fo*mwo; //pden=nmo+nso+zeo+pso+pmo; double pnum=nmo*bo+nso*co+zeo*doo+pso*eo+pmo*fo; double pden=nmo+nso+zeo+pso+pmo; return crispout=(pnum/pden); //return crispout=(psum1+psum2+psum3)/((nmo1+nmo2+nmo3+nmo4+nmo5)+(nso1+nso2+nso 3+nso4+nso5+nso6+nso7)+(zeo1)+(pso1+pso2+pso3+pso4+pso5+pso6+pso7)+(pmo1+pmo2+pm o3+pmo4+pmo5)); } //FUZZTOCRISP generates crisp output from fuzzy output vector. double fuzztocrisp (double *outfuzvectin) { return 0; } double min(double xx, double yy) { if (xx<yy) return xx; else return yy; } double max(double xx, double yy) { if (xx>yy) return xx; else return yy; } double directu (double crisperror) { double duu; int mxpander=1; if (crisperror<0) duu=((crisperror)*(crisperror)*(crisperror)*(crisperror))*mxpand er; else if (crisperror==0) duu=0; else duu=(-1*(crisperror)*(crisperror)*(crisperror)*(crisperror))*mxp ander; return duu;

You might also like