Documente Academic
Documente Profesional
Documente Cultură
3.5 Exercises 2
This set of exercises is designed to give you familiarity with numerical integration.
Hints and solutions are available.
4
Evaluate ∫ x dx using:
−1
In each case, how many uniformly sized intervals do you need for the absolute error in the answer to be less than
0.0001?
Solution reveal
The following code calculates the Trapezium rule and Simpson’s rule approximations for N = 10 .
clear all
TrapeziumIntegral=TrapeziumIntegral+0.5*y(N+1);
TrapeziumIntegral=TrapeziumIntegral*h
SimpsonIntegral=y(1); % Initialise
for i=1:N/2
SimpsonIntegral=SimpsonIntegral+4*y(2*i);
http://www.dtc.ox.ac.uk/online-learning/matlab/BasicCalculusInMATLAB/Exercises2.xhtml 1/5
4/3/2018
end
for i=1:N/2-1
SimpsonIntegral=SimpsonIntegral+2*y(2*i+1);
end
1
5
x
The true answer is [ 5
] = 0.4 .
−1
To get the answer within 0.0001 of the true value, we require N = 164 for the Trapezium rule and
N = 16 for Simpson’s rule. MATLAB code to calculate the N required to get the integral to the
required accuracy is given below.
clear all
format long
TargetError=0.0001;
TrapeziumError=1; % initialise error
Ntrap=10; % initial Ntrap
ExactIntegral = 0.4;
while (TrapeziumError>TargetError)
Ntrap=Ntrap+1; % Number of intervals
h=2/Ntrap; % The size of each interval
x=linspace(-1,1,Ntrap+1);
y=x.*x.*x.*x; % Defining the function
TrapeziumIntegral=TrapeziumIntegral+0.5*y(Ntrap+1);
TrapeziumIntegral=TrapeziumIntegral*h;
TrapeziumError = abs(TrapeziumIntegral-ExactIntegral);
end
http://www.dtc.ox.ac.uk/online-learning/matlab/BasicCalculusInMATLAB/Exercises2.xhtml 2/5
4/3/2018 3.5 Exercises 2
Ntrap
TrapeziumError
TargetError=0.0001;
SimpsonError=1; % initialise error
Nsimp=10; % initial Nsimp
ExactIntegral = 0.4;
while (SimpsonError>TargetError)
Nsimp=Nsimp+2; % Number of intervals so even
h=2/Nsimp; % The size of each interval
x=linspace(-1,1,Nsimp+1);
y=x.*x.*x.*x; % Defining the function
for i=1:Nsimp/2
SimpsonIntegral=SimpsonIntegral+4*y(2*i);
end
for i=1:Nsimp/2-1
SimpsonIntegral=SimpsonIntegral+2*y(2*i+1);
end
SimpsonError = abs(SimpsonIntegral-ExactIntegral);
end
Nsimp
SimpsonError
−1000x
(a) Repeat Problem 3 for ∫ (e + x) dx .
0
(b) Can you suggest a more sensible way to choose the discrete points instead of using uniformly spaced points?
Solution reveal
http://www.dtc.ox.ac.uk/online-learning/matlab/BasicCalculusInMATLAB/Exercises2.xhtml 3/5
4/3/2018 3.5 Exercises 2
−1000x 1 −1000
2
−e x 1−e 1
(a) The true answer is [ 1000
+
2
] =
1000
+
2
≈ 0.501. .
0
To get an answer within 0.0001 of true value, we require N = 904 for the Trapezium rule and
N = 426 for Simpson’s rule. The MATLAB code for Problem 3 can be modified for this question
and is given below.
clear all
format long
% Now look for required N for Trapezium rule
TargetError=0.0001;
TrapeziumError=1; % initialise error
Ntrap=10; % initial Ntrap
ExactIntegral = (1-exp(-1000))/1000+1/2;
while (TrapeziumError>TargetError)
Ntrap=Ntrap+1; % Number of intervals
h=1/Ntrap; % The size of each interval
x=linspace(0,1,Ntrap+1);
y=exp(-1000*x)+x; % Defining the function
TrapeziumIntegral=TrapeziumIntegral+0.5*y(Ntrap+1);
TrapeziumIntegral=TrapeziumIntegral*h;
TrapeziumError = abs(TrapeziumIntegral-ExactIntegral);
end
Ntrap
TrapeziumError
TargetError=0.0001;
SimpsonError=1; % initialise error
Nsimp=10; % initial Nsimp
ExactIntegral = (1-exp(-1000))/1000+1/2;
while (SimpsonError>TargetError)
Nsimp=Nsimp+2; % Number of intervals so even
h=1/Nsimp; % The size of each interval
http://www.dtc.ox.ac.uk/online-learning/matlab/BasicCalculusInMATLAB/Exercises2.xhtml 4/5
4/3/2018 3.5 Exercises 2
x=linspace(0,1,Nsimp+1);
y=exp(-1000*x)+x; % Defining the function
for i=1:Nsimp/2
SimpsonIntegral=SimpsonIntegral+4*y(2*i);
end
for i=1:Nsimp/2-1
SimpsonIntegral=SimpsonIntegral+2*y(2*i+1);
end
SimpsonError = abs(SimpsonIntegral-ExactIntegral);
end
Nsimp
SimpsonError
(b) In order to improve accuracy, a non-uniform distribution of xi ’s with a higher density close to the
origin could be used.
http://www.dtc.ox.ac.uk/online-learning/matlab/BasicCalculusInMATLAB/Exercises2.xhtml 5/5