% fzrelationdemo.m - fuzzy relation demonstration % Define fuzzy relations much_older as: % mu(x,y)=0 if x <= y % mu(x,y)=(x-y)/20 if 0 <= x-y < 20 % mu(x,y)=1 if x > y + 20 % Given Dana is (a) matured (b) exactly 43 years old % infer Mary's age % Both max-min and max-product relation composition rules are used. % (C) 2005 by Yu Hen Hu % created: 12/4/2005 % call fzcompose, fuzify, fzor clear all, close all % define fuzzy sets {children, teenager, young, 30something, middleage, matured, old} % over age=[0:60]; age=[0:60]; M=fsgen(7,age); figure(1),plot(age,M'); [X,Y]=meshgrid(0:60,0:60); Rmuch_older=(min(1,max(0,[X-Y]*0.05)))'; % R(x,y) is the relations x >> y figure(2),imagesc(Rmuch_older),colormap('gray') %%%%%%%% part (a) if dana is matured, what is mary's age dana=[0 0 0 0 0 1 0]*M; % Dana's age is matured mary=fzcompose(dana,Rmuch_older); figure(3),plot(age,mary,'r',age,dana,'g') legend('Mary', 'Dana'), title('(a) Fuzzy sets of ages of Mary and Dana') mary1=fzcompose(dana,Rmuch_older,1); % max product composition figure(4),plot(age,mary1,'r',age,dana,'g') legend('Mary', 'Dana'), title('(a) Max-product solution') %%%%%%%% part (b) if dana is matured, what is mary's age danastar=43; % dana's age in years omega = fuzify(M,danastar,age); % 1 x size(M,1) disp('compatibility omega = '); disp(num2str(omega,4)); % gives compatibility (activation) of each fuzzy set. danab=omega*M; maryb=fzcompose(danab,Rmuch_older); marystar=defuz(maryb,age); disp(['If Dana is ' int2str(danastar) ', then Mary is ' int2str(marystar)]); figure(5), plot(age,maryb,'r',age,danab,'g'),hold on stem([marystar danastar],[1 1]); legend('Mary','Dana'), title([' (b) Dana is ' int2str(danastar) ', Mary is ' int2str(marystar)]); maryb1=fzcompose(danab,Rmuch_older,1); % max product composition marystar1=defuz(maryb1,age); disp(['Use Max-product composition, Mary is ' int2str(marystar1)]); figure(6), plot(age,maryb1,'r',age,danab,'g'),hold on stem([marystar1 danastar],[1 1]); legend('Mary','Dana'), title([' (b) Max-product composition, Mary is ' int2str(marystar1)]);