Lab 4.2

Polynomial Inner Products and Approximation

Focus
coefficients → integral metric → orthonormal basis
Notebook
Ch4-2 Vector Spaces of Polynomials.ipynb

Lab goal

Separate polynomial coefficients from function values, derive the Gram matrix, and project a polynomial using the lecture’s integral inner product.

Lecture-note concepts
  • Polynomial vector spaces
  • Integral inner product
  • Gram matrix
  • Gram–Schmidt
  • Legendre normalization

Code extensionUse coefficients in ascending powers throughout; correct the missing predecessor in the source Gram–Schmidt loop and verify every pair of basis polynomials.

Lecture 4 · Reading guide

Read the symbols first

Wooseok Ha, MAS110 Lecture 4 (Fall 2026), lec04.pdf: printed slides 7–11, 18, 29, 34–39 (PDF pages 9–13, 21, 33, 39–44).

A polynomial is a vector in a finite-dimensional function space. Its coefficient array is a coordinate representation, not the definition of its length. We use the lecture’s inner product ⟨f,g⟩=∫−11f(t)g(t) dt\langle f,g\rangle = \int_{-1}^{1} f(t)g(t) \,dt and start with P2\mathcal P_{2}, the polynomials of degree at most two, including the zero polynomial.

⟨u,v⟩;∥u∥;u⊥v\langle u,v\rangle;\quad \lVert u\rVert;\quad u\perp v
An inner product gives one real number, ∥u∥=⟨u,u⟩\Vert u\Vert = \sqrt{\langle u,u\rangle} is its induced norm, and u⊥vu \perp v means ⟨u,v⟩=0\langle u,v\rangle = 0. The lecture writes |u| for this norm. In Euclidean space the default is uTvu^{\mathsf T}v and the norm is ∥u∥2\Vert u\Vert _{2}; for polynomials it is the stated integral norm.
φ;θ;θ^\varphi;\quad \theta;\quad \hat\theta
This chapter uses φ\varphi for an angle and θ\theta for model coefficients in Lab 4.4. The lecture sometimes uses θ\theta for the angle too; the distinction here prevents mixing an angle with a coefficient vector. A hat marks a minimizing coefficient, not a unit vector.
A;G;Q;R;PA;\quad G;\quad Q;\quad R;\quad P
A stores input columns in the Euclidean QR and least-squares labs. G denotes a Gram matrix of inner products, including the polynomial metric (which the lecture locally calls A). Q stores orthonormal columns, R stores their reconstruction coefficients, and P projects onto a subspace. These matrices have different jobs and shapes.
Pn;B=(1,t,…,tn)\mathcal P_n;\quad B=(1,t,\ldots,t^n)
Pn\mathcal P_{n} has dimension n + 1. “Degree at most n” includes lower degrees and the zero polynomial; polynomials of degree exactly n do not form a vector space. B is an ordered basis, so order determines which coefficient goes with each term.
f(t)=c0+c1t+c2t2;[f]B=cf(t)=c_0+c_1t+c_2t^2;\quad [f]^B=c
The coordinate vector c=(c0,c1,c2)Tc = (c_{0},c_{1},c_{2})^{\mathsf T} has three entries. The scalar t is the argument; f(t) is the value at that argument. This page uses t instead of the lecture’s occasional x to distinguish an argument from a coordinate vector.
Gij=∫−11ti+j dt,i,j=0,…,nG_{ij}=\int_{-1}^1t^{i+j}\,dt,\quad i,j=0,\ldots,n
These indices label monomial degrees, starting at 0. Gij=0G_{ij} = 0 for odd i+j and 2/(i+j+1) for even i+j. The lecture calls this Gram matrix A locally; we call it G to keep A for the design matrix in 4.3–4.4.
⟨f,g⟩=cTGd;∥f∥2=cTGc\langle f,g\rangle=c^{\mathsf T}Gd;\quad \lVert f\rVert^2=c^{\mathsf T}Gc
c and d are the coordinates of f and g in B. The plain coordinate dot product cTdc^{\mathsf T}d is generally different. For example, the coordinate arrays for 1 and t2t^{2} have dot product 0, but their integral inner product is 2/3.
q0,q1,q2;C=[ [q0]B∣[q1]B∣[q2]B ]q_0,q_1,q_2;\quad C=\bigl[\,[q_0]^B\mid[q_1]^B\mid[q_2]^B\,\bigr]
We label polynomial basis vectors by degree, beginning at 0; these correspond to the lecture’s w1,w2,w3w_{1},w_{2},w_{3}. C has coefficient columns. Orthonormality for this metric means CTGC=I3C^{\mathsf T}GC = I_{3}, not CTC=I3C^{\mathsf T}C = I_{3}.
Legendre Pk(t);normalized qk(t)\text{Legendre }P_k(t);\quad \text{normalized }q_k(t)
Standard Legendre polynomials satisfy Pk(1)=1P_{k}(1) = 1 and ∫−11Pk2=2/(2k+1)\int_{-1}^{1} P_{k}^{2} = 2/(2k+1). Multiplying by (2k+1)/2\sqrt{(2k+1)/2} gives qkq_{k} of unit integral norm. Endpoint normalization and unit-norm normalization are different.
NumPy ascending powers: [c0,c1,c2][c_{0},c_{1},c_{2}]
numpy.polynomial.polynomial uses increasing powers: [0,1] is t and [0,0,1] is t2t^{2}. The original notebook uses legacy np.polyval/np.polyint, whose arrays run in the opposite order. We use the modern ascending convention consistently and do not mix the two APIs.

Key ideas

Terms you will use

Polynomial coordinatesf(t)=c0+c1t+c2t2f(t)=c_0+c_1t+c_2t^2

The ordered monomial basis identifies a polynomial with a coefficient vector.

Keep in mind A coefficient is not a sampled function value.
Integral inner product⟨f,g⟩=∫−11f(t)g(t) dt=cTGd\langle f,g\rangle = \int_{-1}^{1} f(t)g(t)\,dt = c^{\mathsf T}Gd

Integrate the product of two functions over the specified interval.

Keep in mind Changing the interval or weight changes the geometry.
Orthonormal polynomialsCTGC=IC^{\mathsf T}GC=I

Basis functions have unit integral norms and zero cross inner products.

Keep in mind CTCC^{\mathsf T}C checks a different metric.

Change the input

See the geometry respond

Project f(t)=tnf(t)=t^n onto Pk\mathcal P_k using ∫−11f(t)g(t) dt\int_{-1}^1f(t)g(t)\,dt. Compare the target, its closest polynomial, and the residual over the whole interval. The curve’s vertical axis shows values, not coefficient coordinates.

Integrated squared error
0.17778
Largest ∣⟨e,qj⟩∣\text{Largest }|\langle e,q_j\rangle|
0
Projection of t^2 onto polynomials of degree at most 1f(t)=t2; p(t) · closest polynomial; e(t) = f(t) − p(t). Function values are plotted against their argument.-101-0.30.31argument tfunction value
  • f(t)=t2f(t)=t^{2}
  • p(t) · closest polynomial
  • e(t) = f(t) − p(t)

p(t)=0.33333p(t)=0.33333. The residual is orthogonal in the integral inner product, even where the curves have different heights.

Lab 4.2 · Geometry of functions

Orthogonalize 1, t,t2t, t^{2} and find the best line

Use the lecture’s low-degree example before generalizing. Coefficients run in ascending powers everywhere in this lab.

Select a line to see its action, array shape, and concrete operation.

Maintained notebookCh4-2 Vector Spaces of Polynomials.ipynb ↗Includes a corrected Gram–Schmidt routine, a cubic approximation exercise, and plots on [−1,1].

Line 1: import numpy as np
L01 / 30Store coefficients in one fixed order
Predict

What polynomial does [1,0,0] represent here?

Before

Polynomials are vectors too

t has coordinates (0,1,0)T;t2(0,1,0)^{\mathsf T}; t^{2} has coordinates (0,0,1)T(0,0,1)^{\mathsf T} in (1,t,t2)(1,t,t^{2}). Their function-space inner product is defined by an integral.

coordinates of t∣t2t | t^{2}

After
?

Reveal to compare.

Connect the steps

Why polynomial geometry needs its own inner product

01 An integral can give lengths and angles to functions

The integral is symmetric and bilinear. Also ∫−11f(t)2 dt≥0\int_{-1}^{1} f(t)^{2} \,dt \ge 0. A nonzero polynomial is continuous and cannot vanish throughout an interval; somewhere its squared value is positive on a neighborhood, so the integral is strictly positive. Thus the integral defines an inner product on Pn\mathcal P_{n}.

For f(t)=tf(t) = t and g(t)=t2g(t) = t^{2}, their product t3t^{3} is odd. Its negative and positive signed areas cancel, giving inner product 0. This is function-space orthogonality; crossing curves or a right angle between their drawn tangents is not the definition.

⟨t,t2⟩=0;∥t∥2=23;∥t2∥2=25\langle t,t^2\rangle=0;\quad \lVert t\rVert^2=\frac23;\quad \lVert t^2\rVert^2=\frac25

02 The Gram matrix translates function geometry into coordinates

Expand f=∑icitif = \sum_i c_i t^i and g=∑jdjtjg = \sum_j d_j t^j. Bilinearity lets us move the finite sums outside the integral, leaving ∑cidj∫ti+j=cTGd\sum c_{i}d_{j}\int t^{i+j} = c^{\mathsf T}Gd. Symmetry comes from swapping i and j.

For degree two, completing the square gives cTGc=2(c0+c2/3)2+(2/3)c12+(8/45)c22c^{\mathsf T}Gc = 2(c_{0}+c_{2}/3)^{2} + (2/3)c_{1}^{2} + (8/45)c_{2}^{2}. This is positive for every nonzero coefficient vector, so G is positive definite. Coordinate Euclidean length is a different metric.

G=(202/302/302/302/5)G=\begin{pmatrix}2&0&2/3\\0&2/3&0\\2/3&0&2/5\end{pmatrix}

03 Subtract every earlier basis direction before normalizing

Normalize 1 to q0=1/2q_{0} = 1/\sqrt{2}. Because t is odd, it is perpendicular to q0q_{0}; normalize it to q1=3/2tq_{1} = \sqrt{3/2}t. The polynomial t2t^{2} is not perpendicular to the constant: ⟨t2,q0⟩=2/3\langle t^{2},q_{0}\rangle = \sqrt{2}/3. Subtracting this component gives w2(t)=t2−1/3w_{2}(t) = t^{2} - 1/3.

The source loop uses range(i − 1), which skips the immediately preceding basis vector and does no subtraction for i=1i = 1. The maintained routine uses range(j) for all previous vectors with 0-based indexing. It checks the norm before division and verifies CTGCC^{\mathsf T}GC, not just the curves’ appearance.

q2(t)=458(t2−13);∫−11(t2−13)2 dt=845q_2(t)=\sqrt{\frac{45}8}\bigl(t^2-\tfrac13\bigr);\quad \int_{-1}^1\bigl(t^2-\tfrac13\bigr)^2\,dt=\frac8{45}

04 The best line for t2t^{2} is a constant, by symmetry

To project t2t^{2} onto P1=span⁡{1,t}\mathcal P_{1} = \operatorname{span}\{1,t\}, use the orthonormal coordinates ⟨t2,q0⟩\langle t^{2},q_{0}\rangle and ⟨t2,q1⟩\langle t^{2},q_{1}\rangle. The latter is zero because t3t^{3} is odd. The former times q0q_{0} gives the constant 1/3.

For any line a + bt, the error is the perpendicular residual t2−1/3t^{2} - 1/3 plus a change within P1\mathcal P_{1}. The integral norm obeys Pythagoras, proving global optimality. The curves need not match at individual points. This is continuous approximation, whereas 4.4 fits a finite list of measurements.

∥t2−(a+bt)∥2=845+2(a−13)2+23b2\lVert t^2-(a+bt)\rVert^2=\frac8{45}+2\bigl(a-\tfrac13\bigr)^2+\frac23b^2

05 A matrix projection uses the chosen metric

Let E contain coordinates of a basis for the approximation subspace. Writing the approximate coefficient vector as EαE\alpha and enforcing ETG(c−Eα)=0E^{\mathsf T}G(c - E\alpha ) = 0 gives (ETGE)α=ETGc(E^{\mathsf T}GE)\alpha = E^{\mathsf T}Gc. The metric G belongs in both places.

If the basis columns C are orthonormal for G,α=CTGcG, \alpha = C^{\mathsf T}Gc and the coefficient map is CCTGCC^{\mathsf T}G. This reduces to QQTQQ^{\mathsf T} only in a Euclidean orthonormal coordinate system. The same projection idea therefore connects all four labs without silently changing metrics.

G-weighted projection: c^=E(ETGE)−1ETGc\text{G-weighted projection: }\hat c=E(E^{\mathsf T}GE)^{-1}E^{\mathsf T}Gc

Check your understanding

Are sampled heights on a grid the polynomial’s coefficients?

No. Coefficients are coordinates in a basis; sampled heights are values f(tk)f(t_{k}). Sampling creates a different representation and a plain sum over samples is not automatically the integral inner product.

Why does 1 fail to be perpendicular to t2t^{2}?

Their product t2t^{2} has integral 2/3 on [−1,1]. Coefficient-array orthogonality does not imply integral orthogonality.

Why must the Gram–Schmidt loop use range(j)?

At 0-based step j there are j previously accepted basis vectors, with indices 0 through j−1. range(j−1) leaves one out.

Is q2q_{2} the standard Legendre polynomial P2P_{2}?

It is a normalized multiple. P2(t)=(3t2−1)/2P_{2}(t) = (3t^{2}-1)/2 has P2(1)=1P_{2}(1)=1, while q2=5/2P2q_{2} = \sqrt{5/2}P_{2} has unit integral norm.

Does the best polynomial approximation pass through every target point?

No. It minimizes the integrated squared difference over the interval. Here the best line for t2t^{2} is 1/3, with nonzero error 8/45.

Code references: NumPy: ascending polynomial coefficients ↗ · NumPy: polynomial integration ↗

Continue in Python

Run the complete notebook in Colab

This maintained notebook uses the same notation and worked example as the web lab. Run all cells in order, then change the inputs. NumPy is required; the final plot also uses Matplotlib, available in Colab.