% Unit 1 (MATLAB Course @ UAM)
% numerical antiderivative of cos()
% (c) Alexander Knebe

%========================================================
% script to calculate int_a^b (f(x)dx)
%========================================================
clear all
close all
clc

a    = 0.1;
xend = 2*pi;
N    = 25;

a    = 30;
xend = 40;
N    = 100;

x = linspace(a,xend,N);
f = cos(x);


g = cumsum( ((f(1:N-1)+f(2:N))./2) .* diff(x) );

% Note: we are not plotting at the mid-points as cumsum() gives the
%       cumulative sum up to the right end of each interval; hence we
%       also use that right end of the interval for plotting
plot(x(2:N),g,'ro')
hold on
plot(x,(sin(x)-sin(a)),'b') % ...the integration constant is "-sin(a)"