Friday, July 20, 2012

C Program for Taylor Series

/*Taylor Series*/

/*ax^n+bx^(n-1)+..........*/

#include<stdio.h>

#include<conio.h>

#include<math.h>

#include<alloc.h>

float **m,x,n,**o,*f;

float value(float);

float sum(float);

float deri();

int fact(int);

void main()

{

int i,p;

float h,ea;

clrscr();

printf("Enter the Degree of the Equation");

scanf("%f",&n);

puts("--------------------------------------------------");

m=(float**)malloc((n+1)*2)*sizeof(float));

f=(float*)malloc(n*4);

printf("Enter the value of coefficient\n");

p=n;

for(i=0;i<=n;i++)

{

scanf("%f",&m[i][0]);

m[i][1]=p;

p--;

}

puts("--------------------------------------------------");

printf("Enter value of x\n");

scanf("%f",&x);

puts("--------------------------------------------------");

printf("Enter value of h\n");

scanf("%f",&h);

puts("-------------------------------------------------");

for(i=0;i<=n;i++)

{

if(i==0)

f[i]=value(x);

else

f[i]=deri();

printf("\nf%d(x)=%f",i,f[i]);




}

puts("\n-----------------------------------------------");

printf("\nResult=f(x+h)=%f",sum(h));

puts("\n-----------------------------------------------");

getch();

}

float sum(float h)

{

int i;

float s=0;

for(i=0;i<=n;i++)

{

s=s+pow(h,i)*f[i]/fact(i);

}

return s;

}




float value(float x)

{

int i;

float v=0;

for(i=0;i<=n;i++)

{

v=v+m[i][0]*pow(x,m[i][1]);

}

return v;

}

float deri()

{

float v=0;

int i,t;

for(i=0;i<=n;i++)

{

t=m[i][1];

m[i][1]--;

m[i][0]=m[i][0]*t;

if(m[i][1]<0)

break;

v=v+m[i][0]*pow(x,m[i][1]);

}

return v;

}

int fact(int i)

{

int j,fact=1;

for(j=i;j>0;j--)

{

fact=fact*j;

}

return fact;

}







Output of Program:-

First:-

Enter the Degree of the Equation2

------------------------------------------------------

Enter the value of coefficient

1 1 1

-------------------------------------------------------

Enter value of x

2

------------------------------------------------------

Enter value of h

1

------------------------------------------------------




f0(x)=7.000000

f1(x)=5.000000

f2(x)=2.000000

-------------------------------------------------------




Result=f(x+h)=13.000000

-------------------------------------------------------


=============================


Second:-

Enter the Degree of the Equation

3

------------------------------------------------------

Enter the value of coefficient

2.2 -5.3 6.2 2.5

-------------------------------------------------------

Enter value of x

2

------------------------------------------------------

Enter value of h

1

------------------------------------------------------




f0(x)=11.299999

f1(x)=11.400001

f2(x)=15.800001

f3(x)=13.200001

-------------------------------------------------------




Result=f(x+h)=32.800003

-------------------------------------------------------





No comments:

Post a Comment