程序代写案例-128A 2021
时间:2021-08-28
Homework 3
Math 128A 2021 Summer Session 2
Due Monday, 08/30/2021, 11:59 PM
1. (33 points)
(a) Derive a difference formula to approximate the first derivative at x using function values
at x− 2h, x− h, and x. What is the order of accuracy of the approximation?
(b) Derive a difference formula to approximate the second derivative at x using function
values at x− 2h, x− h, and x. What is the order of accuracy of the approximation?
(c) Apply the difference formulas to f(x) = exp(−x) at x = 0 to approximate f ′(0) and
f ′′(0). Use increasingly smaller values of h, and make tables of the approximate deriva-
tives and the absolute errors of each of the approximations.
(d) Plot the absolute errors vs.
1
h
on a log-log graph.
(e) Explain how the table of errors and the graph of the errors demonstrate the order of
convergence.
2. (30 points)
(a) For the data below find the least squares polynomials of degrees 1, 2 3, 4, and 6. You
may find the MATLAB commands polyfit and polyval useful. In Python, the same
commands are available in the numpy library.
(b) Make plots of the data and the polynomials on the same graph.
(c) For each least squares polynomial, P (x), compute the error
E2 =

j
(yj − P (xj))2 .
(d) Which polynomial do you think best approximates the data? Does increasing the degree
of the least squares polynomial always decrease the error? Does increasing the degree of
the least squares polynomial give a better approximation? Explain.
x 0.000 0.143 0.286 0.429 0.571 0.714 0.857 1.000
y 0.103 0.121 0.179 0.250 0.444 0.519 0.897 1.269
1
3. (37 points) Let f(x) = (x+ 1) exp
(−3(x+ 1)2
4
)
.
(a) Find the third degree Taylor polynomial, S(x), of f(x) about x = 0.
(b) Find the third degree polynomial, P (x), that minimizes∫ 1
−1
(
f(x)− P (x))2 dx.
Solve this problem by using orthogonal polynomials.
You could, but you need not, preform the integration by hand. You can perform the nec-
essary integration using symbolic computation using, for example, Maple, Mathematica,
or Wolfram Alpha. State what program you used and give the results. Alternately, you
may perform the integration numerically. In matlab the routine to use is integral. In
python, the corresponding routine is quad from the library scipy.integrate. Examples
are given below. We will learn about how these methods work later in the quarter.
(c) Find the third degree polynomial, Q(x), that interpolates f at the Chebyshev extrema
points: ±1, ±0.5.
(d) Make plots of f , S, P , and Q, and plots of the differences |f−S|, |f−P |, and |f−Q| on
the interval [−1, 1]. Comment on the different approximations? What are the advantages
of each of the approximations?
The first four Legendre polynomials, Lj(x), are L0(x) = 1, L1(x) = x, L2(x) = (3x
2 − 1)/2,
L3(x) = (5x
3 − 3x)/2.
% matlab integration example
%
f=@(x)((x+1).*exp(-0.75*(x+1).^2));
Iexact = 2.0/3.0 * (1.0-exp(-3.0));
xmin=-1;
xmax=1;
I0 =integral(f,xmin,xmax);
fprintf(’%f %f %e \n’,Iexact,I0,abs(Iexact-I0)/I0);
# python integration example
#
from math import exp
from scipy.integrate import quad
def f(x):
return (x+1)*exp(-0.75*(x+1)**2)
Iexact = 2.0/3.0 * (1.0-exp(-3.0))
2
xmin = -1
xmax = 1
I0, err = quad(f,xmin,xmax)
print I0,Iexact, abs(I0-Iexact)/Iexact
4. (30 BONUS points) Write a routine that takes as input x and y values from data points
and the polynomial degree n and returns the coefficients of the discrete least squares poly-
nomial approximation. Instead of using polyfit as in Problem (2), your routine should
form and solve the matrix form of the normal equations. Use your routine and the MAT-
LAB command polyval to find and plot the least squares polynomials of degree 3, 4, and 6
for the data given in Problem (2). Compare to those from Problem (2). You may want to
examine the errors, E2, or the differences between the coefficients. Comment on your results.
5. (20 BONUS points)
(a) Let Ln represent the degree n Legendre polynomial. Show that for any polynomial p of
degree less than n, ∫ 1
−1
Ln(x)p(x) dx = 0.
That is, the nth degree Legendre polynomial is orthogonal to all polynomials of degree
less than n. You may without proof that the Legendre polynomials are an orthogonal
basis.
(b) Let L˜n represent the n
th degree monic Legendre polynomial. Show that∥∥∥L˜n∥∥∥2
2
≤ ‖p˜n‖22
for all monic polynomial of degree n, p˜n, where the norm is
‖f‖22 = 〈f, f〉 =
∫ 1
−1
(f(x))2 dx.
Hint: Let w(x) = p˜n(x)− L˜n(x), and compute ‖p˜n‖22.
This result shows that the degree n monic Legendre polynomial is the degree n monic
polynomial with smallest 2-norm. The results from both part (a) and (b) are important
for high-order accurate integration methods, specifically Gaussian quadrature.
6. (10 BONUS points) Express the Legendre polynomials P0, ..., P6 of degrees 0 through 6 as
monomial expansions. That is, for each j = 0, ..., 6, find the coefficients of in the expansion
Pj(x) = a0 + a1x+ ...+ ajx
j
3
Comment on your findings.
7. (30 BONUS points) To solve the normal equations for a continuous least squares problem
on [a,b], we can solve a linear system Ac = b, where Aij =
∫ b
a φi(x)φj(x)dx and bj =∫ b
a φj(x)f(x)dx.
(a) Write a routine to form the matrix A for φi(x) = x
i that takes the inputs n, the degree
of the polynomial and a and b, the endpoints of the interval. (You may or may not have
already done this for an earlier problem. If it wasn’t this general before, edit your code to
generalize it in this way.) Form the matrix A for polynomials of degrees n = 2, 4, 6, ..., 12
on the interval [0, 2] and calculate the condition numbers of the matrices. You may use
the Matlab command cond. You only need to report the condition numbers, not the
matrices.
(b) Repeat part (a) for φi(x) = Li(x), the i
th Legendre polynomial. You will be writing a
new routine to form A when φi(x) = Li(x).
(c) Comment on your results.
4

欢迎咨询51学霸君