
close all, clc

%%
NumPoints = 555
%data = rand(NumPoints,2);
figure('Name','Original data')
plot(data(:,1), data(:,2),'o');
xlabel ('feature 1')
ylabel ('feature 2')
%%
options = [NaN 500 NaN 0];
[center,U,obj_fcn] = fcm(data,3,options);
center
degrees = U'
%%
maxU = max(U);
% Find the data points with highest grade of membership in cluster 1
index1 = find(U(1,:) == maxU);
% Find the data points with highest grade of membership in cluster 2
index2 = find(U(2,:) == maxU);
% Find the data points with highest grade of membership in cluster 3
index3 = find(U(3,:) == maxU);

figure('Name','FCM results - lines and colors')
line(data(index1,1),data(index1,2),'marker','*','color','g');
line(data(index2,1),data(index2,2),'marker','*','color','r');
line(data(index3,1),data(index3,2),'marker','*','color','b');
% Plot the cluster centers
hold on
%plot([center([1 2],1)],[center([1 2],2)], [center([1 2],3)], '*','color','k','linewidth', 3)
plot([center([1 2 3],1)],[center([1 2 3],2)], '*','color','k','linewidth', 3)
hold off;

figure('Name','FCM results - colors')
scatter(data(index1,1),data(index1,2),30,'g');
hold on
scatter(data(index2,1),data(index2,2),30,'r');
scatter(data(index3,1),data(index3,2),30, 'b');
% Plot the cluster centers
hold on
%plot([center([1 2],1)],[center([1 2],2)], [center([1 2],3)], '*','color','k','linewidth', 3)
plot([center([1 2 3],1)],[center([1 2 3],2)], '*','color','k','linewidth', 3)
hold off;

figure ('Name', 'Objective function')
plot(obj_fcn, 'linewidth', 2)
xlabel ('epochs')
ylabel('objective function')
hold on
plot(15:size(obj_fcn),obj_fcn(15:end),'m', 'linewidth', 2)

figure ('Name', 'Objective function - details')
plot(15:size(obj_fcn),obj_fcn(15:end),'m', 'linewidth', 2)
xlabel ('epochs')
ylabel('objective function')
