% Unit 1 (MATLAB Course @ UAM)
% definite integral of analytical function on given interval
% (c) Alexander Knebe

%================================
% integration of cosine function
%================================
clear all
clc

% boundary values for integral
a = 0.05;
b = 1.23*pi;

% use N sampling point
N = 20;

% fill x and y(x) as well as generate an index vector i()
x = linspace(a,b,N);
y = cos(x);


% the numerical solutions to the integral of y(x)
dx=diff(x);

Int1=y(1:N-1)                  .* dx;
Int2=y(2:N)                    .* dx;
Int3=(y(1:N-1)+y(2:N))./2      .* dx;

I_y_num1=sum(Int1)
I_y_num2=sum(Int2)
I_y_num3=sum(Int3)

% print the analytical solution
I_y_ana=sin(b)-sin(a)

% use trapz() and show it agree with Int3
I_y_trapz = trapz(x,y)
I_y_trapz-I_y_num3
