L 10 6 2 0 -2 -8 -20246 8 10 -10 -10-8-6-4-20 For the other arguments, use the periodicity of the sine or cosine function. sinx = 2 = 3! cosx = 1 – + 5! 7! n=0 (-1)" 2n+1 (2n+1)! (1) 2! 4! 6! n=0 (-1)"2n (2n)! (2)
Write a program to calculate the function sin(x) or cos(x) using a Taylor series expansion around the point 0. In other words, you will program the sine or cosine function yourself, without using any existing solution. You can enter the angles in degrees or radians. The program must work for any input, e.g. -4500° or +8649°. The function will have two arguments:
float sinus(float radians, float epsilon);
For your own implementation, use one of the following relations (you only need to program either sine or cosine, you don't need both):
Tip 1: Of course, you cannot calculate the sum of an infinite series indefinitely. You can see (if not, look in the program) that the terms keep getting smaller, so there will definitely be a situation where adding another term will not change the result in any way (see problem 1.3 – machine epsilon). However, you can end the calculation even earlier – when the result changes by less than epsilon (a pre-specified, sufficiently small number, e.g. 0.000001) in two consecutive iterations.
Tip 2: It is important to note that epsilon does not express the accuracy of the calculated approximation.
Tip 3: Be aware that the above series approximate well only around zero, which means that for larger input angles it will "stop working" (see the figure below - the larger the degree of the polynomial, the better the approximation).


Unlock instant AI solutions
Tap the button
to generate a solution







