a) Approximate the integral I= integral evaluated from -1 to 1 of f(x) dx
with f(x) = 1/(1+x^2) by the composite trapezoidal, midpoint and Simpson's rule using subintervals with lengths h = 1/2, 1/4, 1/8, 1/16, 1/32, 1/64. To this end you may use the analytic result or a very accurate numerical result to compute the error. Plot the error for the different methods as a function of h. Comment on the order of accuracy and efficiency of the different methods.
b) Repeat with the function f(x) = the square root of the absolute value of (x-alpha) with alpha=pi/4 and alpha=0. Comment on the results.
c) Repeat with the function f(x) = e^sin*6*pi*x, comment.
d) Adaptive quadrature. Below is a naive implementation of an adaptive version of forward Euler quadrature. The program checks the if the tolerance TOL is met and recursively performs divide and conquer if it is not. Modify the program so that it uses the trapezoidal rule instead. You can plot the
errors as a function of the effective h = 2/(# fun. evals).
a = -1;
b = 1;
TOL = 1e-2;
I = adFwdE(@myfun,a,b,TOL);
function I = adFwdE(myfun,a,b,TOL);
h = b-a;
m = 0.5*(b+a);
fa = myfun(a);
fm = myfun(m);
I = h*fa;
Ie = 0.5*h*(fa+fm);
if (abs(Ie-I) < TOL)
I = Ie; % use the better value
I = adFwdE(myfun,a,m,TOL/2) + adFwdE(myfun,m,b,TOL/2);
The solutions are in a zip file.
I encourage you to look ...
The expert examines intergral evaluated forms for quadratures (MATLAB).