clear all
close all
clc

cd('C:\Users\qinz\Desktop\sent_to_Qin')

load MCD_results.mat

unsorted_MCD_total = [];
weibull_para = [];
Trunc_Power_para = [];
Fitted_ana_prob = [];

fig_num = 1;
clear sorted_floe_size index
[sorted_floe_size,index]=sort(Raw_MCD);
a_sorted_floe_size=Raw_MCD(index);
MCD = a_sorted_floe_size;
N_total = size(MCD,2);
for i = 1:1:size(MCD,2)
    N_L(i) = size(find(MCD>=MCD(i)),2)/N_total;
end

figure(fig_num)

loglog(MCD,N_L,'ro','MarkerSize',8,'LineWidth',2)
hold on
% loglog(MCD_norm,N_L,'ro','MarkerSize',7)
% loglog(MCD_norm,N_L,'ro','MarkerSize',8)
grid on

%---start fitting---
x=MCD;
y=N_L;

%---three methods---
% [eta1_trunc_power, eta2_weibull, eta3_power]=three_fitting_method_and_plotting(fig_num,x,y)
% weibull_para = [eta2_weibull];
% Trunc_Power_para = [eta1_trunc_power];

%---power law---
F_powerlaw0=@(epsilong,x)(epsilong(1)*(x.^(-1*epsilong(2))));
epsilong0 = [min(x) 1];
[epsilong,resnorm,residual,exitflag,output,~,jacobian] = lsqcurvefit(F_powerlaw0,epsilong0,x,y);
x_plot=[min(x):0.001:max(x)*1.5];
y_fit_powerlaw0 = (epsilong(1)*(x_plot.^(-1*epsilong(2))));
figure(fig_num)
hold on

loglog(x_plot,y_fit_powerlaw0,'k','LineWidth',2)
eta=epsilong;

set(gcf, 'Color','w')
set(gca,'Box','off','Fontsize',18)
xlabel(gca,'MCD[m]','Fontsize',25,'interpreter','Latex');
ylabel(gca,'Cumulative Frequency','Fontsize',25,'interpreter','Latex');
legend(gca,{'Observed data','Power law fitting''},'Box','on')
legend('location','southwest')

axis tight



