-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathNewton_Interpolation.c
67 lines (57 loc) · 1.49 KB
/
Newton_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
// Polynomial interpolation using Newton's Interpolation
#include <stdio.h>
#include <math.h>
#define MAX 15
int main()
{
int n;
float xp, fp, sum, pi, x[MAX], f[MAX], a[MAX], d[MAX][MAX];
char q;
printf("\nInput the number of data pairs: ");
scanf("%d", &n);
printf("\nInput data pairs x(i) and values f(i) (one set in each line): ");
for (int i = 1; i <= n; i++)
{
scanf("%f %f", &x[i], &f[i]);
}
/*Construct difference table*/
for (int i = 1; i <= n; i++)
{
d[i][1] = f[i];
}
for (int j = 2; j <= n; j++)
{
for (int i = 1; i <= n - j + 1; i++)
{
d[i][j] = (d[i + 1][j - 1] - d[i][j - 1]) / (x[i + j + 1] - x[i]);
}
}
/* Set the coefficients of interpolation polynomial*/
for (int j = 1; j <= n; j++)
{
a[j] = d[1][j];
}
/*Compute interpolation value*/
do
{
printf("\nInput x at which interpolation is required:");
scanf("%f", &xp);
sum = a[1];
for (int i = 2; i <= n; i++)
{
pi = 1.0;
for (int j = 1; j <= i - 1; j++)
{
pi = pi * (xp - x[j]);
}
sum = sum + a[i] * pi;
}
fp = sum;
/*write results*/
printf("\nInterpolated function value at x=%f is %f.\n", xp, fp);
printf("Do you want to input another value? (y/n): ");
fflush(stdin);
scanf("%c", &q);
} while (q == 'y');
return 0;
}