%File  Tbuffer.m
% Finding the cost for all Tbuffers and Pthresholds
clear all 
close all

Tbuffervector = linspace(60,90,20)+273;

Pricethresholdvector = linspace(0.24,0.32,20);


%% 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=[];

for k=start:(start+days-1)
for j=1:length(Pricethresholdvector)

for i=1:length(Tbuffervector)
    
   
   Pthresholdj = Pricethresholdvector(j) 
   Tbufferi=Tbuffervector(i);
 
   Jkji=objective_generall(Tbufferi,Pthresholdj,start,days,price,time_excel);
   
   Jmatrix(k,j,i)=Jkji;
   
end
end
end

%%

%% Results saved
% Change day for every simulation

result.Tbuffer = Tbuffervector;
result.Pthreshold = Pricethresholdvector;
result.Jmatrix = Jmatrix(k,:,:);

%save('res_demand_31.mat','result')
 
 
%% Plot surf

surf(Pricethresholdvector,Tbuffervector-273,squeeze(Jmatrix(1,:,:)))
title('$Total$ $Cost$ $[NOK]$','interpreter','latex','FontSize',14)
xlabel('$Price$ $Threshold$, $[NOK/kWh]$','interpreter','latex','FontSize',14)
ylabel('$T_{buffer}$, $[{\circ}C]$','interpreter','latex','FontSize',14)
zlabel('$Total$ $cost$, $[NOK]$','interpreter','latex','FontSize',14)
axis tight










