$30
Numerical Analysis, Section 2
Project One: The (n + 1)-point Trapezoidal Rule
1. Project description
The integral
Z b
a
f(x) dx (1)
can be (crudely) estimated by approximating the area under the graph of f(x) by a trapezoid. In
other words, by approximating the integrand f via the affine function
g(x) = f(b) − f(a)
b − a
x +
f(a)b − f(b)a
b − a
whose graph passes through points (a, f(a)) and (b, f(b)). This procedure gives us the (crude)
approximation
Z b
a
f(x) dx ≈
Z b
a
g(x) dx = pf(a) + f(b)q
b − a
2
.
We now suppose that n 1 is an integer, and let {x0, x1, x2, . . . , xn} be the sequence of (n + 1)
points defined by
xj = a +
b − a
n
· j.
If we decompose the integral (1) as
Z b
a
f(x) dx =
nX−1
j=0
Z xj+1
xj
f(x) dx (2)
and use the preceding approximation to estimate each of the integrals in (2), then we obtain the
estimate
Z b
a
f(x) dx =
nX−1
j=0
Z xj+1
xj
f(x) dx
≈
nX−1
j=0
pf(xj ) + f(xj+1)q
b − a
2n
= pf(x0) + 2f(x1) + 2f(x2) + · · · + 2f(xn−1) + f(xn)q ·
b − a
2n
.
(3)
We call (3) the (n+1)-point trapezoidal quadrature rule on the interval [a, b]. The points x0, x1, . . . , xn
are called the nodes of the (n+ 1)-point trapezoidal rule on [a, b], and the quantities w0, w1, . . . , wn
defined by
wj =
b − a
2n
λj ,
1
where
λj =
(
1 if j = 0, n
2 otherwise,
are called the weights of the (n + 1)-point trapezoidal rule. Note that using this notation, (3) can
be rewritten as
Z b
a
f(x) dx ≈
Xn
j=0
f(xj )wj . (4)
Project one consists of writing a function called “traprule” which calculates the (n + 1)-point
trapezoidal rule. More specifically, your routine will take as input an integer n 0 and double
precision numbers a < b. It will return the nodes x0, x1, . . . , xn and weights w0, w1, . . . , wn of the
(n + 1)-point trapezoidal routine in user-supplied arrays. The file “traprule.c” gives the calling
syntax for the “traprule” function. Your task is to implement the function as described there.
2. Testing and grading
A public test code is given in the file “traptest1.c”. Another test code, called “traptest2.c”, will be
used to test your function as well. Half of the project grade will come from the first test file, and
the second half will come from the second.
The commands
gcc -o traptest1 traprule.c traptest1.c -lm
./traptest1
can be used to compiler and execute your program. The first command compiles your program and
creates an executable file named “traptest1”. The “-lm” option tells the compiler to include the
math library which implements cosine, sine and other basic functions. The second command runs
the traptest1 executable. There are five tests of your function in traptest1, and the program will
tell your score out of 5. We will also test your code by compiling against “traptest2.c”, which we
will not release until after the projects are due.
More explicitly, we will grade your project by running the sequence of command
gcc -o traptest1 traprule.c traptest1.c -lm
./traptest1
gcc -o traptest1 traprule.c traptest2.c -lm
./traptest2
You will get a 0 on your project if it does not compile and run. Please start work on your project
early and come see either myself or our TA, Karry Wong, if you are having difficulties getting it
to compile.
Submitting your project:
You will submit your project using canvas. You should submit only your “traprule.c” file. You
must submit your file by 11:59 PM on the due data. Late assignments will not be accepted.
2