← All articles

The accuracy and speed of function approximations

Function approximations trade accuracy for simplicity or speed. Their accuracy depends on the formula and the input interval. But just how accurate are the different approximations? Here I will take a look at the accuracy of the different sine function approximations.

First off, what exactly are we going to approximate? Let us take a look at the sine function in a graph:

The sine function
  • sin(x)
Show data
xsin(x)
−π-0.00000
−3π/4-0.70711
−π/2-1.00000
−π/4-0.70711
0-0.00000
π/40.70711
π/21.00000
3π/40.70711
π0.00000

The X axis goes from –π to π.

Let the approximation begin. We start out with a polynomial approximation. We simply use a quadratic equation to approximate the curve of the sine function as best as we can. I came across a post where a guy used –π, -π/2, 0, π/2,π as points of reference and derived a function from that. He came up with:

\[ 1.27323954x - 0.405284735 \cdot x^2 \]

Without getting too much into the details, this quadratic equation only approximates the curve of the interval [0 ; π]. Putting this equation into a graph with the real sine function gives us this:

Sine and the quadratic approximation
  • sin(x)
  • 1.27323954x − 0.405284735x²
Show data
xsin(x)1.27323954x − 0.405284735x²Error
00.000000.00000+0.00000
π/80.382680.43750+0.05482
π/40.707110.75000+0.04289
3π/80.923880.93750+0.01362
π/21.000001.00000+0.00000
5π/80.923880.93750+0.01362
3π/40.707110.75000+0.04289
7π/80.382680.43750+0.05482
π0.00000-0.00000+0.00000

Blue is our sine function and the dashed green line is our quadratic equation. As you can see, the quadratic function is approximately correct at 0, π/2 and π with the rounded coefficients shown. Now let us have a look at the good old Taylor Series approximation of sine:

Sine and the truncated Taylor polynomial
  • sin(x)
  • x − x³/3! + x⁵/5! − x⁷/7!
Show data
xsin(x)x − x³/3! + x⁵/5! − x⁷/7!Error
00.000000.00000+0.00000
π/80.382680.38268+0.00000
π/40.707110.70711+0.00000
3π/80.923880.92387-0.00001
π/21.000000.99984-0.00016
5π/80.923880.92273-0.00115
3π/40.707110.70124-0.00587
7π/80.382680.35960-0.02308
π0.00000-0.07522-0.07522

Blue is our sine function and the dashed green line is our truncated Taylor polynomial. Pretty neat huh? It is impossible to tell the difference between the two functions from the interval [0;π/2], but then it starts to deviate. Suffice to say that we can see from our results that this Taylor polynomial is more accurate over the plotted interval – or is it? Stay tuned for part 2.

More in Algorithms

All 25 Algorithms articles →