%File  Tbuffer.m
clear all 
close all

%Tbuffervector = [90 90]+273; %Tbuffer init., x1 int.
%Tbuffervector = [90 65 80]+273; %Tbuffer init., x2 int.
%Tbuffervector = [90 90 65 90]+273; %Tbuffer init., x3 int.
%Tbuffervector = [90 90 80 65 90] +273; %Tbuffer init., x4 int.
%Tbuffervector = [90 90 80 65 90 65]+273; %Tbuffer init., x5 int.
%Tbuffervector = [90 90 80 65 90 65 90]+273; %Tbuffer init., x6 int.
Tbuffervector = [87 90 87 64 60 47 45 87]+273; %Tbuffer init.,x7 int.
%Tbuffervector = [90 90 90 90 80 80 70 90 90 90 90 70 65]+273;%Tbuffer init.,x13 int.


%% Case optimal 

%Always expensive energy, Switch goes to T_buffer
Pricethresholdvector = linspace(0.1,0.1,20);
Pthreshold = Pricethresholdvector(1);

%% Case 2

% Always cheap energy, Switch goes to Tmax
% Pricethresholdvector = linspace(1,1,20);
% Pthreshold = Pricethresholdvector(1);

%% 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,Pthreshold,start,days,price,time_excel),Tbuffervector,opts)
  
end




 