C Program to Implement Taylor Series [Sum of Cos Series,Sine Series,e^x]
This is the c program to implement taylor series
it finds the sum of cos series,sine series and e^x
A Taylor series is a representation of a function as an infinite sum of terms that are calculated from the values of the function’s derivatives at a single point.
#include<stdio.h>
#include<math.h>
int n,x;
main()
{
printf("Enter the Value for 'X' and 'N' and Upper Limitn");
scanf("%d %d ",&x,&n);
findex();
findsinx();
findcosx();
}
int fact(int n)
{
int factorial;
if (n==0) return 1;
else
if (n==1)
return (1);
else
factorial=n*fact(n-1);
return (factorial);
}
int findex()
{
double sum=0,ex,numr,denr;
int i;
for(i=0;i<=n;i++)
{
numr=pow(x,i);
denr=fact(i);
if(denr==0)
ex=0;
else
{
ex=numr/denr;
}
sum+=ex;
}
printf("ne^x = %lf t %lf n",sum,denr,numr,ex);
}
int numpow()
{
if(n%2==0)
{
return (1);
}
else
return (-1);
}
findsinx()
{
double numr,den,nr,dr,sinx,sum=0;
int i;
for(i=0;i<=n;i++)
{
numr=numpow(i);
den=1+2*i;
nr=numr*pow(x,den);
dr=fact(i);
if (dr==0)
sinx=0;
else
{
sinx=nr/dr;
}
}
printf("nSinX = %lf n",nr,dr,sinx);
}
findcosx()
{
double cosx,nr,dr,den,numr,sum=0;
numr=numpow(n);
den=2*n;
dr=fact(den);
nr=numr*(pow(x,den));
cosx=nr/dr;
printf("nCosX=%lfn",cosx);
}
Recent Comments