clear all
clc
close all
% skript slouží k výpočtu optimalizace pomocí Curve fitting , funkce jsou
% natáhnuté z Optimization toolbox 
%%%%% 

load('funkce1.mat');
load('funkce2.mat');
funkce1=funkce2;
x = 0.1:0.001:60.5;
x(1,1)=0.001;
x=x(1,1:60001);
 
x2= 0.1:0.001:40.5;
x2(1,1)=0.001;
x2=x2(1,1:40001);
x2=x;

%% Initialize arrays to store fits and goodness-of-fit.
gama_funkce2 = cell( 3, 1 );
gof = struct( 'sse', cell( 3, 1 ), ...
'rsquare', [], 'dfe', [], 'adjrsquare', [], 'rmse', [] );
% Fit: 'untitled fit 3'.
[xData, yData] = prepareCurveData(x2,funkce1);
%%'1000000*exp(-(log(x)-a)^(2)/(2*b^(2)))/(x*b*sqrt(2*pi))'
% Set up fittype and options.
ft = fittype('1200000*gampdf(x,a,b)', 'independent', 'x', 'dependent', 'y' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'Off';
opts.MaxFunEvals = 100;
opts.MaxIter = 300;
opts.StartPoint = [0.792207329559554 2];

% Fit model to data.
[gama_funkce2{3}, gof(3)] = fit( xData, yData, ft, opts );

% Plot fit with data.
figure( 'Name', 'gama' );
gama_funkce2plot = plot( gama_funkce2{3}, xData, yData );
legend(gama_funkce2plot, 'ast vs. xcx', 'gama', 'Location', 'NorthEast' );
% Label axes
xlabel xcx
ylabel ast
grid on
% %%
lognormal_funkce2 = cell( 3, 1 );
gof1 = struct( 'sse', cell( 3, 1 ), ...
'rsquare', [], 'dfe', [], 'adjrsquare', [], 'rmse', [] );
% Fit: 'untitled fit 3'.
[xData, yData] = prepareCurveData(x2,funkce1);
% Set up fittype and options.
ft = fittype('1000000*exp(-(log(x)-a)^(2)/(2*b^(2)))/(x*b*sqrt(2*pi))', 'independent', 'x', 'dependent', 'y' );
%ft = fittype('1000000*lognpdf(x,a,b)', 'independent', 'x', 'dependent', 'y' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'Off';
opts.MaxFunEvals = 100;
opts.MaxIter =300;
opts.StartPoint = [0.792207329559554 2];

% Fit model to data.
[lognormal_funkce2{3}, gof1(3)] = fit( xData, yData, ft, opts );

% Plot fit with data.
figure( 'Name', 'untitled fit 3' );
lognormal_funkce2plot = plot(lognormal_funkce2{3}, xData, yData );
legend('ast vs. xcx', 'lognormal', 'Location', 'NorthEast' );
% Label axes
xlabel xcx
ylabel ast
grid on
% 
% %% 
LDRW = cell( 3, 1 );
gof2 = struct( 'sse', cell( 3, 1 ), ...
'rsquare', [], 'dfe', [], 'adjrsquare', [], 'rmse', [] );
% Fit: 'untitled fit 3'.
[xData, yData] = prepareCurveData(x2,funkce1);
% Set up fittype and options.
ft = fittype('850000*(exp(a)/b)*(sqrt((b*a)/(x*2*pi)))*exp((-1/2)*a*((b/x)+(x/b)))', 'independent', 'x', 'dependent', 'y' );
%ft = fittype('1000000*lognpdf(x,a,b)', 'independent', 'x', 'dependent', 'y' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'Off';
opts.Lower = [0 0];
opts.StartPoint = [0.744468418970804 0.521552516961098];
opts.Upper = [10 10];


% Fit model to data.
[LDRW{3}, gof2(3)] = fit( xData, yData, ft, opts );

% Plot fit with data.
figure( 'Name', 'LDRW' );
LDRW_funkce2plot = plot(LDRW{3}, xData, yData );
legend('ast vs. xcx', 'LDRW', 'Location', 'NorthEast' );
% Label axes
xlabel xcx
ylabel ast
grid on
%% 
FTP = cell( 3, 1 );
gof3 = struct( 'sse', cell( 3, 1 ), ...
'rsquare', [], 'dfe', [], 'adjrsquare', [], 'rmse', [] );
% Fit: 'untitled fit 3'.
[xData, yData] = prepareCurveData(x2,funkce1);
% Set up fittype and options.
ft = fittype('10000000*(exp(a)/b)*(sqrt(b/(x)))*(a/(2*pi))^(3/2)*exp((-1/2)*a*((b/x)+(x/b)))', 'independent', 'x', 'dependent', 'y' );
%ft = fittype('1000000*lognpdf(x,a,b)', 'independent', 'x', 'dependent', 'y' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'Off';
opts.MaxFunEvals = 100;
opts.MaxIter =300;
opts.StartPoint = [0.792207329559554 2];

% Fit model to data.
[FTP{3}, gof3(3)] = fit( xData, yData, ft, opts );

% Plot fit with data.
figure( 'Name', 'FTP' );
FTP_funkce2plot = plot(FTP{3}, xData, yData );
legend('ast vs. xcx', 'FTP', 'Location', 'NorthEast' );
% Label axes
xlabel xcx
ylabel ast
grid on
%%
%% 
% ERLANG = cell( 3, 1 );
% gof = struct( 'sse', cell( 3, 1 ), ...
% 'rsquare', [], 'dfe', [], 'adjrsquare', [], 'rmse', [] );
% % Fit: 'untitled fit 3'.
% [xData, yData] = prepareCurveData(x,funkce2);
% % Set up fittype and options.
% ft = fittype('((x)^(2)*exp(-x/b))/(b*(2))', 'independent', 'x', 'dependent', 'y' );
% %ft = fittype('1000000*lognpdf(x,a,b)', 'independent', 'x', 'dependent', 'y' );
% opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
% opts.Display = 'Off';
% opts.MaxFunEvals = 100;
% opts.MaxIter =300;
% opts.StartPoint = [0.792207329559554 2];
% 
% % Fit model to data.
% [ERLANG{3}, gof(3)] = fit( xData, yData, ft, opts );
% 
% % Plot fit with data.
% figure( 'Name', 'FTP' );
% ERLANG_funkce2plot = plot(ERLANG{3}, xData, yData );
% legend('ast vs. xcx', 'ERLANG', 'Location', 'NorthEast' );
% % Label axes
% xlabel xcx
% ylabel ast
% grid on
%%
figure(10)
plot(FTP{3});
hold on 
plot(LDRW{3},xData, yData );
  hold on 
 plot(lognormal_funkce2{3});
  hold on
  plot( gama_funkce2{3});
legend('data','FTP','LDRW','lognormal','gama' );
