function I = intCosX2(L)

horzLine = '_____________________';
fprintf('\n\n       Homework #3, Problem 3 ');
fprintf('\n\n Integral of cos(x^2) from 0 to %g', L);
fprintf('\n\n I(%g) = Sum for n=0..m of (-1)^n %g^(4n+1)/(4n+1)/(2n)! \n\n',L,L);
fprintf('   m  |     I(%g)    \n%s\n', L, horzLine);

tolerance = 1e-5;
I = 0;
for n=0:1000
    an = (-1)^n * L^(4*n+1) / factorial(2*n) / (4*n + 1);
    I = I + an;
    if  abs(an) < tolerance  break
    end;
    fprintf('  %2d  | %13.7g \n', n, I);
end;

fprintf('%s \n\n', horzLine);
x=sym('x'); 
exact = quad('cos(x.^2)',0,L);  % "quad" is the Matlab's integrator ("quad"
                                % stands for "quadrature", which means integral)
fprintf(' Compare with exact result: %.8g \n', exact);