% Error analysis of simplified problem: integrate a to get v.
%
% Assumption: Error is random and uncorrelated from point to point, with mean 
% of zero and a given standard deviation.
%
% Motivation:
% How does error accumulate? Does time resolution matter?
% The effects of noise and signal are separable since this is a linear problem.
% Therefore, try integrating noise with no signal. 

tmax = 10;  % seconds
rms_err = 0.02; % m/s

for i=1:5
   dt(i) = 10^(-i);
   t = 0:dt(i):tmax;
   N(i) = numel(t)-1; % number of steps from 0 to tmax.
   for j=1:100  % cycle through some number of trials to get a standard deviation
      % a = normrnd(0, rms_err, size(t));
      a = rms_err * randn( size(t) );
      v = cumsum(a) .* dt(i);
      error(j) = v(end);
   end
   rms_error(i) = std(error);
end

figure(1)
hold off
loglog(N, rms_error,'^-b')
hold on
xlabel('Number of samples in a fixed time interval')
ylabel('Cumulative integration error')
print('../plots/error_trend.pdf','-dpdf')
% CLASS EXERCISE
% (1) Derive the trend in this plot.
% (2) How would error scale with tmax if we hold dt constant?