Wednesday, October 3, 2018

CPL 9 - Sin(X) USING TAYLOR SERIES

9. Develop a Program to compute Sin(x) using Taylor series approximation. Compare your result with the built-in Library function. Print both the results with appropriate messages.

ALGORITHM:

Step 1: [Start]

Step 2: [Read the value of x in degrees] Read x

Step 3: [Initialization & Radians conversion]
Temp=x
        x=x*(3.142/180.0)
        Term=x
        Sinx=term
        n=1

Step 4: [compute sin value]]
Repeat While (term>FLT_EPSILON)
Fact =2*n*(2*n+1)
Term=term*x*x/fact Sinx
sinx+term= n+1n

Step 5: [Output]
        Print sin(x) without using library function Print  sin(x) with using library function
   

Step 6: [Stop]

FLOWCHART:

PROGRAM:

#include<stdio.h>
#include<stdlib.h>
#include<math.h>

void  main()
{
int x,n,i;
float rad, res, sum=0;
printf("Enter degree\n");
scanf("%d",&x);
printf("Enter number of terms\n");
scanf("%d",&n);

rad=x*3.14/180;
for(i=1;i<=n;i+=2)
{
if ((i-1)%4==0)
    sum=sum+pow(rad,i)/fact(i);
else
    sum=sum-pow(rad,i)/fact(i);
}
printf("Calculate sin(%d) = %f", x,sum);
printf("\nLibrary sin(%d) = %f", x,sin(rad));
}

int fact(int m)
{
int i,f=1;
for(i=1;i<=m;i++)
{
   f=f*i;
}
return 1;

}


OUTPUT:


3 comments: