%% ***** Instructions on the utlization of LSL algorithm****** %%******1. When the number of linguestic variables in the input sets or %% output sets is not same, the program will add new linguestic variable with zero value %% to the ones with less linguestic variables to make them equal in size : example 1st input %% set has 2 linguestic variables and 2nd input set has 3 linguestic variables, the program %% will add 1 linguestic variable to in 1st set with zero value %% ****2. In each rule, you have to use either "and" or "or" in antcedence or consequent. Both can't be used %% in the same rule for antcedence or consequent. clc clear all %input No_Input_set=2; for j=1:1:No_Input_set input_sets={'temperature','temperature_difference'}; fuzzy_input_name(j)=input_sets(j); num_liguestic_variables=7; no_lin_var(j)=num_liguestic_variables; end k=size(no_lin_var); i=1; u=1; p=1; q=1; while i<=k(2) for o=1:1:no_lin_var(i) fprintf('the name of the % f lingeustic variable of the fuzzy input set % f is',o,i) input(' ','s') t=ans; y(p,o)={t}; lingestic_input=t; fprintf('starting point of the % s lingeustic variable is',t) input(' ') sp=ans; start_point(p,o)=sp; fprintf('width of the % s lingeustic variable is',t) input(' ') wl=ans; width_ling(p,o)=wl; end_point(p,o)=sp+wl; spp=sp+wl; rectangle('position',[sp, 200-10*(u-1),wl,10],'curvature',[0,0]) text((sp+wl/2),200-10*(u-1)+5,y(p,o)); hold on u=q+1; q=q+1; end kt=[230 130] p=p+1; text(sp+wl/2,kt(i),fuzzy_input_name(i)) i=i+1; end %output No_output_set=input('Enter the number of fuzzy output set'); for j2=1:1:No_output_set fprintf('name of the %f fuzzy output set',j2) input(' ','s'); fuzzy_output_name={ans} fprintf('how many liguestic variable in % f output set',j2) input(' '); no_lin_var2(j2)=ans; end k2=size(no_lin_var2); i2=1; u2=1; while i2<=k2(2) for o2=1:1:no_lin_var2(i2) fprintf('the name of the % f lingeustic variable of the fuzzy output set % f is',o2,i2) input(' ','s') t2=ans; y2(i2,o2)={t2}; lingestic_input=t2; fprintf('starting point of the % s lingeustic variable is',t2) input(' ') sp2=ans; start_point2(i2,o2)=sp2; fprintf('width of the % s lingeustic variable is',t2) input(' ') wl2=ans; width_ling2(i2,o2)=wl2; rectangle('position',[sp2+spp+wl+20, 200-10*(u2-1),wl2,10],'curvature',[0,0]) text((sp2+spp+wl+20+wl2/2),200-10*(u2-1)+5,y2(i2,o2)); hold on u2=o2+1; end i2=i2+1; end text(sp2+sp+wl+20+wl2/2,130,fuzzy_output_name) %% laser simulator logic membership calculation: Numerator for(f=1:1:No_Input_set) fprintf('enter the crisp value of % f input ',f) input(' '); xf(f)=ans; t=1:1:250; val(t)=xf(f); %%text(val(f),120*f,y); plot(val,t,'r--','linewidth',3); end for(f=1:1:No_Input_set) x=xf(f); op=no_lin_var(f); for (r=1:1:op) %%numerator of mu start_pointi1(f,r)=start_point(f,r); m(f,r)=start_point(f,r)+width_ling(f,r)/2; e(f,r)=start_point(f,r)+width_ling(f,r); ks_a(f,r)=(x-start_pointi1(f,r))>0; ui_a(f,r)=1-ks_a(f,r); ks_b(f,r)=(e(f,r)-x)>0; ui_b(f,r)=1-ks_b(f,r); xa(f,r)=x-start_pointi1(f,r); xb(f,r)=e(f,r)-x; mu_numf(f,r)=min([xa(f,r) xb(f,r)]); end end mu_num=mu_numf; %% laser simulator logic membership calculation: Denomerator for(f=1:1:No_Input_set) op=no_lin_var(f); for (r=1:1:op) if(mu_numf(f,r)<=0) ghy(f,r)=0; mu_num(f,r)=0; else ghy(f,r)=1; end end end %% summation of ui(bi-x) multiply by min(x-a,b-x) %% summation of ui(x-ai)multiply by min(x-a,b-x) for(f=1:1:No_Input_set) op=no_lin_var(f); ksa2den=0; for (r=1:1:op) ks_a2denf(f,r)=ksa2den+mu_num(f,r).*ghy(f,r); ksa2den=ks_a2denf(f,r); if(no_lin_var(f)>=no_lin_var(1)) no_lin_var_max=no_lin_var(f); no_lin_var(1)=no_lin_var_max; end end ksa2denf(f)=ksa2den; end ksa2denf_final=repmat(ksa2denf',1,no_lin_var_max); %% multiplication of 1-ui(x-ai) and mi-x %% multpication of 1-ui(bi-x) and mi-x for(i=1:1:No_Input_set) frt=start_pointi1(i,:); fet=e(i,:); frt(1)=[]; fet(r)=[]; ms(i)=m(i,1); mf(i)=m(i,no_lin_var(i)); if(xf(i)fet) msf(i)= abs(mf(i)-xf(i)); else msf(i)=0; end end msf_final=repmat(msf',1,no_lin_var_max); mu_den=msf_final+ksa2denf_final; %% %% laser simulator logic membership membership value muk=mu_num./mu_den; muf=muk; mu=muf' %%Inference rules rule=input('how many rules you have'); for i=1:1:rule fprintf('how many input sets in the antecedence in %f rule',i); input(' ') io=ans; for (rw=1:1:io) fprintf('which input set is % f antecedence of % f rule (enter the input set number)',rw,i); input(' '); i1(i,rw)=ans; fprintf('which linguestic variable in % f antecedence of %f input set in the % f rule(enter the linguestic variable order)',rw,i1,i); input(' '); i2(i,rw)=ans; ji(i,rw)=fuzzy_input_name(rw); iuy=y{i1,i2}; j1(i,rw)={iuy}; mui(i,rw)=mu(i,rw); end fprintf('how many output sets in the consequent in %f rule',i); input(' ') io2=ans; for(rw2=1:1:io2) fprintf('which output set is % f consequent of % f rule (enter the output set number)',rw2,i); input(' '); i3(i,rw2)=ans; fprintf('which linguestic variable in % f consequent of %f output set in the % f rule(enter the linguestic variable number)',rw2,i3,i); input(' '); i4(i,rw2)=ans; jo(i,rw2)=fuzzy_output_name(rw2); iuy2 =y2{i3,i4}; j2p(i,rw2)={iuy2}; %% yout(i)=width_ling2(i,:)+start_point2(i,:); end fprintf('which operator you want to use between antecedance and consequent (and) or (or), for (and) enter a and for (or) enter o') input(' ','s'); iao=ans; if (iao=='a') %% and %% fprintf('%f rule AND: If %s is %s , respectively, Then %s is %s , repectively\n',i,ji, j1, jo,j2p ); mu_implicat(i)=min(mui(i,:)) end if (iao=='o') %% or %% fprintf('%f rule Or: If %s is %s , respectively, Then %s is %s , repectively\n',i,ji, j1, jo,j2p ); mu_implicat(i)=max(mui(i,:)); end end %% implication process for(im=1:1:rule) output_implicat(im)=mu_implicat(im)*width_ling2(i3(im),i4(im))+start_point2(i3(im),i4(im)) end output_crisp=sum(output_implicat)/im; %% just to compare implication process %%yout1=mu1_f*width_ling2(kco3(1))+start_point2(kco3(1)); %%val1(t)=yout1; %% just to compare and draw aggregation and defuzzification %%youtput=(yout2+yout3)/2+(spp+wl+20); %%youtput1=(yout2+yout3)/2; val4(t)=sp2+spp+wl+20+output_crisp; plot(val4,t,'g--','linewidth',3); text(val4(1),150,' Crisp Output'); fprintf('the crisp output is % f',output_crisp);