Trigonometric functions like sine, cosine, … can all be computed recursively as well. As exponential in our previous article, the need is just the recursive relation and the terminating condition.
Recursive relation for sine(x) could be sine(x) = 2 * sine(x / 2) * cosine(x / 2) = 2 * sine(x / 2) * sqrt(1 – sine2(x / 2)). And accordingly, the terminating condition has to be derived for the limiting case when x is approaching zero, but not necessarily zero. And from world of limits, we have sine(x) = x for small x’s in radians. Also, on careful observation, the recursive relation has just one lower order x / 2, so the computation could be simplified as:
v = sine(x / 2); sine(x) = 2 * v * sqrt(1 - v2);
Putting in the complete logic (say in C) would be as follows:
#define DELTA 0.001 // Depends on the desired accuracy double sine(double x) { if (fabs(x) < DELTA) // Terminating Condition return x; else { double v = sine(x / 2); return 2 * v * sqrt(1 - v * v); } }
Similarly, cosine(x) could be recursively defined as 2 * cosine2(x / 2) – 1 with terminating condition of 1 – x2 / 2. And similarly all others.
Moreover, why only trigonometric functions, but all kind of mathematical functions having some recursive relation (with a terminating condition) can be computed using recursion.
And why only mathematical functions, but even procedural logic could be computed using recursion. Watch out for the same …
Pingback: Deriving the Functional Recursion | Playing with Systems
Pingback: Introduction to Procedural Recursion | Playing with Systems