Sie sind auf Seite 1von 21

Slides

Module 2.1: Numerical Integration - Part 1


Trapezoidal and Simpsons Rule

A NumFys Module
http://www.numfys.net

Fall 2012

A NumFys Module
Module 2.1: Numerical Integration - Part 1

http://www.numfys.net

Slides

p.1

Matlab Module

First Step:
Save the corresponding Matlab code onto your computer and
open it in Matlab.

A NumFys Module
Module 2.1: Numerical Integration - Part 1

http://www.numfys.net

Slides

p.2

Numerical Integration - Part 1


The Task:
How to determine a definite integral like
Z b
f (x) dx,

(1)

if we cannot solve it analytically, i.e. if we cannot find a function


F (x) with
d
F (x) = f (x)
(2)
dx
and
Z b
f (x) dx = F (b) F (a).
(3)
a
A NumFys Module
Module 2.1: Numerical Integration - Part 1

http://www.numfys.net

Slides

p.3

Numerical Integration - Part 1


Example:
We cannot determine the integral
Z 1p
x 5 + e5x dx

(4)

analytically. So what to do?

Lets plot
the integrand
f (x) = x 5 + e5x for
x [0, 1].

A NumFys Module
Module 2.1: Numerical Integration - Part 1

http://www.numfys.net

Slides

p.4

Numerical Integration - Part 1


We know that the integral represents, in this case, the area
under the graph. The area is well defined and finite.
We could approximate it by a Riemann sum. For this purpose,
we divide the interval [0, 1] into N 1 intervals of the same
length h with
1
.
(5)
h=
N 1
The endpoints of these intervals are located at
xn = (n 1)h with n = 1, 2, 3, ..., N.

(6)

This leads naturally to the definition of a rectangle for each


interval [xn , xn+1 ] whose height is determined by the value of
the function at xn , i.e. f (xn ).
A NumFys Module
Module 2.1: Numerical Integration - Part 1

http://www.numfys.net

Slides

p.5

Numerical Integration - Part 1


For N = 6, we can sketch this in our previous plot.

We can see that we make a fairly large error in our estimate of


the integral when we use these rectangles. How can we do
better?
A NumFys Module
Module 2.1: Numerical Integration - Part 1

http://www.numfys.net

Slides

p.6

Numerical Integration - Part 1


One way to go forward is to make the intervals smaller. In the
limit when their length goes to zero, we should find the exact
value of the integral.
However, this is impractical on the computer since it involves
infinitely many calculations. What else can we do?
Well, a better approximation can already be obtained with our
last set of intervals if we use trapezoids instead. Two sides (top
and bottom) of each trapezoid are determined by the interval
along the x-axis, while the other two sides (left and right) are
determined by the values of the function f (x) at each end of the
interval.
Lets see what this looks like in the plot.

A NumFys Module
Module 2.1: Numerical Integration - Part 1

http://www.numfys.net

Slides

p.7

Numerical Integration - Part 1

It seems that we are making a much smaller error than before,


given the same set of intervals. The error seems to increase
when the curvature of the graph increases (towards the right in
this plot).
A NumFys Module
Module 2.1: Numerical Integration - Part 1

http://www.numfys.net

Slides

p.8

Numerical Integration - Part 1


Let us derive a formula for the area that the trapezoids cover.
Using our notation above, the area of a trapezoid is given by
fn + fn+1
with fn = f (xn ).
(7)
An = h
2
Here, h is the length of each interval, and hence the length of
the base of each trapezoid, and the expression
fn + fn+1
(8)
2
represents the "average height" of the corresponding trapezoid.

A NumFys Module
Module 2.1: Numerical Integration - Part 1

http://www.numfys.net

Slides

p.9

Numerical Integration - Part 1


Adding up all the trapezoids, we obtain the total area
A = A1 + A2 + A3 + ... + AN1

(9)

and so
f2 + f3
f3 + f4
fN1 + fN
f1 + f2
+h
+h
+ ...h
.
2
2
2
2
Factoring out h and rearranging terms gives


1
1
f1 + f2 + f3 + ... + fN1 + fN .
A=h
2
2
A=h

(10)

(11)

This formula describes the trapezoidal rule and approximates


the original integral.
A NumFys Module
Module 2.1: Numerical Integration - Part 1

http://www.numfys.net

Slides

p.10

Numerical Integration - Part 1


The factors inside the bracket are 0.5, 1, 1, ..., 1, 0.5. This stems
from the fact that we only use the endpoints 0 and 1 once in our
calculation while all other points are involved twice in the
determination of trapezoidal areas.
Note that we do not need to assume an interval [0, 1]. Instead,
we can use this method for any interval [a, b]. The definitions
then change to
ba
and xn = a + (n 1)h.
(12)
h=
N 1
This way, we can approximate the integral by


Z b
1
1
f (x) dx h
f1 + f2 + f3 + ... + fN1 + fN .
(13)
2
2
a

A NumFys Module
Module 2.1: Numerical Integration - Part 1

http://www.numfys.net

Slides

p.11

Numerical Integration - Part 1


It can be shown that this method contains an error (i.e. actual
value of the definite integral vs. numerical value) that is
proportional to
1
2
3

(b a)3 ,
1
and
N2
the (maximum) magnitude of the second derivative of f (x)
over the respective interval.

Hence, doubling the amount of intervals reduces the error by a


factor of four! However, choosing very large N is not a good
idea for two reasons:
i) It increases the computational time.
ii) The computer has finite precision; rounding errors become
important!
A NumFys Module
Module 2.1: Numerical Integration - Part 1

http://www.numfys.net

Slides

p.12

Numerical Integration - Part 1


The second derivative is linked to the curvature of the graph
and we saw above that the error indeed increased where the
curvature was larger. This makes sense since we are trying to
approximate a curved graph with a straight line.
In the enclosed Matlab script, we approximate the integral
Z 1p
x 5 + e5x dx
(14)
0

for various N. As we increase N, the values for the trapezoidal


rule converge.
Try it yourself!
Choose N = 5, 10, 20, 50, 100, 250, 500.
A NumFys Module
Module 2.1: Numerical Integration - Part 1

http://www.numfys.net

Slides

p.13

Numerical Integration - Part 1


Question:
Is there a better, i.e. more precise, method to approximate an
integral? Can we do just as well with fewer intervals?
Answer:
Yes, there are several methods.
One method is called Simpsons rule. The idea behind this
method is that we do not approximate the graph locally by a
straight line, as is the case for the trapezoidal method, but
rather by a polynomial of order 2. This means that we
approximate the function f (x) in each subinterval by
f (x) cn x 2 + dn x + en =: gn (x).

(15)

The parameters cn , dn , en are chosen so that f (x) and gn (x)


coincide at the endpoints of the interval and at its midpoint.
A NumFys Module
Module 2.1: Numerical Integration - Part 1

http://www.numfys.net

Slides

p.14

Numerical Integration - Part 1


Example:
Let us plot our original function and approximate it by a
second-order polynomial with the above characteristics.

We see that the polynomial describes the original function


already quite well. And we have not even started to divide the
interval [0, 1] into subintervals...
A NumFys Module
Module 2.1: Numerical Integration - Part 1

http://www.numfys.net

Slides

p.15

Numerical Integration - Part 1


It is important to note that Simpsons rule requires and odd
number of points (N = 3, 5, 7, ...) along the x-axis since we
need to use three points for each interval, i.e. left endpoint,
right endpoint and midpoint.
At the same time, two adjacent intervals share one endpoint,
i.e. the right endpoint of one interval coincides with the left
endpoint of another interval (except at x = N).
All along, we still maintain a uniform division of the original
interval [a, b] into smaller subintervals.

A NumFys Module
Module 2.1: Numerical Integration - Part 1

http://www.numfys.net

Slides

p.16

Numerical Integration - Part 1


The simplest case is using three points for the original interval,
representing one interval with one midpoint:
x1 = a, x2 = (a + b)/2, x3 = b.
(16)
This case is exhibited in the previous plot. In this case, it can be
shown thatZ the integral approximation reduces to
b
ba
[f (x1 ) + 4f (x2 ) + f (x3 )] .
(17)
f (x) dx
6
a
The next case uses five points: two intervals with one midpoint
each. Now, we obtain
Z b
h
f (x) dx [f (x1 ) + 4f (x2 ) + 2f (x3 ) + 4f (x4 ) + f (x5 )] (18)
3
a
where h is the distance between two successive points along
the x-axis. This scenario is plotted on the next slide.

A NumFys Module
Module 2.1: Numerical Integration - Part 1

http://www.numfys.net

Slides

p.17

Numerical Integration - Part 1

The case of two intervals: [0, 0.5] and [0.5, 1]


We have a total of 5 points along the x-axis, namely 3 interval
endpoints (x1 = 0, x3 = 0.5, x5 = 1) and 2 midpoints
(x2 = 0.25, x4 = 0.75). Simpsons rule employs second-order
polynomials in each interval.
A NumFys Module
Module 2.1: Numerical Integration - Part 1

http://www.numfys.net

Slides

p.18

Numerical Integration - Part 1


The general expression for an arbitrary amount of intervals is
Z

f (x) dx
a

h
[f (x1 ) + 4f (x2 ) + 2f (x3 ) + 4f (x4 ) + ... + 4f (xN1 ) + f (xN )]
3
(19)

or, using our previous notation above,


Z

f (x) dx
a

h
[f1 + 4f2 + 2f3 + 4f4 + ... + 4fN1 + fN ] .
3

(20)

Let us use Matlab again to compute the same integral as for the
trapezoidal rule, only this time we use Simpsons rule.
Simply uncomment the corresponding lines in the enclosed
Matlab script!
A NumFys Module
Module 2.1: Numerical Integration - Part 1

http://www.numfys.net

Slides

p.19

Numerical Integration - Part 1

Try it yourself!
Choose N = 3, 5, 7, 9, 11, 13, 15.
Does it converge faster than before?

A NumFys Module
Module 2.1: Numerical Integration - Part 1

http://www.numfys.net

Slides

p.20

Numerical Integration - Part 1


In contrast to the trapezoidal rule, Simpsons rule contains an
error that is proportional to
1

(b a)5 ,

1
N4

and

the (maximum) magnitude of the fourth derivative of f (x)


over the respective interval.

In other words, it is superior in accuracy to the trapezoidal rule


with respect to the amount of intervals N.

A NumFys Module
Module 2.1: Numerical Integration - Part 1

http://www.numfys.net

Das könnte Ihnen auch gefallen