27
$\begingroup$

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?$$

  • 3
    well posed, interesting question with nice images (+1)2017-01-26
  • 3
    Nice question. I think you can get an analytic solution to the case in which you square the integrand, similar to the least squares approximation.2017-01-26
  • 0
    If you were instead interested in minimizing the $L_\infty$ norm you could numerically calculate the minimizing polynomial using the [Remez algorithm](https://en.wikipedia.org/wiki/Remez_algorithm). I'm not sure if there is an analogous algorithm for the $L_1$ norm.2017-01-26
  • 0
    @MrYouMath Squaring the integrand and minimising that gives a slightly different result, though, as that makes places where the graphs differ by much weigh more than with the absolute value integrand.2017-01-26
  • 0
    @Arthur: It is very likely that the result will be different, but I think it will be easier to answer.2017-01-26
  • 0
    When the game is minimising integrals, perhaps you could try the Euler-Lagrange equations?2017-01-26
  • 1
    Note that if the minimum is unique, all of the odd-order coefficients of $p_n(x)$ must vanish, since by symmetry the area between $\cos x$ and $p_n(x) = a_0 + a_1 x + a_2 x^2 + a_3 x^3 + \dots$ is the same as the area between $\cos x$ and $p_n(-x) = a_0 - a_1 x + a_2 x^2 - a_3 x^3 + \dots$. I suspect that the minimum will in fact be unique, but it's possible that it won't.2017-01-26
  • 4
    $p_2(x)\approx 0.985095 - 0.427001x^2$ gives area $\approx 0.0449076$.2017-01-26
  • 3
    The best fitting (in the $L^1$ norm) parabola with vertex at $(0,1)$ has the equation $y=1-\frac{1-\cos(a)}{a^2}x^2$ with $a=\frac{\pi}{2^{4/3}}$. The error in such a case is $0.053\color{red}{565936574}\ldots$2017-01-26
  • 0
    I think the answer looked interesting, was there some mistake? When I solve numerically for a vector of 1024 equally spaced values over a polynomial basis with the $L_2$ norm (least squares) gives me an estimated $L_1$ error $0.047002$ for $p(x) = 0.98-0.41755x^2$. Surprisingly close for aiming for the wrong norm.2017-01-26
  • 0
    @Oleg567 I do not know how to link to a user from answer. My answer below shows that Oleg567's numbers are (likely to be) extremely close to the solution.2017-01-26
  • 5
    There is a classical result that any continuous function on a compact set of $\mathbb{R}$ has a unique polynomial which delivers the best approximation in the sup norm. Take a look at for example: Timan(1963), "Theory of approximation of functions of a real variable", sections 2.1-2.3.2017-01-28

2 Answers 2

5

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).

  • 0
    Very cool to see $y=\frac{\pi\varphi}{4}$ and $x=-\frac{\pi\hat\varphi}{4}$, where $\varphi$ is the golden ratio. Intuitively, I also would expect two intersection points as that means the two functions would spend more time closer to each other, in some sense. Nonetheless, thank you for the great information!2017-01-26
3

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}