Least Squares Problems

Continuous least squares. Use the orthonormal basis comprising the normalized Legendre polynomials to obtain the quadratic polynomial that gives the best continuous least-squares fit for the function f(x) = e2x on the interval [-1,1]. Sketch both the function and the polynomial that fits it.

Solution This problem is really a straightforward application of the minimization results we got earlier. The inner product and norm are
< f, g > = ∫ −11 f(x)g(x)dx and ||f|| = (∫ −11 f(x)2dx)½.
As we mentioned in class, the normalized Legendre polynomials of degree 0, 1, and 2 are

p0(x) = 2-1/2,   p1(x) = (3/2)1/2x,   and   p2(x)= (5/8)1/2(3x2-1).

The quadratic polynomial p that minimizes ||f-q|| among all quadratics q is

p(x) = < f, p0> p0(x) + < f, p1> p1(x) + < f, p2> p2(x).

The inner products are all integrals of products of polynomials and exponentials; doing them results in these values:

< f, p0> = 8-1/2(e2 - e-2)
< f, p1> = (3/32)1/2(e2 + 3e-2)
< f, p2> = (5/128)1/2(e2 - 13e-2)

The final polynomial we get is
p(x) = (1/4)(e2 - e-2) + (3/8)(e2 + 3e-2)x + (5/32)(e2 - 13e-2)(3x2-1).

We now want to use Matlab to create the required plot. Plotting e2x is easy. To plot the polynomials, we will do the following steps.

  1. Put the coefficients of each Legendre polynomial into a row vector, in descending order.
    c0 = [0 0 1]; c1= [0 1 0]; c2 = [3 0 -1];
  2. Calculate the coefficients for the Legendre polynomials in the polynomial p(x).
    a0 = (1/4)*(exp(2) - exp(-2));
    a1 = (3/8)*(exp(2) + 3*exp(-2));
    a2 = (5/32)*(exp(2) - 13*exp(-2));
  3. Form the coefficient vector c for p(x). This contains the coefficients of p(x) relative to the basis {x2}
    c = a0*c0 + a1*c1 + a2*c2;
  4. Select the values of x to be used in the plot. Find the corresponding values p(x) and e2x. Plot these.
    x = linspace(-1,1,300);
    yp = polyval(c,x);
    yf = exp(2*x);
    plot(x,yf,x,yp,'r')
The title, legend, and so on can be put in using the tools in the figure window. The resulting plot is shown below.