#trigonometry #numericalmethods #taylorexpansion #taylorseries
So a YouTube video got me thinking about numerical methods for calculating sines and cosines. Not that it's important for most purposes, because essentially every programming language and math-capable utility has built-in sine and cosine functions.
Nevertheless, if you wanted to write the code yourself, you would at some point be doing a Taylor Expansion for sine and cosine according to the usual formulas. The problem is getting the expansions to converge to accurate, reliable values in as few terms as possible. Here is what I would recommend:
1) Angles greater than pi or less than -pi, add or subtract multiples of 2*pi to bring them between -pi and pi.
2) Use symmetry to map the angle to the range 0 to pi/2.
3) Angles from pi/4 to pi/2, you can calculate as the complementary function on the complementary angle. In other words, if you want to figure out the sine of 85 degrees, you can instead calculate the cosine of 5 degrees. So that reduces all our calculations to angles from 0 to pi/4.
All of that is obvious. Here's the part that is less obvious:
4) Figure out the sine and cosine of angles 0, pi/16, pi/8, 3*pi/16, and pi/4. As in, pre-calculate them, and store them as constants in your program. Now remember these trig identities:
sin(a+b) = sina*cosb + cosa*sinb
cos(a+b) = cosa*cosb - sina*sinb
Those identities will let you calculate all your angles as offsets from 0, pi/16, pi/8, 3*pi/16, or pi/4, whichever is closest. Like, suppose you wanted to calculate sin(7*pi/64). Well, think of that as sin(pi/8 - pi/64). Based on the trig identities above, that'd be sin(pi/8)*cos(pi/64) - cos(pi/8)*sin(pi/64). And remember, we have the sine and cosine of pi/8 pre-calculated, so all that's left is doing the Taylor Expansions on cos(pi/64) and sin(pi/64), which will resolve in only a few terms.
You could take this thinking further, where you're doing offsets against multiples of pi/32 or even pi/64. Whatever lets you get an accurate result in only a handful of expansion terms