
MTH 351, Programming assignment 3, due 3/1
In this assignment you are asked to use polynomials to approximate a func-
tion and to (ultimately) find its (numerical) derivative(s) and definite integral(s).
Hint: you may find the following MATLAB and Atkinson/Han routines useful:
polyfit, polyval, divdiff, poly. interp1, spline, trapz, quad*. You
should have gained the experience necessary to solve this assignment from solv-
ing the additional assignment from 2/20.
1. Let f(x) = exp(x). Interpolate this functions on the interval [−1,1] using
n= 3 equally spaced nodes and a Lagrange polynomial p(x) of appropriate
degree (what degree ?). Estimate the error and examine the performance of
the interpolation. Repeat with a (piecewise) linear s1(x) and a cubic spline
s3(x) (specify which type) to achieve the same goal (remember to discuss the
error). Find the least squares linear lsq1(x) and quadratic lsq2(x) approximating
polynomials. Plot f(x) and all the polynomials and verify if the error that you
can read from the plot agrees with the estimate that you derived. Comment.
2. Repeat (1) for gα(x) = 1
1+α2x2with α= 1,2,5 using n= 3,7,11 equally
spaced nodes, over interval [−1,1]. How is αrelated to the difficulties with
interpolation ? Plot the polynomials and the function. To improve the results,
use Chebyshev nodes that is, the roots of Chebushev polynomials. Hint: these
are given by the formula: zi= cos( (2i+1)π
2n), i = 0,1, . . . n −1.
3. Use trapezoidal method with npoints to integrate f(x) = cos xover
(0, π/2). Compute and estimate the error depending on n. Use simpson method
and other available MATLAB methods. Comment.
4. Write a routine to implement the left, midpoint and right rectangle
quadrature rule. Apply it to the function in (3) and compare the results of
other ingeration methods.
It is known that R1
0
1
2√xdx = 1. Which of the integration methods can you use
to verify this statement ? Comment.
5. Use forward, backward, and symmetric difference with step hto find the
derivative f0(0) for f(x) = cos x. Use many values of hstarting with h=.1
down to very small h(how small can it be ?). Compute and plot the error in
function of h.
6. (EXTRA) Apply techniques found in 3,4,5 to the integral over (−1,1)
and to the derivative at 1/10000 of g(x) = p|x|. Comment.
1