Pages - Menu

Tuesday, 22 July 2014

Implementation of Newton Forward Interpolation Formula in C.

Here is the code..

//AIM: TO FIND THE VALUE USING NEWTON FORWARD INTERPOLATION FORMULA

#include<stdio.h>

#define differ(b,a) (b-a)
#define m 5
int fact(int );
void main()
{
float a[m],fx[m][m],t,u,f;
float fxx=0.0,fxx1=1.0,fxx2=0.0;
int i,j;

printf("NEWTON FORWARD INTERPOLATION FORMULA\n");
printf("enter %d values of x\n",m);
for(i=0;i<m;i++)
scanf("%f",&a[i]);
printf("enter %d values of fx\n",m);
for(i=0;i<m;i++)
scanf("%f",&fx[i][0]);
printf("enter the value across which you want to find value\n");
scanf("%f",&t);
for(i=0;i<m;i++)
{
for(j=i;j<m;j++)
{
fx[j+1][i+1] = differ(fx[j+1][i],fx[j][i]);
}
}
printf("TABLE IS\n");
printf("x :\n");
for(i=0;i<m;i++)
printf("%f\t",a[i]);
printf("fx\t\t$fx\t\t$2fx\t\t$3fx\t\t$4fx\n");
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
if(j<=i)
printf("%f\t",fx[i][j]);
else
printf("\t\t");
}
}
u = ((t-a[0])/(a[1]-a[0]));
for(i=0;i<m-1;i++)
{
fxx1=fxx1*(u-i);
fxx2 = fxx2+((fxx1*fx[i+1][i+1])/(fact(i+1)));
}
fxx = fxx2+fx[0][0];
printf("the value of f(t) is: %f\n",fxx);

}
int fact(int n)
{
int f=1,i;
for(i=n;i>0;i--)
f=f*i;
return f;
}


For Turbo C users.
//AIM: TO FIND THE VALUE USING NEWTON FORWARD //INTERPOLATION FORMULA

#include<stdio.h>
#include<conio.h>
#define differ(b,a) (b-a)
#define m 5
int fact(int );
void main()
{
float a[m],fx[m][m],t,u,f;
float fxx=0.0,fxx1=1.0,fxx2=0.0;
int i,j;
clrscr();
printf("NEWTON FORWARD INTERPOLATION FORMULA\n");
printf("enter %d values of x\n",m);
for(i=0;i<m;i++)
scanf("%f",&a[i]);
printf("enter %d values of fx\n",m);
for(i=0;i<m;i++)
scanf("%f",&fx[i][0]);
printf("enter the value across which you want to find value\n");
scanf("%f",&t);
for(i=0;i<m;i++)
{
for(j=i;j<m;j++)
{
fx[j+1][i+1] = differ(fx[j+1][i],fx[j][i]);
}
}
printf("TABLE IS\n");
printf("x :\n");
for(i=0;i<m;i++)
printf("%f\t",a[i]);
printf("fx\t\t$fx\t\t$2fx\t\t$3fx\t\t$4fx\n");
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
if(j<=i)
printf("%f\t",fx[i][j]);
else
printf("\t\t");
}
}
u = ((t-a[0])/(a[1]-a[0]));
for(i=0;i<m-1;i++)
{
fxx1=fxx1*(u-i);
fxx2 = fxx2+((fxx1*fx[i+1][i+1])/(fact(i+1)));
}
fxx = fxx2+fx[0][0];
printf("the value of f(t) is: %f\n",fxx);
getch();
}
int fact(int n)
{
int f=1,i;
for(i=n;i>0;i--)
f=f*i;
return f;
}


No comments:

Post a Comment