Tuesday, July 15, 2008

Find the value of sin(x) using the series

/* Write a C program to find the value of sin(x) using the series *
* up to the given accuracy (without using user defined function) *
* Also print sin(x) using library function. */

#include stdio.h
#include conio.h
#include math.h
#include stdlib.h

void main()
{
int n, x1;
float acc, term, den, x, sinx=0, sinval;

clrscr();

printf("Enter the value of x (in degrees)\n");
scanf("%f",&x);

x1 = x;

/* Converting degrees to radians*/

x = x*(3.142/180.0);
sinval = sin(x);

printf("Enter the accuary for the result\n");
scanf("%f", &acc);

term = x;
sinx = term;
n = 1;

do
{
den = 2*n*(2*n+1);
term = -term * x * x / den;
sinx = sinx + term;
n = n + 1;

} while(acc <= fabs(sinval - sinx));

printf("Sum of the sine series = %f\n", sinx);
printf("Using Library function sin(%d) = %f\n", x1,sin(x));

} /*End of main() */

/*------------------------------
Output
Enter the value of x (in degrees)
30
Enter the accuary for the result
0.000001
Sum of the sine series = 0.500059
Using Library function sin(30) = 0.500059

RUN 2
Enter the value of x (in degrees)
45
Enter the accuary for the result
0.0001
Sum of the sine series = 0.707215
Using Library function sin(45) = 0.707179
---------------------------------------------*/

This program uses the sine series formula. The absolute value is the number of terms used inorder to get the result. By increasing the number of times you are increasing the accuracy but at the cost of memory and execution time. So well balance has to considered in giving the absolute value.


You can increase the accuracy by exapanding the series with the odd powers of x. The program uses a varaible n to keep track of the number of terms in the fornula. When it meets the necessary limit, the value is printed. Another point to note is that first we are converting degrees into radinans using the formula x=(angle)*(PI / 180)


No comments: