-
Notifications
You must be signed in to change notification settings - Fork 0
/
07_Lagranges_Interpolation.c
55 lines (49 loc) · 994 Bytes
/
07_Lagranges_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
#include <stdio.h>
int main()
{
float x[100], y[100], xp, yp = 0, p;
int i, j, n;
/* Input Section */
printf("Enter number of data: ");
scanf("%d", &n);
printf("Enter data:\n");
for (i = 1; i <= n; i++)
{
printf("x[%d] = ", i);
scanf("%f", &x[i]);
printf("y[%d] = ", i);
scanf("%f", &y[i]);
}
printf("Enter interpolation point: ");
scanf("%f", &xp);
/* Implementing Lagrange Interpolation */
for (i = 1; i <= n; i++)
{
p = 1;
for (j = 1; j <= n; j++)
{
if (i != j)
{
p = p * (xp - x[j]) / (x[i] - x[j]);
}
}
yp = yp + p * y[i];
}
printf("Interpolated value at %.3f is %.3f.\n", xp, yp);
return 0;
}
/*
Output :
Enter number of data: 4
Enter data:
x[1] = 5
y[1] = 12
x[2] = 6
y[2] = 13
x[3] = 9
y[3] = 14
x[4] = 11
y[4] = 16
Enter interpolation point: 10
Interpolated value at 10.000 is 14.667.
*/