clear variables
close all
clc

pocet_iteraci = 50;
kritname = 'kvadr';
get_SimData();
%   for i=1:10

%%  nacteni parametru optimalizace
%[ fmin_param, fmin_param_names ] = getfminparam();
[ fmin_param, fmin_param_names ] = loadfminparam();

%%  Nacteni dat

    measurename = ('Reg_step_neg');
%   measurename = ('Reg_step_pos');
%   measurename = ('Komora_step');
% 	measurename = ('Chlazeni_50_2');
%	measurename = ('Chlazeni2');
%   measurename = ('Ventilatory');
% 	measurename = ('Topeni100');
%   measurename = ('Svetla10');
%   measurename = ('Chlazeni50svetla50');
% 	measurename = ('Chlazeni10_new');
%	measurename = ('Chlazeni75_new');
%	measurename = ('Chlazeni100_new');
%	measurename = ('Svetla20_new');
%	measurename = ('Svetla80_new');
%	measurename = ('Topeni50_new');
%	measurename = ('ZvlOdvl_new');
filename = strcat('Data/', measurename,'.xlsx');

if exist(strcat('SimData/', measurename, '.mat'),'file') == 2;
    load(strcat('SimData/', measurename, '.mat'));
    [data_meas, data_meas_title, T_vz, meastime] = load_data(filename, endtime);
end

[ sim_param , data_meas] = sim_input( T_vz, simtime, data_meas, T_out, topeni, chlazeni, osvetleni, ventil, ventilator, Zvlhcovac_ovladani, Odvlhcovac_ovladani);


%%  parametry optimalizacni funkce
% opt_param = {T_vz, simtime, sim_param, data_meas, fmin_param_names, kritname};

opt_param = makeopt_param( measurename, fmin_param_names, kritname);

T_vz = opt_param{1};
simtime = opt_param{2};
sim_param = opt_param{3};
data_meas = opt_param{4};
data_meas_title = opt_param{7};

%%  vykresli puvodni krivky
start_values = getstartvalues(data_meas);
[data_sim, data_sim_title] = simulace( T_vz, simtime, sim_param, start_values, fmin_param, fmin_param_names);
% vykresli_grafy( data_meas, data_meas_title, data_sim, data_sim_title);
 vykresligrafy2(data_meas, data_meas_title, data_sim, data_sim_title);
% vykresli_grafy3( data_meas, data_meas_title, data_sim, data_sim_title);
% 
% %%  fminsearch
% options = optimset('Display','iter','PlotFcns',@optimplotfval);
% options.MaxIter = pocet_iteraci;
% options.TolFun = 1e-10;
% fmin_param_end = fminsearch(@(fmin_param) optimalizace(fmin_param, opt_param), fmin_param, options);
% 
% save('fmin_param.mat', 'fmin_param_end', 'fmin_param_names');
% savefminparam(fmin_param_end)
% end
% 
% %%  vykresli optimalizovane krivky
% [data_sim, data_sim_title] = simulace( T_vz, simtime, sim_param, start_values, fmin_param_end, fmin_param_names);
% vykresli_grafy(data_meas, data_meas_title, data_sim, data_sim_title);
% vykresligrafy2(data_meas, data_meas_title, data_sim, data_sim_title);


