Fundamentals / Numerical mathematics / Approximation and quadrature

Weighted projections and Legendre polynomials

Derive a best mean-square polynomial and determine how a change of weight changes what that polynomial predicts.

From first principles

Project a signal under a specified measurement weight

Choose a nonnegative weight \(w(x)\) and define the squared error of a model \(p\) by integration of \(w|f-p|^2\) over the interval of interest. Assume the candidate basis functions have finite, positive weighted norms and are mutually orthogonal. To find the best coefficient for a basis function \(\phi_j\), vary that coefficient while holding the others fixed. The derivative of the squared error is

\[\frac{\partial}{\partial a_j}\int w\left(f-\sum_k a_k\phi_k\right)^2 dx=-2\left\langle f-\sum_k a_k\phi_k,\phi_j\right\rangle_w.\]

At the minimum this derivative vanishes. All cross terms involving other basis functions disappear by orthogonality, leaving

\[a_j=\frac{\langle f,\phi_j\rangle_w}{\langle\phi_j,\phi_j\rangle_w}.\]

Consequently each coefficient can be computed independently. For unit weight on \([-1,1]\), the Legendre polynomials are one natural choice. If an experiment samples unevenly or certain regions matter more, the implied weight and hence the best fit can change even though the basis spans the same set of functions.

Derivation and applications

Quantify the return from adding one orthogonal basis function

Given a limited coefficient budget, one may want to know exactly how much an extra term reduces integrated squared error. Consider a real signal \(f\), a positive weight defining an inner product, and an existing least-squares projection \(p\) onto a finite-dimensional space. Its residual \(r=f-p\) is orthogonal to every included basis function. Suppose a proposed function \(\psi\) overlaps the original space. Remove its projection first,

\[v=\psi-P\psi,\]
\[\langle v,p_{\rm old}\rangle=0\quad\text{for every }p_{\rm old}\text{ in the original space}.\]

If \(v=0\), the added function contains no new direction. Otherwise the expanded space splits into the original space and the direction \(v\). The new projection is \(p+av\); expansion and minimization give

\[\|r-av\|^2=\|r\|^2-2a\langle r,v\rangle+a^2\|v\|^2,\]
\[a=\frac{\langle r,v\rangle}{\|v\|^2}.\]

The benefit of adding the term follows immediately,

\[\|r\|^2-\|r-av\|^2=\frac{|\langle r,v\rangle|^2}{\|v\|^2}.\]

This expression is unchanged if the candidate is multiplied by a nonzero constant. It also explains why a large raw correlation with a candidate function need not imply new explanatory power: part of that candidate can be represented already. The chosen weight must be used consistently in all projections. A continuous integral and a discrete sum over experimental samples may define different gains.

Worked example

Calculate the gain from adding curvature

With unit weight on \([-1,1]\), fit \(f=x^2\) first by a constant. The constant fit is \(1/3\). Adding \(\psi=x^2\) introduces \(v=x^2-1/3\), equal to the residual. The integrated reduction is

\[\int_{-1}^1(x^2-1/3)^2dx=2/5-2/9=8/45.\]

The expanded fit reproduces the signal exactly.

Second application

Compare two candidate corrections to a measured constant

Take three equally weighted readings \(f=(2,3,7)\). The constant projection gives 4 and residual \((-2,-1,3)\). Two proposed centered shapes are \(u=(-1,0,1)\) and \(v=(1,-2,1)\). Their gains are \(5^2/2=12.5\) and \(3^2/6=1.5\) respectively. The first leaves squared error \(14-12.5=1.5\). The other centered shape is orthogonal to the first, so adding both eliminates the residual.

Distance depends on the observation

A large error over a small part of an interval may be acceptable when an instrument averages over the interval. With a positive integrable weight \(w\), minimise \(J(p)=\int_a^b w(x)|f(x)-p(x)|^2\,dx\) over polynomials of degree at most \(m\). The first variation in an arbitrary allowed direction \(q\) gives \(\int w(f-p)q=0\). Conversely this orthogonality implies

\[J(p+q)=J(p)+\int wq^2.\]

This Pythagorean identity proves uniqueness and explains why a residual with large local extrema can still be optimal in the chosen metric.

In a monomial basis these conditions couple every coefficient to every other. Choose polynomials \(\phi_j\) orthogonal for the same weight instead. Then \(p=\sum c_j\phi_j\) with \(c_j=\int wf\phi_j/\int w\phi_j^2\). Increasing the degree preserves every coefficient already computed. Using quadrature to estimate these integrals introduces another approximation. A rule that samples too sparsely can make two distinct basis vectors look identical under the discrete inner product.

Construct a basis on a symmetric interval

For unit weight on \([-1,1]\), start with \(1,x,x^2\). Odd integrands integrate to zero, so the first two are orthogonal already. Subtract the projection of \(x^2\) onto the constant, namely \(1/3\), and retain \(x^2-1/3\). Scaling this polynomial to take value one at the right endpoint gives \(P_2=(3x^2-1)/2\), the second Legendre polynomial. Its squared integral is \(2/5\), obtained by integrating its expanded fourth-degree square.

Approximating \(x^2\) by a line retains just \(1/3\). Squaring the residual and integrating gives

\[2/5-4/9+2/9=8/45.\]

The mean-square error is half that number and its square root is about 0.29814. The worst local error occurs at the endpoints and equals \(2/3\). The constant \(1/2\) has smaller uniform error but a larger squared integral of \(7/30\). The objective function explains the difference without appealing to how a fitted curve looks.

Move the weight toward one endpoint

On \([0,1]\) choose density \(w(x)=2x\), normalised to integrate to one. Fitting a constant to \(f(x)=x\) now gives its weighted mean \(2\int_0^1 x^2\,dx=2/3\). The weighted mean-square error is

\[2\int x^3\,dx-(2/3)^2=1/18.\]

Equal weighting would instead place the constant at \(1/2\). Selecting a weight thus amounts to specifying the region over which a loss should be economical. When interpreting coefficients state the interval, basis normalisation and weight together; otherwise two reported coefficients can be different descriptions of the same fitted polynomial.

A discrete inner product can give a different optimum

If only the endpoints of \([-1,1]\) are sampled with equal weight, then \(x^2\) takes value one at every observation and the best discrete constant is one. Adding the midpoint with the same weight changes it to \(2/3\). Simpson weights on those three samples instead give \((1+4\cdot0+1)/6=1/3\), the exact continuous coefficient, because that particular integral is quadratic. A mismatch between a discrete fit and a continuous projection can thus arise entirely from the observation design, even with error-free measurements.

Represent multiplication by the independent variable

Orthogonal polynomials supply a bridge from projection to quadrature. Choose an orthonormal family \(\phi_0,\phi_1,\ldots\) on an interval with positive weight \(w\). Multiplication of \(\phi_j\) by \(x\) raises its degree by one. Its inner product with \(\phi_i\) for \(i<j-1\) vanishes, because \(\langle x\phi_j,\phi_i\rangle=\langle\phi_j,x\phi_i\rangle\) and \(x\phi_i\) has degree below \(j\). Consequently \(x\phi_j\) is a linear combination of just \(\phi_{j-1},\phi_j,\phi_{j+1}\). Choosing positive leading coefficients makes the outer recurrence coefficients positive.

Truncating this multiplication map to the first \(n\) basis functions gives a symmetric tridiagonal matrix. At a root \(\xi\) of \(\phi_n\) the omitted last recurrence term vanishes. The column \((\phi_0(\xi),\ldots,\phi_{n-1}(\xi))^T\) is then an eigenvector of the truncated matrix with eigenvalue \(\xi\). Thus a polynomial root-finding task for Gaussian quadrature nodes becomes a symmetric eigenvalue problem. This correspondence uses the same positive weight throughout. If projection coefficients were assembled with a different effective inner product from the quadrature moments, the nodes would describe a different measure.

Derive two-point Gaussian quadrature from a small matrix

On \([-1,1]\) with unit weight, use \(\phi_0=1/\sqrt2\) and \(\phi_1=\sqrt{3/2}\,x\). Both diagonal entries \(\int x\phi_i^2dx\) vanish by oddness. The off-diagonal entry is

\[(\sqrt3/2)\int_{-1}^1x^2dx=1/\sqrt3.\]

The truncated multiplication matrix has zeros on the diagonal and \(1/\sqrt3\) off it, so its eigenvalues are \(\pm1/\sqrt3\). The two normalised eigenvectors have first-coordinate magnitude \(1/\sqrt2\). Symmetry makes the quadrature weights equal and their sum must be the mass of the interval, two; hence the two weights are both one.

A further spectral check reconstructs low moments without an integration table. Write the matrix as \(J\). Since \(J^2=I/3\), the quadrature estimate of the second moment is \(2e_1^TJ^2e_1=2/3\), whereas odd moments through three vanish. The rule therefore integrates every cubic exactly by linearity. At degree four the same formula predicts \(2/9\), while the true fourth moment is \(2/5\). Multiplication by \(x\) repeatedly can produce a component outside the retained basis that returns later to affect the observed moment; truncation eventually loses that information.

For nonsymmetric weights the recurrence diagonal is usually nonzero and the quadrature nodes need no longer occur in opposite pairs. A practical diagnostic remains available: all weights must be positive and their sum must equal the integral of the constant. Passing these checks is necessary but does not certify the node positions. Testing further independently known moments can expose a normalisation or indexing error that the constant check alone would miss.

For instance adding the same spurious offset to both computed quadrature nodes leaves the weights positive and unchanged. The integral of the constant still passes exactly, while the first moment becomes twice that offset instead of zero. On an interval invariant under reflection, testing one odd and one even moment quickly distinguishes centring errors from errors in the spacing or overall weight normalisation.

Change parameters · Explore the worked calculation

Change one input at a time and compare the recalculated curves with the argument above. The browser evaluates this small model locally; the accompanying numerical values state its assumptions and reference checks. Reset restores the reference inputs, and the data can be downloaded as CSV.