You are on page 1of 4

PROBLEM 1:

Problem 1 involves using integration by parts to evaluate the definite integral. This enables one
to confirm the accuracy of the result produced by using the codes. As the first prerequisite for
Problem 1, the group had to estimate the integral using the Trapezoidal rule in (b) with 10 as
the value of N as well as in Simpson’s 1/3 rule (c) and in Adaptive Quadrature via Trapezoidal
rule (d). The students did this while following the guidelines presented in the presentation. The
final stage of the formula, the trapezoidal rule, allowed the students to arrive at the solution of
100.67995 (b), 98.45669 (c) in Simpson’s 1/3 rule, and 98.42770 (d) in Adaptive Quadrature via
Trapezoidal rule.

A.)
3
= [(𝑥2 − 2𝑥 + 2)𝑒𝑥] |
0
= (32 − 2(3) + 2)𝑒3 − (02 − 2(0) + 2)𝑒0
= 100.4276846 − 2
= 𝟗𝟖. 𝟒𝟐𝟕𝟔𝟖𝟒𝟔𝟐

B.)
f = @(x) exp(x).*x.^2; % Declare the function
a = 0; b = 3; % Limits of integration
N = 10; h = (b-a)/N; % # of pts and step size
x = linspace(a,b,N+1); % Create vector x

% Trapezoidal Rule
ii = 1:N;
S = 0.5*h*sum(f(x(ii)) + f(x(ii+1)));
fprintf('Area: %.5f (Trapezoidal Rule)\n',S);

Area: 100.67995 (Trapezoidal Rule)


f = @(x) exp(x).*x.^2; % Declare the function
a = 0; b = 3; % Limits of integration
N = 10; h = (b-a)/N; % # of pts and step size
x = linspace(a,b,N+1); % Create vector x

% Simpson's 1/3 Rule


ii = 1:N/2;
S = h/3*sum(f(x(2*ii-1)) + 4*f(x(2*ii)) + f(x(2*ii+1)));
fprintf('Area: %.5f (Simpson''s 1/3 Rule)\n',S);

Area: 98.45669 (Simpson's 1/3 Rule)

D.)

function adaptivequadrature

f = @(x) exp(x).*x.^2;
a = 0; b = 3; tol = 1e-8;
fprintf('Area: %.5f\n',AdaptQuad(f,a,b,tol));

function S = AdaptQuad(f,a,b,tol)
S = 0.5*(b-a)*(f(a)+f(b)); % Area from a to b
m = 0.5*(a+b); % Midpoint of a and b
S2 = 0.5*(m-a)*(f(a)+f(m))+... % (Area from a to m) +
0.5*(b-m)*(f(m)+f(b)); % (Area from m to b)
if abs(S2 - S) > tol % If S and S2 are dissimilar
S = AdaptQuad(f,a,m,tol)+... % Do AdaptQuad from a to m
AdaptQuad(f,m,b,tol); % Do AdaptQuad from m to b
end
end
end

Area: 98.42770
Problem 2:
The group integrates the equation after solving problem 2-A analytically to arrive at the result
of 9. The following problem requires adaptive quadrature with a tolerance level of 10e-8 in
order to acquire the area covered. The region is encoded with the value 8.9999. In order to
identify the area in the final problem, the group must also apply the midway rule wherein we
had to estimate the equivalent of 500 in N. The codes were executed, giving 9.0000.

A.)

(6𝑥 − 2𝑥2)𝑑𝑥

=6 𝑥𝑑𝑥 − 2 𝑥2 𝑑𝑥
𝑥2
=
2
𝑥3
=
3
= 6 𝑥 𝑑𝑥 − 2 𝑥2 𝑑𝑥
2𝑥3
= 3𝑥 − 2
3
2𝑥3
= 3𝑥 −2 +𝐶
3
2(3)3 2
2(0)3
= [ 3𝑥 −2 − 3(0) −
3 3
=9−0
=𝟗
function adaptivequadrature
f = @(x) -2*x.^2+6*x;
a = 0; b = 3; tol = 1e-8;
fprintf('Area: %.5f\n',AdaptQuad(f,a,b,tol));

function S = AdaptQuad(f,a,b,tol)
S = 0.5*(b-a)*(f(a)+f(b)); % Area from a to b
m = 0.5*(a+b); % Midpoint of a and b
S2 = 0.5*(m-a)*(f(a)+f(m))+... % (Area from a to m) +
0.5*(b-m)*(f(m)+f(b)); % (Area from m to b)
if abs(S2 - S) > tol % If S and S2 are dissimilar
S = AdaptQuad(f,a,m,tol)+... % Do AdaptQuad from a to m
AdaptQuad(f,m,b,tol); % Do AdaptQuad from m to b
end
end
end

Area: 8.99999

C.)

f = @(x) -2*x.^2+6*x;
a = 0; b = 3;
N = 500; h = (b-a)/N;
x = linspace(a,b,N+1);

% Midpoint Rule
ii = 1:N;
S = h*sum(f(0.5*(x(ii)+x(ii+1))));
fprintf('Area: %.4f (Midpoint Rule)\n',S);

Area: 9.0000 (Midpoint Rule)

You might also like