%% Compare the integrals of an approximating function and the standard one
%  This script requires ExT6_2point3 to be run prior

%% Define a vector of x-values and evaluate the polynomial s at these values 
x = [min(x_given):.001:max(x_given)];
y = eval(s);

%% Compute the integral of the polynomial numerically
Integral_Approx = trapz(x,y) 

%% Compute the integral of the trig function numerically
% For T6
Integral_Exact = trapz(x,sin( (pi*x.^2)/2 ))
% For T7
%Integral_Exact = trapz(x,exp(x.^2))

%% Compute the difference between the two integrals
Defect = abs(Integral_Approx - Integral_Exact)

%% Plot the two curves and the area under the curve
figure; hold on; 
title('Approximating Trig Integrals via Polynomials')

%% Define a new set of x-values to plot which go beyond the limits of integration
x = [x x(end):.001:1.25];
y = eval(s);

%% Plotting
% Plot the poly in green 
plot(x,y,'og','MarkerSize',10)
pause
% Plot the sine curve in black
plot(x,sin( (pi*x.^2)/2 ),'.k','MarkerSize',10)
% For T7
%plot(x,exp(x.^2));


%% Fancy tricks to plot the area under the curve
basey = min(0,min(y));
x1 = find(x==1);
h = fill([x(1:x1) 1 x(1)], [y(1:x1) basey basey ], 'r'); 

%% Create a legend
legend('Polynomial Approximation', 'Exact Function', 'Integral form 0 to 1')

