I'm working on the second part of this problem from Strang's Linear Algebra, 4e (problem 3.4.21):
What is the closest function $a\cos(x) + b\sin(x)$ to the function $f(x) = \sin(2x)$ on the interval from $-\pi$ to $\pi$? What is the closest straight line $c + dx$?
The solution says,
By orthogonality, the closest functions are $0\sin(2x) = 0$ and $0 + 0x = 0$.
That is, $a=b=c=d=0$.
However, it seems that $d$ should not be zero, as $x$ is not orthogonal to $\sin(2x)$ on the interval given:
$\int_{-\pi}^{\pi}x\sin(2x) dx = -\pi$
Here is how I solved it (by least squares). We have
$$ \begin{bmatrix} 1 & x \\ \end{bmatrix} \begin{bmatrix} c \\ d \\ \end{bmatrix} = \sin(2x) $$ Then the normal equations are
$$ \begin{bmatrix} \int_{-\pi}^{\pi} dx & \int_{-\pi}^{\pi}x dx \\ \int_{-\pi}^{\pi}x dx & \int_{-\pi}^{\pi}x^2 dx \\ \end{bmatrix} \begin{bmatrix} c \\ d \\ \end{bmatrix} = \begin{bmatrix} \int_{-\pi}^{\pi}\sin(2x) dx \\ \int_{-\pi}^{\pi}x\sin(2x) dx \\ \end{bmatrix} $$
$$ \begin{bmatrix} 2\pi & 0 \\ 0 & \frac{2}{3}\pi^3 \end{bmatrix} \begin{bmatrix} c \\ d \\ \end{bmatrix} = \begin{bmatrix} 0 \\ -\pi \\ \end{bmatrix} $$ which yields $$ c= 0, d= \frac{-3}{2\pi^2} \\ y= \frac{-3}{2\pi^2}x $$
I think I'm right, because my solution has lower error than $y=0$: $$ E_1^2 = \int_{-\pi}^{\pi} (\sin(2x) - 0)^2 dx = \pi \\ E_2^2 = \int_{-\pi}^{\pi} (\sin(2x) + \frac{3}{2\pi^2}x)^2 dx = 2.664 $$
Is this correct?
Another way to find out is to minimize the following integral directly with respect to $a$,
$$I(a)= \int_{-\pi}^{\pi} [\sin(2x) -ax]^2 dx = \frac{\pi}{3}(2\pi^2a^2+6a+3) $$
Setting $I’(a) =0$ produces the same result as the least square method, i.e. $a=-3/(2\pi^2)$. Then,
$$I = \int_{-\pi}^{\pi} \left[ \sin(2x) + \frac{3x}{2\pi^2}\right]^2 dx = \pi - \frac{3}{2\pi} < \pi $$
Thus, $f(x)=0x+0$ is not the best fit.
——————
Edit: Keep in mind, though, the book may be using a different criteria for the optimal solution. For instance, it may assume that the best fit is for functions to have the same average value. In this case, $f(x)=0x$ would be the answer, because
$$\int_{-\pi}^{\pi} \sin(2x) dx = 0 $$