% 1. Load Data
data = dlmread('temp_data_01.csv', ';', 1, 0);
h_full = data(:, 1);
t_full = data(:, 2);

% 2. Window Configuration
start_idx  = 1;   
num_points = 35;    
end_idx = min(start_idx + num_points - 1, length(h_full));

h_vis = h_full(start_idx:end_idx);
t_vis = t_full(start_idx:end_idx);

% Shared Parameters
total_interval = h_vis(end) - h_vis(1);
h_dense = linspace(h_vis(1), h_vis(end), 200);
y_lims = [min(t_vis)-1, max(t_vis)+1]; % Keeps Y-axis identical for comparison

figure('Position', [100, 100, 1200, 400]); % Wider window for horizontal layout

% --- Subplot 1: Riemann Left ---
subplot(1,3,1); hold on; grid on;
dt = diff(h_vis);
avg_riemann = sum(t_vis(1:end-1) .* dt) / total_interval;

for i = 1:length(h_vis)-1
    rx = [h_vis(i), h_vis(i+1), h_vis(i+1), h_vis(i)];
    ry = [0, 0, t_vis(i), t_vis(i)];
    fill(rx, ry, 'r', 'FaceAlpha', 0.1, 'EdgeColor', 'none');
end
stairs(h_vis, t_vis, 'r', 'LineWidth', 1.5);
plot(h_vis, t_vis, 'ko', 'MarkerFaceColor', 'k');
line([h_vis(1) h_vis(end)], [avg_riemann avg_riemann], 'Color', 'r', 'LineWidth', 2);
title(sprintf('Riemann (Left)\nAvg: %.3f', avg_riemann));
ylim(y_lims); ylabel('Temp (C)');

% --- Subplot 2: Trapezoidal ---
subplot(1,3,2); hold on; grid on;
avg_trap = trapz(h_vis, t_vis) / total_interval;

for i = 1:length(h_vis)-1
    tx = [h_vis(i), h_vis(i+1), h_vis(i+1), h_vis(i)];
    ty = [0, 0, t_vis(i+1), t_vis(i)];
    fill(tx, ty, 'b', 'FaceAlpha', 0.1, 'EdgeColor', 'none');
end
plot(h_vis, t_vis, 'b-o', 'LineWidth', 1.5, 'MarkerFaceColor', 'k');
line([h_vis(1) h_vis(end)], [avg_trap avg_trap], 'Color', 'b', 'LineWidth', 2);
title(sprintf('Trapezoidal (Linear)\nAvg: %.3f', avg_trap));
ylim(y_lims); xlabel('Hour of Day');

% --- Subplot 3: Cubic Spline ---
subplot(1,3,3); hold on; grid on;
t_spline_dense = interp1(h_vis, t_vis, h_dense, 'spline');
spline_func = @(t) interp1(h_vis, t_vis, t, 'spline');
avg_spline = integral(spline_func, h_vis(1), h_vis(end)) / total_interval;

area(h_dense, t_spline_dense, 'FaceColor', 'g', 'FaceAlpha', 0.1, 'EdgeColor', 'none');
plot(h_dense, t_spline_dense, 'g', 'LineWidth', 2);
plot(h_vis, t_vis, 'ko', 'MarkerFaceColor', 'k');
line([h_vis(1) h_vis(end)], [avg_spline avg_spline], 'Color', [0 0.5 0], 'LineWidth', 2);
title(sprintf('Cubic Spline (Smooth)\nAvg: %.3f', avg_spline));
ylim(y_lims);