clear;

%% Data

rng(2022);

final_times = [1, 2, 3];
R = 4;
lambdas = (0:R)/R;
dt = 2^(-8);
nTrajectories = 1000;

%% Riemann sum

L = nan(length(final_times), length(lambdas));
for i = 1:length(final_times)
    T = final_times(i);
    nSteps = R*(T/dt);
    W = BM(T, nSteps, nTrajectories);
    for j = 1:length(lambdas)       
        r = lambdas(j)*R;
        L(i,j) = mean(sum(W(:,1+r:R:end-(R-r)).*(W(:,1+R:R:end) - W(:,1:R:end-R)), 2) - W(:,end).^2/2);
    end
end

%% Figure

figure;
for i = 1:length(final_times)
    plot(lambdas, L(i,:), 'o-', 'Linewidth', 2, 'DisplayName',['t = ', num2str(final_times(i))]);
    hold on;
end
xlabel('\lambda', 'Fontsize', 20);
set(gca, 'FontSize', 20);
legend('Location', 'best');
saveas(gcf, 'figures/exercise5.png');