From 2ae78e0b9101e13a50f2697388293804dcf9ab7e Mon Sep 17 00:00:00 2001 From: Mayankjha997 Date: Sun, 4 Oct 2020 20:20:12 +0530 Subject: [PATCH] Create lagrange_interpolation.cpp --- Graph Algorithms/lagrange_interpolation.cpp | 45 +++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 Graph Algorithms/lagrange_interpolation.cpp diff --git a/Graph Algorithms/lagrange_interpolation.cpp b/Graph Algorithms/lagrange_interpolation.cpp new file mode 100644 index 0000000..e8c2a72 --- /dev/null +++ b/Graph Algorithms/lagrange_interpolation.cpp @@ -0,0 +1,45 @@ +#include + +using namespace std; + +int main() + +{ + + float x[100], y[100], xp, yp=0, p; + int i,j,n; + + cout<<"Enter number of data: "; + cin>>n; + cout<<"Enter data:"<< endl; + + for(i=1;i<=n;i++) + + { + cout<<"x["<< i<<"] = "; + cin>>x[i]; + cout<<"y["<< i<<"] = "; + cin>>y[i]; + } + + cout<<"Enter interpolation point: "; + cin>>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]; + } + cout<< endl<<"Interpolated value at "<< xp<< " is "<< yp; + + return 0; +}