

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


D. Haugli, Lecturer Aer E 161 Aerospace Engineering
The Trapezoidal Rule
The trapezoidal rule uses a first-order polynomial (i.e., the equation of a line) to estimate the
−
i
i
x
x
Ii f xdx
1
polynomial is first-order) will be dropped in the derivation that follows.
With a bit of algebraic trickery, the solution to the incremental integral is,
1 1
1 1
1 0 1 0 1 1
1 0 1 1
1 0 1 1
0 1 1 1 1
0 1 1 1 1
2 1
2 0 1 1
2 0 1
0 1
1
1
− −
− −
− −
− −
− −
− − −
− − −
− −
−
−
i i i i
i i i i
i i i i
i i i i
i i i i
i i i i i i
i i i i i i
i i i i
x
x
x
x
i
x x y y
x x y y
x x a ax a ax
x x a a x x
x x a a x x
a x x a x x x x
a x x a x x x x
a x x a x x
ax ax
I a axdx
i
i
i
i
This result yields the general trapezoidal rule, which works for unequally-sized increments,
=
n
i
i i i i
n
i
I Ii x x y y 1
1 1 1 2
D. Haugli, Lecturer Aer E 161 Aerospace Engineering
∑
∑
∑
−
=
− − −
=
=
=
− −
n
n
i
i
n n n n
n
i
i i
n
i
i i i i
y y y
x
y y y y y y y y y y
x
y y
x
I x x y y
1
1
0
0 1 1 2 2 3 2 1 1
1
1
1
1 1
l
which is the form commonly seen in calculus books.
with X (^) i and y (^) i with Yi.
Example FORTRAN Code.
! This program computes the area under a curve via the general trapezoidal
! rule for arbitrary grid spacing.
PROGRAM trapezoidal_rule
IMPLICIT NONE
INTEGER :: i, n, p
REAL (kind=8) :: area
REAL (kind=8), ALLOCATABLE :: x(:), y(:)
! Open the input file and determine the number of data pairs.
OPEN (unit=12, file="input.dat")
READ (12,*) p! p is the number of data pairs (lines) in the file.
! The number if increments, which the trapezoidal rule is based on,
! is one less than the number of data pairs.
n = p - 1
ALLOCATE (x(0:n), y(0:n))
! Read the input data and compute the area (integral).
READ (12,*) x(0), y(0)
area = 0.d
DO i = 1, n
READ (12,) x(i), y(i) area = area + (x(i) - x(i-1))(y(i) + y(i-1))
END DO
area = area/2.d
! Write out the result.
WRITE (,) "The area under the curve is approximately ", area
STOP
END PROGRAM trapezoidal_rule