Sunteți pe pagina 1din 5

Basic calculus in MATLAB

3.5 Exercises 2

This set of exercises is designed to give you familiarity with numerical integration.
Hints and solutions are available.

Hint based problem 3

4
Evaluate ∫ x dx using:
−1

(a) the Trapezium rule with 10 equal-sized intervals,

(b) Simpson’s rule with 10 equal-sized intervals.

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

N=10; % Number of intervals


h=2/N; % The size of each interval
x=linspace(-1,1,N+1);
y=x.*x.*x.*x; % Defining the function

% Firstly we apply the Trapezium rule:

TrapeziumIntegral=0.5*y(1); % Initialising 0.5*y0

% We now loop over the elements y1…yN


for i=2:N
TrapeziumIntegral=TrapeziumIntegral+y(i);
end

% Lastly, we need to add 0.5*yN (which corresponds to the (N+1)th


% entry of the vector y) and multiply by the interval size, h

TrapeziumIntegral=TrapeziumIntegral+0.5*y(N+1);
TrapeziumIntegral=TrapeziumIntegral*h

% Now we apply Simpson’s rule:

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

% Lastly, we add the last value and multiply by h/3


SimpsonIntegral=SimpsonIntegral+y(N+1);
SimpsonIntegral=SimpsonIntegral*h/3

Download code as NumericalIntegration1.m [link: NumericalIntegration1.m ] file

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

% Now look for required N for Trapezium rule

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

% Now we apply the Trapezium rule:


TrapeziumIntegral=0.5*y(1); % Initialising 0.5*y0

% We now loop over the elements y1…yN


for i=2:Ntrap
TrapeziumIntegral=TrapeziumIntegral+y(i);
end

% Lastly, we need to add 0.5*yN (which corresponds to the (N+1)th


% entry of the vector y) and multiply by the interval size, h

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

% Now look for required N for Simpson’s rule

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

% Now we apply Simpson’s rule:


SimpsonIntegral=y(1); % Initialise

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

% Lastly, we add the last value and multiply by h/3


SimpsonIntegral=SimpsonIntegral+y(Nsimp+1);
SimpsonIntegral=SimpsonIntegral*h/3;

SimpsonError = abs(SimpsonIntegral-ExactIntegral);
end

Nsimp
SimpsonError

Download code as NumericalIntegration1Error.m [link: NumericalIntegration1Error.m ] file

Hint based problem 4

−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

% Now we apply the Trapezium rule:


TrapeziumIntegral=0.5*y(1); % Initialising 0.5*y0

% We now loop over the elements y1…yN


for i=2:Ntrap
TrapeziumIntegral=TrapeziumIntegral+y(i);
end

% Lastly, we need to add 0.5*yN (which corresponds to the (N+1)th


% entry of the vector y) and multiply by the interval size, h

TrapeziumIntegral=TrapeziumIntegral+0.5*y(Ntrap+1);
TrapeziumIntegral=TrapeziumIntegral*h;

TrapeziumError = abs(TrapeziumIntegral-ExactIntegral);
end

Ntrap
TrapeziumError

% Now look for required N for Simpson’s rule

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

% Now we apply Simpson’s rule:


SimpsonIntegral=y(1); % Initialise

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

% Lastly, we add the last value and multiply by h/3


SimpsonIntegral=SimpsonIntegral+y(Nsimp+1);
SimpsonIntegral=SimpsonIntegral*h/3;

SimpsonError = abs(SimpsonIntegral-ExactIntegral);
end

Nsimp
SimpsonError

Download code as NumericalIntegration2Error.m [link: NumericalIntegration2Error.m ] file

(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

S-ar putea să vă placă și