Information Technology Reference
In-Depth Information
easy to see if we write out P iD1
6 f.x i1 /C 6 f.x iC 2 /C 6 f.x iC1 / as
1
6 f.x 0 /C 4
6 f.x 2 / C 1
6 f.x 1 / C
1
6 f.x 1 /C 4
6 f.x 2 2 /C 1
6 f.x 2 / C
1
6 f.x 2 /C 4
6 f.x 3 2 /C 1
6 f.x 3 / C
C
1
6 f.x n1 /C 4
6 f.x n 2 /C 1
6 f.x n /:
This expression can be expressed as two sums plus a contribution from the end
points:
n X
n X
1
6 f.x 0 /C 4
f.x i 2 / C 2
f.x i /C 1
6 f.x n /:
6
6
iD1
iD1
A more efficient formula for Simpson's rule is therefore
!
Z b
X
n1
X
n
f.x/dx 1
6 h
f.a/C f.b/C 2
f.x i / C4
f.x i 2 /
:
(6.4)
a
iD1
iD1
The corresponding algorithm appears in Algorithm 6.4 . Of course, we could per-
form the hand optimization of incrementing x i
instead of using a C ih
and a C .i 2 /h . This saving of one multiplication in each loop can be detected
automatically by a compiler or simply drown in the work required by evaluating
f.x/ .
and x i 2
Algorithm 6.4
Optimized Simpson's Rule.
Simpson ( a , b , f , n )
h D ba
n
s 1 D 0
for i D 1;:::;n1
x D a C ih
s 1 s 1 C f.x/
end for
s 2 D 0
for i D 1;:::;n
x D a C .i 2 /h
s 2 s 2 C f.x/
end for
s 6 h.f .a/ C f.b/C 2s 1 C 4s 2 /
return s
 
Search WWH ::




Custom Search