Fitting $\cos(x)$ with a polynomial

Consider the graph of $\cos(x)$ on the interval $[-\pi/2,\pi/2]$. It looks very close to a parabola. I would like to find the parabola (or possibly family of parabolas) that "fit" the $\cos$ function the best on this interval.

By "fit" the best, I mean I want to minimize the absolute area between the curves as much as possible, i.e. minimize $$\int_{-\pi/2}^{\pi/2}|\cos(x)-p_2(x)|dx,$$

where $p_2$ is a degree $2$ polynomial.

My initial approach was to look at the Taylor approximation about $x=0$ of degree $2$

$$\cos(x)\approx1-\frac {x^2}2$$

enter image description here

The area between these curves is $2-\pi-\frac{\pi^3}{24}\approx0.15403$. Surely we can do better.

I then looked at the interpolating polynomial through the points $(-\pi/2,0),(0,1),(\pi/2,0)$, which gives the function $1-\frac{4x^2}{\pi^2}$.

enter image description here

The area between these two curves is $\frac23(\pi-3)\approx0.094395$. Better!

Next, I tried looking what quadratic, with vertex $(0,1)$, satisfies $$\int_{-\pi/2}^{\pi/2}\cos(x)-p_2(x)dx=0.$$

We then find that $p_2(x)=1-\frac{12(\pi-2)}{\pi^3}x^2$.

enter image description here

So, there area between these two curves is $0$, but the absolute area is approximately $0.0539276$. Checking numerically, this appears to be the best-fitting parabola that has vertex $(0,1)$.

Is the polynomial $p(x)=1-\frac{12(\pi-2)}{\pi^3}x^2$ the unique quadratic that minimizes $$\int_{-\pi/2}^{\pi/2}|\cos(x)-p_2(x)|dx,$$ where $p_2$ is a degree two polynomial?

Of course, it could be that there is a better-fitting polynomial that does not have its vertex at $(0,1)$. If not, then I simply ask:

Find a quadratic polynomial $p_2(x)$ that minimizes $$\int_{-\pi/2}^{\pi/2}|\cos(x)-p_2(x)|dx.$$

Now, this can be extended to other degree polynomials. For example, the best fitting constant is $c=\sqrt{2}/2$, and that gives $$\int_{-\pi/2}^{\pi/2}|\cos(x)-\frac{\sqrt2}2|dx=2(\sqrt2-1)\approx0.82843.$$ This raises my more general question.

Let $n\in\mathbb{N}$ and let $p_n(x)$ be an $n$th degree polynomial. For each $n$, what polynomial minimizes $$\int_{-\pi/2}^{\pi/2}|\cos(x)-p_n(x)|dx?$$


Solution 1:

This is NOT a complete answer. Let me offer few observations that might help someone figure out a complete answer.

The problem is to find (I am assuming $b=0$ in the solution) $a$ and $c$ such that $\int_{0}^{\pi/2}|\cos(x)-(ax^{2}+c)|dx$ is minimized. (The switch from $[-\pi/2,\pi/2]$ to $[0,\pi/2]$ is by the symmetry of the problem around $0$).

First order conditions w.r.t. $a$ and $c$ (by the usual principle of wishful thinking) are $$\newcommand{\sgn}{\mathop{\rm sgn}} \begin{aligned} \int_{0}^{\pi/2}\sgn{(\cos(x)-(ax^{2}+c))}(-x^2)dx&=0\\ \int_{0}^{\pi/2}\sgn{(\cos(x)-(ax^{2}+c))}(-1)dx&=0\\ \end{aligned}$$

The second equation says that the areas where the approximation is above the original function and is below have to be equal.

My conjecture is that this implies that the approximation and the original function have to intersect twice on $[0,\pi/2]$ (if only once then if the second equation holds the first cannot). But this is pure speculation.

Taking the idea of two intersections seriously, denote the intersections, that is solutions to $\cos(z)=az^2+c$ on $[0,\pi/2]$ by $x$ and $y>x$. The two first order conditions then become $$\begin{aligned} {}[-z]_{0}^{x}+[z]_{x}^{y}+[-z]_{y}^{\pi/2}&=0\\% {}[-z^3/3]_{0}^{x}+[z^3/3]_{x}^{y}+[-z^3/3]_{y}^{\pi/2}&=0\\% \end{aligned}$$ which solves to $y=\frac{1}{8}\pi(\sqrt5+1)$ and $x=\frac{1}{8}\pi(\sqrt5-1)$. Finding $a$ and $c$ such that $\cos{z}=az^2+c$ for $z\in\{x,y\}$ gives (thank you Mathematica) $$\begin{aligned} a&=-\frac{32\sin{(\frac{\pi}{8})}\sin{(\frac{\sqrt{5}\pi}{8})}}{\sqrt5\pi^2}\\% c&=\cos{(\tfrac{\pi}{8})}\cos{(\tfrac{\sqrt{5}\pi}{8})}\frac{3\sin{(\frac{\pi}{8})}\sin{(\frac{\sqrt{5}\pi}{8})}}{\sqrt{5}} \end{aligned}$$ which is approximately $a=-0.427001$ and $c=0.985095$. The area is then approximately (taking $[-\pi/2,\pi/2]$) equal to $0.0449071$. (This shows that Oleg567 above in the comments has done an amazing job).

Solution 2:

Just examples of approximations by such polynomials.
With comparison of Taylor approximations.
Here $t_n(x)$: degree-$n$ polynomials given from Taylor series $$ \cos(x) = 1 - \frac{1}{2!} x^2 + \frac{1}{4!}x^4 - \frac{1}{6!} x^6 + \ldots $$ And $p_n(x)$: "best fitting" polinomials found empirically.

\begin{array}{|c|l|l|} \hline n & t_n(x) \mbox { or } p_n(x) & \epsilon & -\log_{10}(\epsilon) \\ \hline 2 & t_2(x)=1.000000 - 0.500000x^2 & 0.150335 & 0.823 \\ 2 & p_2(x)=0.985095 - 0.427001x^2 & 0.044907 & 1.348 \\ \hline 4 & t_4(x)= 1.0000000 - 0.5000000x^2 + 0.0416667x^4 & 0.0090497 & 2.043 \\ 4 & p_4(x)= 0.9996916 - 0.4969942x^2 + 0.0375584x^4 & 0.0009478 & 3.023 \\ \hline 6 & t_6(x)=1.00000000-0.50000000x^2+0.04166667x^4-0.00138889x^6 & 3.1380\times10^{-4} & 3.503 \\ 6 & p_6(x)=0.99999658-0.49994448x^2+0.04153112x^4-0.00128529x^6 & 1.0604\times10^{-5} & 4.975 \\ \hline 8 & t_8(x)=1.0000000000-0.5000000000x^2+0.0416666667x^4-0.0013888889x^6+0.0000248016x^8 & 7.0852\times10^{-6} & 5.150 \\ 8 & p_8(x)=0.9999999763-0.4999994242x^2+0.0416644880x^4-0.0013860546x^6+0.0000233125x^8 & 7.3437\times10^{-8} & 7.134 \\ \hline \end{array}