Advertisement
dzieciol

interpolacja metoda lagrangea

Oct 21st, 2016
430
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.96 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3.  
  4. /* run this program using the console pauser or add your own getch, system("pause") or input loop */
  5.     using namespace std;
  6.  
  7. int main(int argc, char** argv) {
  8.  
  9.      cout<<"podaj ilość argumentow"<<endl;
  10.      int n;
  11.      cin>>n;
  12.      int x[n];
  13.      int y[n];
  14.      int i;
  15.      for(i=0;i<n;i++){
  16.         cout<<"podaj argument nr "<<i<<endl;
  17.         cin>>x[i];
  18.      }
  19.      for(i=0;i<n;i++){
  20.         cout<<"podaj wynik funkcji "<<i<<endl;
  21.         cin>>y[i];
  22.      }
  23.      int j;
  24.      for (i=0;i<n;i++){
  25.         for(j=i+1;j<n;j++){
  26.             if(x[i]==x[j]){
  27.                 cout<<"podales bledne x"<<endl;
  28.                 return -1;
  29.              }
  30.          }
  31.      }
  32.      double arg;
  33.      double l;
  34.      double w;
  35.      cout<<"podaj argument";
  36.      cin>>arg;
  37.      if(arg>=x[0]&&arg<=x[n-1])
  38.      {
  39.      w=0;
  40.      for (i=0;i<n;i++){
  41.      
  42.      l=1;
  43.         for(j=0;j<n;j++){
  44.            
  45.             if(i!=j){
  46.              l=l*((arg-x[j])/(x[i]-x[j]));
  47.              
  48.              }
  49.              
  50.            
  51.          }w=w+(y[i]*l);
  52.      }
  53.      cout<<w;
  54.      }else cout<<"argument z poza zakresu";
  55.      
  56.    
  57.     return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement