C Program for Newton Forward Interpolation
Newton's forward interpolation formula is used for equal interval. suppose y=f(x) function with equal interval of independent argument is x0,x1,x2 …….xn and corresponding value if y is y0,y1,y2 …….yn
where xr- xr-1=h
Formula :
u=(x-X0 )/h
Example :
X | 0 | 1 | 2 | 3 | 4 |
---|---|---|---|---|---|
f(x) | 1 | 1.5 | 2.2 | 3.1 | 4.3 |
Find the value of f(1.2) using the following table.
Difference table:
x | y=f(x) | Δy | Δ2 y |
---|---|---|---|
0 | 1 | ||
0.5 | |||
1 | 1.5 | 0.2 | |
0.7 | |||
2 | 2.2 | 0.2 | |
0.9 | |||
3 | 3.1 | 0.3 | |
1.2 | |||
4 | 4.3 |
C program:
//newton's forward interpolation
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
float x[10],y[10][10],h,u1,u,fx,fy,fact;
int i,j,n,ch=30;
printf("How many terms you want to enter : ");
scanf("%d",&n);
printf("Enter the value of X and Y (X,Y)");
for(i=0;i<n;i++)
{
scanf("%f,%f",&x[i],&y[i]);
}
for(j=1;j<n;j++)
{
for(i=0;i<n-j;i++)
{
y[i][j]=y[i+1][j-1]-y[i][j-1];
}
}
printf("-------Difference Table-------\n");
printf("X Y");
for(i=0;i<n-1;i++)
printf("\t%c^%d",ch,i+1);
printf("\n");
for(i=0;i<n;i++)
{
printf("%.2f",x[i]);
j=0;
while(j<n-i)
{
printf(" %.3f",y[i][j]);
j++;
}
printf("\n");
}
printf("\nEnter the value of x for which you wants to find Y : ");
scanf("%f",&fx);
h=x[1]-x[0];
u=(fx-x[0])/h;
fy=y[0][0];
u1=u;
fact=1;
for(i=1;i<n;i++)
{
fy=fy+(u1*y[0][i])/fact;
u1=u1*(u1-i);
fact=fact*(i+1);
}
printf("\n Y(%f)=%.3f",fx,fy);
}
Post a Comment
0 Comments