Advertisement
namkongkirat

Lagrange Interpolation

Dec 23rd, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.55 KB | None | 0 0
  1. #include<iostream>
  2. #include<conio.h>
  3. #include<stdlib.h>
  4.  
  5. using namespace std;
  6.  
  7. int main(){
  8.     float x[30], y[30],X, p, sum=0;
  9.     int n;
  10.    
  11.     cout<<"Enter the no of data you want to input: ";
  12.     cin>>n;
  13.    
  14.     for(int i=0;i<n;i++) {
  15.         cout<<"X ["<<i+1<<"] :";
  16.         cin>>x[i];
  17.         cout<<"Y ["<<i+1<<"] :";
  18.         cin>>y[i];
  19.     }
  20.    
  21.     cout<<"Enter the value for x: ";
  22.     cin>>X;
  23.    
  24.     for(int i=0;i<n;i++){
  25.         p=1;
  26.         for(int j=0;j<n;j++){
  27.             if (i != j){
  28.                 p=p*((X-x[j])/(x[i]-x[j]));
  29.             }
  30.         }
  31.         sum=sum+(p*y[i]);
  32.     }
  33.     cout<<endl<<"The root is: "<<sum;
  34.     getch();
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement