## Transcribed Text

Numerical Integration
1. Composite Gaussian quadrature.
(a) Write a matlab function to implement the composite two-point Gaussian quadrature. The input arguments should include function handle for the integrand
f(x), interval [a, b], and number of subintervals n (or grid spacing h = (b − a)/n
instead). On each subinterval, a two-point Gaussian quadrature rule is used.
Attach your code to the project report.
(b) Code validation. Use your code to compute the integral
∫ 2
1
1
x
2
dx
and generate data to complete the following table. Here I is the numerical
integral, E = |I − If | where If is the exact integral, and α is the numerical
order of accuracy that can be computed in the same way as problem 2 in project
1. What is the numerical order that you observe? Is it consistent with the
theoretical value?
n h I E α
2 1/2 —
4 1/4
8 1/8
16 1/16
32 1/32
(c) How many function evaluations are required in the composite two-point Gaussian quadrature for n subintervals? How many function evaluations are required
if the Simpson’s rule is applied to each of these subintervals? (In the composite
Simpson’s rule, each of these subintervals has 3 nodes with two end nodes shared
with neighbours.) If you need to approximate ∫ b
a
f(x)dx, where f(x) is discontinuous at x =
a+b
2
, which composite rule (two-point Gaussian or Simpson’s) will
you choose? Why?
(d) The two-point Gaussian quadrature rule can be made adaptive in a way similar to
the adaptive Simpson’s rule. For smooth integrands, which adaptive quadrature
(two-point Gaussian or Simpson’s) is more efficient in terms of the number of
function evaluations? Explain.
2. Consider the following three numerical integration methods: composite two-point
Gaussian quadrature, adaptive Simpson’s rule, and Romberg integration. The code
for the latter two can be obtained from the course web page.
(a) Invent an example (i.e., an integrand f(x), an interval [a, b], and a small tolerance
tol) for which the Romberg integration requires at least five times the function
evaluations of the adaptive Simpson’s rule. For the Romberg integration, you
may try different n until Rn,n satisfies the accuracy requirement. Show the
results from your computations to support your conclusion and plot f(x) on [a, b].
Explain why the adaptive method is more efficient. How does the composite
Gaussian quadrature perform in this case?
1
(b) Invent an example for which the adaptive Simpson’s rule requires at least five
times the function evaluations of the Romberg integration. Show the results from
your computations to support your conclusion and plot f(x) on [a, b]. Explain
why the Romberg integration is more efficient. How does the composite Gaussian
quadrature perform in this case?
(c) Based on your discoveries in (a) and (b), comment on these methods.
2

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

2. function [R]=Romberg(f,a,b,n)

%ROMBERG - Romberg integration

% -Input:

% f - function name or function handle

% a,b - integration interval [a,b]

% n - size of the romberg table

% -Output:

% R - an nxn matrix of numerical integrals

% R(i,j): h=(b-a)/2^(i-1), order=2*j

%

%% Calculate Romberg integration

R=zeros(n,n);

h=b-a;

R(1,1)=h/2*(feval(f,a)+feval(f,b));

for i=2:n

m=2^(i-2);

h=(b-a)/m;

s=sum(feval(f,a+([1:m]-0.5)*h));%summation at new points

R(i,1)=0.5*(R(i-1,1)+h*s); %composite trapezoidal rule

for j=2:i

R(i,j)=R(i,j-1)+(R(i,j-1)-R(i-1,j-1))/(4^(j-1)-1); % extrapolation

end

end

%% Output R

for i=1:n

for j=1:i

fprintf(1,'R(%1d,%1d)=%11.4e ',i,j,R(i,j));

end

fprintf(1,'\n');

end

return;...