data = dlmread('temp_data_01.csv', ';', 1, 0);
hours = data(:, 1);
temps = data(:, 2);

% Define integration parameters
T_total = hours(end) - hours(1); % Interval length (24)
dt = diff(hours);                % Time steps between points (all 1 here)

% --- 1. Riemann Sum (Left-Hand Sum) ---
% Treats the temperature as constant over each hour interval.
riemann_sum = sum(temps(1:end-1) .* dt);
avg_riemann = riemann_sum / T_total;

% --- 2. Trapezoidal Rule (Manual Calculation) ---
% Connects points with straight lines, creating trapezoids (more accurate).
trap_manual = sum((temps(1:end-1) + temps(2:end)) / 2 .* dt);
avg_trap_manual = trap_manual / T_total;

% --- 3. Built-in Commands (trapz and integral) ---
% Octave's primary tool for discrete data is 'trapz'.
avg_built_in_trapz = trapz(hours, temps) / T_total;

% Alternative: Using 'integral' with interpolation
% (Creates a continuous function handle from your discrete points)
temp_func = @(t) interp1(hours, temps, t, 'linear');
avg_built_in_integral = integral(temp_func, 0, 24) / T_total;

% --- Comparison Output ---
fprintf('--- Numerical Integration Results ---\n');
fprintf('1. Riemann Sum (Left):     %.4f °C\n', avg_riemann);
fprintf('2. Trapezoidal (Manual):   %.4f °C\n', avg_trap_manual);
fprintf('3. Built-in (trapz):       %.4f °C\n', avg_built_in_trapz);
fprintf('4. Built-in (integral):    %.4f °C\n', avg_built_in_integral);
fprintf('------------------------------------\n');