-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13.Lagrange Interpolation.cpp
More file actions
69 lines (58 loc) · 1.11 KB
/
13.Lagrange Interpolation.cpp
File metadata and controls
69 lines (58 loc) · 1.11 KB
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
#include<bits/stdc++.h>
using namespace std;
double x[100],f[100];
int n;
double numeritor(int i, double value)
{
double mul = 1;
for(int j = 1; j <= n; j++)
{
if(j != i)
{
mul = mul * (value - x[j]);
}
}
return mul;
}
double denominator(int i)
{
double mul = 1;
for(int j = 1; j <= n; j++)
{
if(j != i)
{
mul = mul * (x[i] - x[j]);
}
}
return mul;
}
double Lagrange(double value)
{
double sum;
for(int i = 1; i<=n; i++)
{
sum = sum + (f[i] * ((numeritor(i,value)) / denominator(i)));
}
return sum;
}
int main()
{
double value;
freopen("in.txt","r",stdin);
cout<<"Enter the Number of Elements :"<<endl;
cin>>n;
cout<<"Enter the Value of x axis "<<endl;
for(int i = 1; i <=n; i++)
{
cin>>x[i];
}
cout<<"Enter the Value of f(x) "<<endl;
for(int i = 1; i <=n; i++)
{
cin>>f[i];
}
cout<<"Enter the value of x ";
cin>>value;
cout<<endl<<"Result is : "<<Lagrange(value);
return 0;
}