-
Notifications
You must be signed in to change notification settings - Fork 0
/
newton_forward_interpolation.c
169 lines (156 loc) · 3.72 KB
/
newton_forward_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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double eval_pol(double *pol, int degree, double point)
{
double ans=0;
for(int i=degree; i>=0; i--)
ans = ans * point + pol[i];
return ans;
}
int fact(int n)
{
if(n==0)
return 1;
else
return n*fact(n-1);
}
void free2(double **arr, int m, int n)
{
for(int i=0; i<m; i++)
free(arr[i]);
free(arr);
}
double *allocate(int n)
{
double *p = (double*)malloc(n*sizeof(double));
for(int i=0; i<n; i++)
p[i] = 0;
return p;
}
double **allocate2(int m, int n)
{
double **arr = (double **)malloc(m*sizeof(double *));
for(int i=0; i<m; i++)
arr[i] = allocate(n);
return arr;
}
int main()
{
printf("Enter in format\nFirst line >> size\nFollowing 2 lines >> X and Y\n\n");
int n;
scanf("%d", &n);
double *X = allocate(n), *Y = allocate(n), *theta = allocate(n), *coeff = allocate(n);
double **A = allocate2(n,n), **tmp = allocate2(n,n), **d = allocate2(n,n);
float m, prod=1, h;
for(int i=0; i<n; i++)
scanf("%lf", &X[i]);
for(int i=0; i<n; i++)
scanf("%lf", &Y[i]);
h = X[1] - X[0];
//Calculating equation
for(int j=0;j<n-1;j++)
{
for(int i=0;i<(n-j);i++)
{
if(j==0)
d[i][j]=Y[i+1]-Y[i];
else
d[i][j]=d[i+1][j-1]-d[i][j-1];
}
}
theta[0] = Y[0];
for(int i=1; i<n; i++)
{
m = fact(i);
theta[i] = d[0][i-1]/(m*pow(h,i));
}
printf("\nTheta values...\n");
for(int i=0; i<n; i++)
printf("%7.7lf ", theta[i]);
printf("\n");
//Calculating approximated polynomial
for(int i=0; i<n; i++)
if(i==0)
A[0][i]=1;
else
A[0][i]=-X[i-1];
for(int i=0; i<n; i++)
if(i==0)
A[1][i]=0;
else
A[1][i]=1;
for(int i=2; i<n; i++)
for(int j=0; j<n; j++)
A[i][j]=0;
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
tmp[i][j]=A[i][j];
for(int i=1; i<n-1; i++)
{
for(int j=i+1; j<n; j++)
{
double *t_tmp=(double *)malloc((2+i)*sizeof(double));
for(int k=0; k<2+i; k++)
{
t_tmp[k]=tmp[k][j];
tmp[k][j]=0;
}
for(int l=0; l<=1; l++)
for(int k=0; k+l<2+i; k++)
tmp[k+l][j] += A[l][i]*t_tmp[k];
free(t_tmp);
}
}
for(int i=0; i<n; i++)
{
coeff[i]=0;
for(int j=i; j<n; j++)
coeff[i] = coeff[i] + theta[j]*tmp[i][j];
}
printf("\nRequired coefficients are...\n");
for(int i=0; i<n; i++)
printf("x^%d ... %lf\n", i, coeff[i]);
printf("\nComparing given values and computed values...\n");
printf("Given_X\t\tGiven_Y\t\tComputed_Y\n");
for(int i=0; i<n; i++)
printf("| %lf\t| %lf\t| %lf\n", X[i], Y[i], eval_pol(coeff, n-1, X[i]));
free(Y); free(theta);
free2(A, n, n); free2(tmp, n, n);free2(d, n, n);
double x = 0;
printf("\nEnter X values to be interpolated. Keep within valid range(%lf to %lf).", X[0], X[n-1]);
while(1)
{ printf("\n\nX ? ");
scanf("%lf", &x);
printf("Y = %7.7lf", eval_pol(coeff, n-1, x));
}
free(X); free(coeff);
}
// OUTPUT...
// Enter in format
// First line >> size
// Following 2 lines >> X and Y
// 5
// 1891 1901 1911 1921 1931
// 46 66 81 93 101
// Theta values...
// 46.0000000 2.0000000 -0.0250000 0.0003333 -0.0000125
// Required coefficients are...
// x^0 ... -167340852.636012
// x^1 ... 349907.925967
// x^2 ... -274.385575
// x^3 ... 0.095633
// x^4 ... -0.000013
// Comparing given values and computed values...
// Given_X Given_Y Computed_Y
// | 1891.000000 | 46.000000 | 46.000000
// | 1901.000000 | 66.000000 | 66.000000
// | 1911.000000 | 81.000000 | 81.000000
// | 1921.000000 | 93.000000 | 93.000000
// | 1931.000000 | 101.000000 | 101.000000
// Enter X values to be interpolated. Keep within valid range(1891.000000 to 1931.000000).
// X ? 1899
// Y = 62.5168001
// X ? 1922
// Y = 94.0224875
// X ? ^C