Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 28

Numerical Integration

(Calculating the area under a curve)

Integration is a pretty important part of engineering.


The total change in velocity during a time period t is
calculated by integrating acceleration with respect to
v2
t2
time. dv
v dv a( t ) dt
a( t )
dt
v1
t1
The total change in position is calculated by
s2
t2
integrating
velocity
with
respect
to time.
ds
s ds v( t ) dt
v( t )
dt
s1
t1
The total work done on an object is calculated by
integrating the
to
2
position.
force with respect
W1 2 F d r
dW F d r
1
2

Graphical interpretation of the differential equations:


t2

v2 v1 a dt
t1

The area under the a-t curve is


the net velocity change of the
particle.
t2
s2 s1 v dt
t1

The area under the v-t curve is


the net displacement of the
particle.
If we record the speed of our car frequently, we could
estimate the total distance traveled through
integration.
3

There are many functions that are impossible to


integrate analytically.
dy
3 x 2ln x sin(e x )

dx cos( x) tan(ln x)
y2

x 3

3 x 2 ln x sin(e x )
y dy
dx ??
cos( x) tan(ln x)
y1
x 1

Remember that an integral is the area under a curve.


f (x)

f ( x ) dx
a

We can calculate the area under a curve by breaking


the total area into chunks and adding the subareas.
This process is called numerical integration.
When we integrate analytically, we break the area
into an infinite number of infinitesimally small

The simplest method for numerical integration is


Riemann summation.
f (x)
f

In Riemann summation, we represent the area under


the curve as N rectangles with width x and height f
(x).
ba
x
N
6

The figure demonstrates left Riemann summation.


f

f (a x)

f (a )

f (x)

x
a
x
b
Overestimates the area when f (x) is decreasing.
Underestimates the area when f (x) is increasing.

f ( x) dx f (a ) x f (a x) x ... f (a (N 1) x) x
a

N 1

i 0

f ( x) dx x f (a i x)

We can perform right Riemann summation too.


f

f (a x)

f (a )

f (x)

x
a
x
b
Underestimates the area when f (x) is decreasing.
Overestimates the area when f (x) is increasing.
b

f ( x) dx f (a x) x f (a 2x) x ... f (a N x) x
a

i 1

f ( x) dx x f (a i x)

Middle Riemann summation usually provides a better


estimate of the area.
f (x)
f
f (a (1/2) x)
f (a (3/2) x)

f ( x) dx f (a (1/2)x) x f (a (3/2)x) x ... f (a (2 N 1)x/2) x


a

i 1

f ( x) dx x f (a (2i 1)x/2)
9

The error in the Riemann summation methods


decrease as the N increases.

10

Using rectangles to estimate the area under a curve


provides a pretty rough estimate of the area unless a
lot of rectangles are used.
This is because horizontal lines provide a very poor fit
to a curve (except where the curve is nearly
horizontal).
Sloped lines, parabolas, and higher degree
polynomials provide better fits to a curve.

11

In the Trapezoidal Rule method, the curve is


estimated as a diagonal line between the
f (x)
f
discretization points.
f (a x) f (a 2x)

f (a ) f (a x)

x
2

f (a x) f (a 2x)

x
2

f (a ( N 1)x) f (b )

x
2

f (a ( N 2)x) f (a ( N 1)x)

x
2

12

If we add up all the areas,

f (a ) f (a x)

x
2

f (a x) f (a 2x)

x
2

f (a 2x) f (a 3x)

x
2

...
f (a ( N 3)x) f (a ( N 2)x)
x
2

f (a ( N 2)x) f (a ( N 1)x)
x
2

f (a ( N 1)x) f (b )

x
2

f (a ) f (b) x N 1
area x
2 f (a ix)

2
2 i 1
13

N 1
f (a ) f (b)
f ( x ) d x x
x f (a ix)
2

i 1

f (x)

f (a x) f (a 2x)

Underestimates area when curve is concave down.


Overestimates area when curve is concave up.
Accuracy is enhanced with smaller x.
Higher accuracy for same x compared to Riemann
summation.
14

Interesting note: If we average the left and right


Riemann summations, use will obtain the Trapezoidal
Rule.
b
N 1
N
1

f (a ix) x f (a ix)
a f ( x) dx 2 x
i 0
i 1

N 1
N 1
1

x f (a ) x f (a ix) x f (a ix) x f (a Nx)


2
i 1
i 1

N 1
x
f (a ) f (b) x f (a ix)

2
i 1

Adding two lower accuracy estimates yields a higher


accuracy estimate. This is the idea behind Romberg
15
Integration (which we wont cover).

Riemann summation uses one point to estimate the


curve shape.

Pa
ra
b

ol
a

The Trapezoidal
Rule method uses two points.
f
oid
z
e
p
Tra
l
Left
Riemann

f (x)

xi

xm

xi1

x xi 1 xi

Simpsons Rule method uses three points (a


parabola) to estimate the curve shape.
Higher degree polynomials and other integration
schemes can be can be used (see Romberg

16

In the Simpsons Rule method, each parabola is fit to


3 points in an interval (two endpoints and midpoint)

Pa
ra
b

ol
a

fk
f k 1

f (x)

f k 1
xi
xk 1

xi1/2
xk

xi1
xk 1

x xi 1 xi
x xk 1 xk 1

These points are relabeled with indices k1, k, k+1.


17

The parabola passes through the point (xk, fk) and so


2
P
(
x
)

A
(
x

x
)
must have the form ofk B( x xk ) f k
f
P(
x)

fk
f k 1

f (x)

f k 1
xk 1

xk

Evaluate this function at xk-1 and xk+1 ,

xk 1

x xk 1 xk 1

f k 1 A( xk 1 xk ) 2 B( xk 1 xk ) f k
f k 1 A ( xk 1 xk ) 2 B( xk 1 xk ) f k
18

f k 1 A ( xk 1 xk ) 2 B( xk 1 xk ) f k
f k 1 A( xk 1 xk ) 2 B( xk 1 xk ) f k

x xk 1 xk 1
Using the relationship
f k 1 A ( x/2) 2 B( x/2 ) f k A (x/2) 2 B(x/2 ) f k
f k 1 A (x/2) 2 B( x/2) f k A(x/2) 2 B(x/2 ) f k

Adding the two equations,


f k 1 f k 1 2A ( x/2 ) 2 2 f k

f k 1 2 f k f k 1
A
2(x/2 ) 2

Subtracting the two equations,


f k 1 f k 1 2B( x/2 )

f k 1 f k 1
B
2(x/2) 2
19

The equation for a parabola that passes through the


function f (x) at the endpoints and midpoint of an
interval is,
P ( x) A( x xk ) 2 B( x xk ) f k
f k 1 2 f k f k 1
A
2(x/2 ) 2

f k 1 f k 1
B
2(x/2) 2

xk 1
xk 1
2
The
area
under
the
parabola
isx obtained
through
P
(
x
)
d
x

[A
(
x

x
)

B
(
x

f
]
d
x
k
k
k
xk 1
xk1
integration.

(A/3) ( x xk )

3 xk 1

xk 1

( B/2) ( x xk )

2 xk 1

xk 1

xk 1

fk x |

xk 1

x/2 xk 1 xk xk xk 1
xk 1
Evaluating
the
x result at the limits of integration and
f k 1 4 f k f k 1
x relationship
20
xk1 P( x) dthe
utilizing
6

The area under one parabola is,

x
f k 1 4 f k f k 1
6

x xk 1 xk 1

Using our original notation,

x
f ( xi ) 4 f ( xi1/2 ) f ( xi1 )
6

x xi 1 xi

Lets look at a simple example with three parabolas.

21

f (x)
x xi 1 xi

x0

x0.5

x1

x1.5

x2

x2.5

x3

x
The area of each parabola isf ( xi ) 4 f ( xi 1/2 ) f ( xi 1 )
6
x
f ( x0 ) 4 f ( x0.5 ) f ( x1 )
6
x
f ( x1 ) 4 f ( x1.5 ) f ( x2 )
6
x
f ( x2 ) 4 f ( x2.5 ) f ( x3 )
6

22

x
f ( x0 ) 4 f ( x0.5 ) f ( x1 )
6
x
f ( x1 ) 4 f ( x1.5 ) f ( x2 )
6
x

f ( x2 ) 4 f ( x2.5 ) f ( x3 )
6
x
1 f ( x0 ) 4 f ( x0.5 ) 2 f ( x1 ) 4 f ( x1.5 ) 2 f ( x2 ) 4 f ( x2.5 ) 1 f ( x3 )
6

A pattern emerges,
b

N 1
N 1
x

f ( x ) dx
f (a ) f (b) 4 f (a x(2i 1)/2 ) 2 f (a ix)

6
i 0
i 1

23

We assumed that all intervals are equally spaced, but


often it is advantageous to use smaller intervals
when the function changes rapidly and larger
intervals when the function changes smoothly.

x
For ME 330A, we will use a fixed interval size.
24

We have learned about five methods of numerical


integration
b
b
N
N 1
f ( x) dx x f (a i x)
f ( x) dx x f (a i x)
a

i 0

i 1

i 1

f ( x) dx x f (a x(2i 1)/2)
b

N 1
f (a ) f (b)
f ( x) dx x
x f (a ix)
2

i 1

N 1
N 1
x

f ( x ) dx
f
(
a
)

f
(
b
)

4
f
(
a

x
(2i

1)/2
)

2
f
(
a

x
)

6
i 0
i 1

Simpsons Rule requires more calculations at a given


25
x, but it is more accurate for the same x .

Si
m

ps

on
s

If a 2nd degree polynomial (Simpsons Rule) is pretty


accurate, wouldnt a higher degree polynomial be
even more accurate?
f

f (x)

xi

oid
z
e
p
Tra
l
Left
Riemann

xi1

x xi 1 xi
x

4th degree
polynomial

Yes, but more calculations are required. Usually


Simpsons Rule method is sufficient.
26

How many discretizations are enough?

x
First, establish how much error () in the area you are
comfortable with. 10%? 1%? 0.01%? 0.0001%?
Calculate the area for N discretizations (x), AN
Calculate the area for 2N discretizations (x/2), A2N
If |(A2N AN)/AN|< , you are done.
If |(A A )/A |> , halve the interval size and try

27

Matlab allows you to numerically integrate using the


quad( ) function. Place the function in single quotes.
To calculate the integral of x3ex from 0 to 2,
>> quad('x.^3.*exp(x)', 0, 2)
FUTURE ADJUST TOLERANCE IN QUAD()

28

You might also like