%File  Tbuffer.m
clear all 
close all


%% Case 5

Pthresholdvector = [0 0 0 0 0 0 0 0]+0.25; %Pthreshold init. x7 int.
Tbuffervector = [50]+273; %Tbuffer optimal from case 2.

decisionvector = [Tbuffervector Pthresholdvector];



%% Input data

prompt = {'Enter start day','Enter days of simulation'};
dlg_title = 'Simulation data';
num_lines = 1;
def = {'1','1'};
answere = inputdlg(prompt,dlg_title,num_lines,def);

[start, days] = answere{:};

start = str2num(start);
days = str2num(days);

if start > 31
    display('Start is to high')
    return
end
    if start<1
        display('Start is to low')
        return
    end
        if days>31
            display('To many days')
            return
        end
            if days<1
                display('To few days')
                return
            end

%% Energy price

% Energy price profile, Nordpoolspot
% continuously varying energy price 

fid = fopen('nordpoolspotmnd2.csv');
temp = textscan(fid,'%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q'); %32 collons, 1 for time, rest for days

time_excel = str2double(temp{1});

for i=2:32
price(:,i-1)= str2double(temp{i})/1000;
end


%% Cost calulation
    %Jvector=[];
opts=optimset('TolFun',1e-2,'TolX',1e-1);
options = optimset('Display','iter');

for k=start:(start+days-1)
    
    [Tbufferopt,J,EXITFLAG]= fminsearch(@(Tbufferi)objective_generall(Tbufferi,start,days,price,time_excel),decisionvector,opts)
   %[Tbufferopt,J,EXITFLAG]= fmincon(@(Tbufferi)objective_generall(Tbufferi,Pthreshold,start,days,price,time_excel),Tbuffervector,[],[],[],[],LB,UB,[],options)
end

%%

% plot(Tbuffervector - 273,J)
% %axis([1 24 0.23 0.31])
% title('$Total$ $cost$ $vs$ $T_{buffer}$','interpreter','latex','FontSize',14)
% xlabel('$T_{buffer}$ , $[{\circ}C]$','interpreter','latex','FontSize',14)
% ylabel('$Total$ $cost$, $[NOK]$','interpreter','latex','FontSize',14)


 
 

