# Approximating $|x|$ by a linear combination of $1, \cos x, \sin x, \cos 2x, \sin 2x$

Let $\phi(x) = |x|$ for $x \in (-\pi, \pi)$. Suppose we approximate $\phi(x)$ by a linear combination of the functions $\{1, \cos x, \sin x, \cos 2x, \sin 2x\}$. What linear combination of the form:

$$a_0/2 + a_1 \cos x + b_1 \sin x+ a_2 \cos 2x + b_2\sin 2x$$

gives the best least square ($L^2$) approximation?

Any and all help is appreciated. I am having trouble starting this for now although more help would be nice.

Thank you

-
Related post: math.stackexchange.com/questions/82391. – Srivatsan Dec 7 '11 at 23:07
Note that $|x|$ is even; you can thus expect $b_1$ and $b_2$ to be zero. (why?) – J. M. Dec 7 '11 at 23:45

I hope I have interpreted this question properly, but I believe the way to go about solving this is to set up the following error integral:

$$E(a_0, a_1, a_2) = \int_{0}^{\pi} 2 ( x - \frac{a_0}{2} - a_1 \cos(x) - a_2 \cos(2 x))^2 dx$$

and then minimizing $E(a_0, a_1, a_2)$.

As J.M. pointed out, $|x|$ is even and the $\sin$'s will just increase the error so they can be left out. The above integral can be done easily, if not tediously, by hand but I'm too lazy, so I just asked Mr. Wolfram which yielded:

$$\frac{\pi a_0^2}{2} - \pi^2 a_0 + \pi a_1^2 + \pi a_2^2 + 8 a_1 + \frac{2 \pi^3}{3}$$

This is a (hyper?) paraboloid so let's use our standard tricks:

$$\frac{\partial E(a_0, a_1, a_2)}{\partial a_0} = \pi a_0 - \pi^2 = 0 \to a_0 = \pi$$ $$\frac{\partial E(a_0, a_1, a_2)}{\partial a_1} = 2 \pi a_1 + 8 = 0 \to a_1 = \frac{-4}{\pi}$$ $$\frac{\partial E(a_0, a_1, a_2)}{\partial a_2} = 2 \pi a_2 = 0 \to a_2 = 0$$

Just by eye-balling it you can see that the second derivative in each is positive, so that the paraboloid is increasing and the points just calculated represent the unique global minimum.

Comparing the plot of $|x|$ and $\frac{\pi}{2} - \frac{4 cos(x)}{\pi}$ on $[0, \pi]$, we can see it sort of matches up:

-
@J.M: Arg, fixed. Thanks – user4143 Dec 8 '11 at 4:47

You can just take the $a_n,b_n$ to be the Fourier coefficients of $|x|$:

$$a_n = \frac{1}{\pi}\int_{-\pi}^\pi |x| \cos nx \, dx$$ $$b_n = \frac{1}{\pi}\int_{-\pi}^\pi |x| \sin nx \, dx$$

Since the functions $\{\cos nx, \sin nx\}$ form an orthonormal basis for $L^2(-\pi, \pi)$ any $f \in L^2(-\pi, \pi)$ can be written as $$f = a_0/2 + \sum a_n \cos nx + b_n \sin nx$$ Then the approximation above is the orthogonal projection of $|x|$ onto the subspace spanned by $\{1,\cos x,\sin x,\cos 2x,\sin 2x\}$ so this is the best approximation in $L^2$ of this form.

For details see for example Real and Complex Analysis by Rudin, Chapter 4.

-