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:
- sin(x)
Show data
| x | sin(x) |
|---|---|
| −π | -0.00000 |
| −3π/4 | -0.70711 |
| −π/2 | -1.00000 |
| −π/4 | -0.70711 |
| 0 | -0.00000 |
| π/4 | 0.70711 |
| π/2 | 1.00000 |
| 3π/4 | 0.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:
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:
- sin(x)
- 1.27323954x − 0.405284735x²
Error: 1.27323954x − 0.405284735x² − sin(x)
Show data
| x | sin(x) | 1.27323954x − 0.405284735x² | Error |
|---|---|---|---|
| 0 | 0.00000 | 0.00000 | +0.00000 |
| π/8 | 0.38268 | 0.43750 | +0.05482 |
| π/4 | 0.70711 | 0.75000 | +0.04289 |
| 3π/8 | 0.92388 | 0.93750 | +0.01362 |
| π/2 | 1.00000 | 1.00000 | +0.00000 |
| 5π/8 | 0.92388 | 0.93750 | +0.01362 |
| 3π/4 | 0.70711 | 0.75000 | +0.04289 |
| 7π/8 | 0.38268 | 0.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:
- sin(x)
- x − x³/3! + x⁵/5! − x⁷/7!
Error: x − x³/3! + x⁵/5! − x⁷/7! − sin(x)
Show data
| x | sin(x) | x − x³/3! + x⁵/5! − x⁷/7! | Error |
|---|---|---|---|
| 0 | 0.00000 | 0.00000 | +0.00000 |
| π/8 | 0.38268 | 0.38268 | +0.00000 |
| π/4 | 0.70711 | 0.70711 | +0.00000 |
| 3π/8 | 0.92388 | 0.92387 | -0.00001 |
| π/2 | 1.00000 | 0.99984 | -0.00016 |
| 5π/8 | 0.92388 | 0.92273 | -0.00115 |
| 3π/4 | 0.70711 | 0.70124 | -0.00587 |
| 7π/8 | 0.38268 | 0.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.