% FuzzyTest.m % % Exercise functions associated with fuzzy logic: % m = FuzzyMembership(membership_left_center_right_pts, x) % m_AND = FuzzyAND(m_array) % c = FuzzyCentroid(m) membership_left_center_right_pts = ... [0.5, 1.0, 1.5]; % Specify x values. x_pts = 0:0.1:2; % Calculate membership values for membership function m_vec = FuzzyMembership(membership_left_center_right_pts, x_pts); % Plot the membership function. figure(1) plot(x_pts, m_vec, 'b-') set(gcf,'color','white') xlabel('x') ylabel('membership') title('Fuzzy membership function') % Create mesh for plot of fuzzy AND. x1_limits = [0, 2]; % [min, max] for x1. x2_limits = [0, 2]; % [min, max] for x2. num_pts_x1_and_x2 = 51; % number of pts in both x1 and x2 directions. % Create the x1,x2 pts. x1_spacing = (x1_limits(2)-x1_limits(1))/(num_pts_x1_and_x2 - 1); x2_spacing = (x2_limits(2)-x2_limits(1))/(num_pts_x1_and_x2 - 1); x1 = x1_limits(1) : x1_spacing : x1_limits(2); x2 = x2_limits(1) : x2_spacing : x2_limits(2); [xx1,xx2] = meshgrid(x1,x2); % Calculate fuzzy_AND of membership of pts on grid for x1_index = 1:size(xx1,1) for x2_index = 1:size(xx2,2) m1 = FuzzyMembership(membership_left_center_right_pts, ... xx1(x1_index,x2_index)); m2 = FuzzyMembership(membership_left_center_right_pts, ... xx2(x1_index,x2_index)); m_AND(x1_index,x2_index) = FuzzyAND([m1, m2]); end end % Plot the surface. figure(2) surf(xx1,xx2,m_AND) set(gcf,'color','white') xlabel('x1') ylabel('x2') zlabel('Fuzzy AND') title('Fuzzy AND function of membership functions') % Calculate centroids of memberships of pts on grid c_array = FuzzyCentroid(m_AND); % Plot the surface. figure(3) surf(xx1,xx2,c_array) set(gcf,'color','white') xlabel('x1') ylabel('x2') zlabel('Centroid value') title('Centroid value of fuzzy membership values')