% Numerical Integration (Quadrature)
% The Matlab function integral performs numerical integration using global adaptive quadrature and default error tolerances. To carry this out for
% f\left(x\right)=e^{-x} \;\sin \left(x\right)
% you simply create a function as follows:

a=0;
b=1;
res=integral(@f,a,b)

%
% to integrate f from 0 to 1. Since we know the solution to this problem, we can calculate the relative error here as follows:

exact=0.5*(exp(-a)*(sin(a)+cos(a))-exp(-b)*(sin(b)+cos(b)));
relerror=(res-exact)/exact

% We can control the tolerances as follows

res = integral(@f,a,b,'AbsTol',1e-2,'RelTol',1e-2)
relerror=(res-exact)/exact 

%
% The function definition is here:

function func=f(x)
func=exp(-x).*sin(x);
end

% © James Blanchard, University of Wisconsin-Madison, blanchard@engr.wisc.edu