Sunday, December 12, 2010

Cos x Function Evaluation using Taylor series

//PROGRAM TO EVALUATE FUNCTION COS X USING TAYLOR SERIES.
#include
#include
#include
#define PI 3.1415

double cos_x(int,int);
int fact(int);
void main()
{
int x,n;
printf("Enter the value of x:");
scanf("%d",&x);
printf("Enter the value of n:");
scanf("%d",&n);
printf("\nValue of cos x is ",exp_x(x,n));
getch();
}
double cos_x(int ang_deg,int nt)
{
int term,j;
double value=1.0,ang_rad=0.0;
ang_rad=(double)ang_deg*PI/180;
for(term=2,j=1;term<=nt;term+=2,j++)
{
value+=(double)pow(-1.0,j)*pow(ang_rad,term)/fact(term);
}
return value;
}

int fact(int num)
{
int f=0;
if(num==1)
return 1;
else
f=num*fact(num-1);
return f;
}

No comments:

Post a Comment