-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlangrage_interpolation.c
63 lines (55 loc) · 1.6 KB
/
langrage_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
// #include<stdio.h>
// int main(){
// int n;
// printf("Enter value of n: ");
// scanf("%d",&n);
// float x,xarr[n],farr[n],prodfun;
// printf("Enter value of x at which point is to be interpolated: ");
// scanf("%f",&x);
// for (int i = 1; i <= n; i++)
// {
// printf("Enter value of x[%d] and f[%d]: ",i,i);
// scanf("%f %f",&xarr[i],&farr[i]);
// }
// double sum = 0;
// for (int i = 1; i <= n; i++)
// {
// prodfun = 1;
// for (int j = 1; j <= n; j++)
// {
// if (j != i)
// {
// prodfun *= (x - xarr[i]) / (xarr[i] - xarr[j]);
// }
// }
// sum += farr[i] * prodfun;
// }
// printf("At x = %f , functional value = %lf",x,sum);
// return 0;
// }
#include <stdio.h>
int main() {
int n;
float x;
printf("Please enter the value of x(at which value is to be interpolated) and n: ");
scanf("%f %d",&x, &n);
float xVal[n];
float funcVal[n];
//Reading the tabulated values
for(int i=0;i<n;i++){
printf("Enter value of x[%d] and f[%d]\t",i+1,i+1);
scanf("%f %f", &xVal[i], &funcVal[i]);
}
float sum = 0;
for(int i=0;i<n;i++){
float prodFunc = 1;
for(int j=0;j<n;j++){
if(i!=j){
prodFunc = prodFunc*((x-xVal[j])/(xVal[i]-xVal[j]));
}
}
sum = sum + (funcVal[i]*prodFunc);
}
printf("\nThe value of the function is :%f at x=%f",sum, x);
return 0;
}