function [ J ] = objective_generall( Tbufferi,Pthreshold,start,days,price,time_excel)
%Finds the objective function
%Takes in the Tbufferi and Pthresholdj, runs the simulink model and returns the cost


%Parameters
par.cp.water = 4.19; %[kJ/Kg,K] SI Chemical Data
par.ro.water = 1; %[g/cm^3] SI Chemical Data

%Saturation limits
Qmin = 0.0;     %[kW]
Qmax = 5.5;     %[kW]
qin_min = 0;    %[m^3/s]
qin_max = 10;   %[m^3/s]

Qlb = Qmin;
Qub = Qmax;
qinlb = qin_min;
qinub = qin_max;




load day.mat

            qhw=[];
            p=[];

for i = start:(start+days-1);
    
    tsim = 3600*24;
    
    %constant demand
        
    qhw = day.flow;
    Thws = zeros(size(day.time))'+273+50;
    Tin = zeros(size(day.time))'+273+5;
    
    
%     %constant price
%     day(i) = getProfile;   
%     qhw(:,i) = day(i).flow;
%     Thws(:,i) = zeros(size(day(i).time))'+273+50;
%     Tin(:,i) = zeros(size(day(i).time))'+273+5;
    
    
    
    % varying price
    % PRICE: converting excel values into the dim of the distrubance matrix
    
    price_day(:,i) = price(:,i);
    price_days(:,i) = interp1(time_excel,price_day(:,i),day.time,'linear','extrap');
    
    %p(:,i) = price_days(:,i)';
    p=[p price_days(:,i)];
    
    %% Distubance matrix
%     
% %   %constant price
%     dsim.time = [day(i).time]'*3600;
%     dsim.signals.values = [qhw(:,i) Thws(:,i) Tin(:,i) p(:,i)]; % disturbances
%     dsim.dimentions = [4 1];
    

    %constant demand
    dsim.time = [day.time]'*3600;
    dsim.signals.values = [qhw Thws Tin p]; % disturbances
    dsim.dimentions = [4 1];
     

    %Tbuffer values
    %Tbuffertime = linspace(0,23,24)';
    Tbuffertime = [0 6 8 11 16 23 24]';  %Time in h
    Tbuffer_vect = (zeros(size(Tbuffertime))+Tbufferi')';
    
    Tbuffer.time = [Tbuffertime]*3600;
    Tbuffer.signals.values = [Tbuffer_vect]';
    Tbuffer.dimentions = [1 1];


    options = simset('SrcWorkspace','current');
    sim('simulinkmodel_Tbuffer_case2',[],options)
    J=simout(end,end)
    

   
end
end