clear all
load('datalog2.mat')

% extract only the time where the door is closed (throw away rest of data
% since door control is not an input to the system)
door_open_idx = find(dooropen,1);

fit_time = time(1:door_open_idx);
fit_temp = temp(1:door_open_idx);
fit_heateron = heateron(1:door_open_idx);
plot(fit_time, fit_temp)
title('Input Data')
ylabel('Temp [DegC]')
xlabel('Time [sec]')
grid on

sample_time = mean(diff(fit_time));

% fit a TF to the data if SysIDToolbox is available, otherwise use the fit
% determined by someone who does have the toolbox
if(license('test','Identification_Toolbox'))
    data = iddata(fit_temp,[fit_heateron],sample_time,'OutputUnit','DegC');
    sys = tfest(data,2)
else
    % prefix 
    sys = tf([-0.5454 0.02677],[1 0.0266 2.849e-05]);
end

% simulate the system using the original input data
sim_time = 0:0.5:(door_open_idx-1)/2;
sim_temp = lsim(sys,heateron_test,sim_time);

% plot the sim run
figure
plot(sim_time,sim_temp)
title('Simulated System')
ylabel('Temp [DegC]')
xlabel('Time [sec]')
grid on

