Example 6.63 from Axler's Linear Algebra Done Right 🙏
Goal: compute an approximation to the sine function that improves upon the Taylor Polynomial approximation from calculus.
- Let
$C[-\pi, \pi]$ denote the real inner product space of continuous real-valued functions on$[-\pi, \pi]$ with inner product$\langle f, g \rangle = \int_{-\pi}^{\pi} fg$ . - Let
$v \in C[-\pi, \pi]$ be the function$v(x) = \sin x$ - Let
$U$ denote the subspace$\mathcal{P}_{5}(\mathbb{R}) \subseteq C[-\pi, \pi]$ (the space of polynomials of degree at most$5$ with real coefficients) - The goal is to choose
$u \in U$ such that$\Vert v - u \Vert$ is as small as possible where
- The solution is given by the orthogonal projection of
$v(x) = \sin x$ onto$U = \mathcal{P}_{5}(\mathbb{R})$
Approach:
- Have standard basis of
$\mathcal{P}_{5}(\mathbb{R})$
- Compute orthonormal basis of
$\mathcal{P}_{5}(\mathbb{R})$ by applying the Gram-Schmidt procedure to the basis above. - The $k$th vector of the orthonormal basis is computed as:
- Compute the orthogonal projection of
$v(x) = \sin x$ onto$\mathcal{P}_{5}(\mathbb{R})$ by using the orthonormal basis found above and the formula:
- Install with
uv venv
uv pip install .
See code.
❯ python gs_poly_project.py
Creating orthonormal basis...
Projecting sin(x) onto orthonormal basis...
Projection: 0.00564311797634678*x**5 - 0.155271410633428*x**3 + 0.987862135574673*x
Largest relative error for x ∈ [3, π]: (x=3.0, error= 271.11)
Computing pairwise inner products (might take some time)...
Inner product of 1 and 2 = 0
Inner product of 1 and 3 = 0
Inner product of 1 and 4 = 0
Inner product of 1 and 5 = 0
Inner product of 1 and 6 = 0
Inner product of 2 and 3 = 0
Inner product of 2 and 4 = 0
Inner product of 2 and 5 = 0
Inner product of 2 and 6 = 0
Inner product of 3 and 4 = 0
Inner product of 3 and 5 = 0
Inner product of 3 and 6 = 0
Inner product of 4 and 5 = 0
Inner product of 4 and 6 = 0
Inner product of 5 and 6 = 0
All inner products are zero
Projection exact (latex):
\frac{3 x}{\pi^{2}} + \frac{5 \sqrt{14} \left(x^{3} - \frac{3 \pi^{2} x}{5}\right) \left(- \frac{5 \sqrt{14} \left(- \frac{2 \pi^{3}}{5} + 6 \pi\right)}{4 \pi^{\frac{7}{2}}} + \frac{5 \sqrt{14} \left(- 6 \pi + \frac{2 \pi^{3}}{5}\right)}{4 \pi^{\frac{7}{2}}}\right)}{4 \pi^{\frac{7}{2}}} + \frac{63 \sqrt{22} \left(- \frac{63 \sqrt{22} \left(- 120 \pi - \frac{8 \pi^{5}}{63} + \frac{40 \pi^{3}}{3}\right)}{16 \pi^{\frac{11}{2}}} + \frac{63 \sqrt{22} \left(- \frac{40 \pi^{3}}{3} + \frac{8 \pi^{5}}{63} + 120 \pi\right)}{16 \pi^{\frac{11}{2}}}\right) \left(x^{5} - \frac{3 \pi^{4} x}{7} - \frac{10 \pi^{2} \left(x^{3} - \frac{3 \pi^{2} x}{5}\right)}{9}\right)}{16 \pi^{\frac{11}{2}}}
The orthogonal projection (function) is given by (approximately):
The exact solution (see sympy output) equals:
Sine function, orthogonal projection and Taylor and approximation on
Sine function, orthogonal projection and Taylor and approximation on
Sine function, orthogonal projection and Taylor and approximation on