-
Notifications
You must be signed in to change notification settings - Fork 1
/
backward interpolation.c
56 lines (49 loc) · 1.11 KB
/
backward interpolation.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j;
float arrx[10];
float arry[10][10];
printf("Enter the number of observations");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the observation value at %d",(i+1));
scanf("%f",&arrx[i]);
}
for(j=0;j<n;j++)
{
printf("Enter the observation value of y at %d",(j+1));
scanf("%f",&arry[0][j]);
}
for(i=1;i<n;i++)
{
for(j=i;j<n;j++)
{
arry[i][j]=arry[i-1][j]-arry[i-1][j-1];
}
}
printf("then the difference table is\n");
for(j=0;j<n;j++)
{
printf("%f",arrx[j]); printf("\t");
for(i=0;i<=j;i++){
printf("%f\t",arry[i][j]);
}
printf("\n");
}
float x;
printf("enter the x for which you want to find f(x)");
scanf("%f",&x);
float fx;
float h=arrx[1]-arrx[0];
float p=(x-arrx[n-1])/h;
printf("p=%f",p);
fx=arry[0][n-1]+p*arry[1][n-1]+(p*(p+1)/2*arry[2][n-1])+(p*(p+1)*(p+2)/6*arry[3][n-1]);
if(n>4)
fx+=(p*(p+1)*(p+2)*(p+3)/24*arry[4][n-1]);
if(n>5)
fx+=(p*(p+1)*(p+2)*(p+3)*(p+4)/120*arry[5][n-1]);
printf("\nso fx=%f",fx);
}