Question: Here you will be writing a function: double Sin(float); to compute the sin of an angle (given in radians) using the Taylor series shown. Your
Here you will be writing a function:
double Sin(float);
to compute the sin of an angle (given in radians) using the Taylor series shown. Your function should find the value of the sin for ANY angle (preferred) or at least angles up to 2 PI (Hint: if you can do the later, the former should be easy) to a precision of 4 decimal places (within 0.00005). You should put the function Sin() in the file called trig.c. You can use the function pos_power() in your exponent.c from Lab 6 and Hw 4, and you should write functions for factorial() and close_enough() in the file called util.c. This last function is given two doubles and returns true if they are within 0.00005 of each other. You can put any other functions you may want in util.c as well. You will need to provide your own .h files for each of these .c files. You should also write a simple test driver to exercise your function in the file driver1.c. You should update the makefile to compile and link driver1.c, trig.c, util.c and exponent.c when you type
make trig
and create the executable called trig.
This program works up to a certain number and then the numbers just become too big.
TRIG.C
double Sin(double x) { double term, total_so_far; int i;
term = x; /* First term in the expansion. */ total_so_far = 0.0; for (i = 1; i <= 30; i++) { /* Add current term to sum. */ total_so_far += term; /* Compute next term from the current one. */ term *= -(x * x) / (2*i) / (2*i + 1); } return total_so_far; }
DRIVER1.C
#include
int main() { double x; double ans; /*Print statement*/ printf("Enter x value in degrees: "); /*While input isn't EOF*/ while(scanf("%lf", &x) != EOF) { /*Call function*/ ans = Sin(x); /*Print statements*/ printf("Value of sin(%lf) = %lf ", x, ans); printf("Enter x value in degrees: "); } printf(" "); }
SAMPLE OUTPUT:
Values of sin(x)
sin(0) = 0, sin(1) = 0.841471, sin(1.57) = 1.000000, sin(3.14) = 0.001593, sin(6.28) = -0.003185,
sin(10) = -0.544021, sin(100) = -0.506366, sin(1000) = 0.826880
sin(-10) = 0.544021, sin(-100) = 0.506366
This was a hint my TA gave me:
Just make use of the periodicity of sine function: sin(2k? + x) = sin(x).
You can repeatedly add 2? to x (for negative x) or subtract 2? from x (for positive x) until x falls into the range of [0, 2 ?]. Another approach is to use modular operator "%" to obtain the remainder of x divided by 2?.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
