thespeedracer38

Lagrange Interpolation by Me

Feb 7th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.95 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3.  
  4. using namespace std;
  5.  
  6. class Lagrange{
  7.     double **diff_table;
  8.     int n;
  9.     double *x, *y;
  10.     double x_unknown;
  11.     double y_at_x_un;
  12.     public:
  13.         Lagrange(int);
  14.         ~Lagrange();
  15.         void input();
  16.         void calcDiffTable();
  17.         void calcY();
  18.         void display();
  19. };
  20.  
  21. Lagrange::Lagrange(int n){
  22.     Lagrange::n = n;
  23.     x = new double[n];
  24.     y = new double[n];
  25.     diff_table = new double*[n];
  26.     for(int i = 0; i < n; i++){
  27.         diff_table[i] = new double[n];
  28.     }
  29. }
  30.  
  31. Lagrange::~Lagrange(){
  32.     delete[] x;
  33.     delete[] y;
  34.     for(int i = 0; i < n; i++){
  35.         delete[] diff_table[i];
  36.     }
  37. }
  38.  
  39. void Lagrange::input(){
  40.     for(int i = 0; i < n; i++){
  41.         cout << "x[" << i << "] = ";
  42.         cin >> x[i];
  43.         cout << "y[" << i << "] = ";
  44.         cin >> y[i];
  45.     }
  46.     cout << "Enter unknown x: ";
  47.     cin >> x_unknown;
  48. }
  49.  
  50. void Lagrange::calcDiffTable(){
  51.     // Setting the diagonals
  52.     for(int i = 0; i < n; i++){
  53.         diff_table[i][i] = x_unknown - x[i];
  54.     }
  55.     // Setting rest of the elements
  56.     for(int i = 0; i < n; i++){
  57.         for(int j = 0; j < n; j++){
  58.             if(i != j){
  59.                 diff_table[i][j] = x[i] - x[j];
  60.             }
  61.         }
  62.     }
  63. }
  64.  
  65. void Lagrange::calcY(){
  66.     double l_i;
  67.     y_at_x_un = 0;
  68.     double numerator;
  69.     double denominator;
  70.     for(int i = 0; i < n; i++){
  71.         numerator = 1;
  72.         denominator = 1;
  73.         for(int j = 0; j < n; j++){
  74.             if(i != j){
  75.                 numerator = numerator * diff_table[j][j];
  76.                 denominator = denominator * diff_table[i][j];
  77.             }
  78.         }
  79.         l_i = numerator / denominator;
  80.         y_at_x_un += (l_i * y[i]);
  81.     }
  82. }
  83.  
  84. void Lagrange::display(){
  85.     calcDiffTable();
  86.     cout << "\nElements in the Lagranges Difference Table" << endl;
  87.     for(int i = 0; i < n; i++){
  88.         for(int j = 0; j < n; j++){
  89.             cout << diff_table[i][j] << "\t";
  90.         }
  91.         cout << endl;
  92.     }
  93.     cout << endl;
  94.     calcY();
  95.     cout << "Unknown y-value = " << y_at_x_un << endl;
  96. }
  97.  
  98. int main() {
  99.     system("cls");
  100.     cout << "Enter the number of points: ";
  101.     int n;
  102.     cin >> n;
  103.     Lagrange L(n);
  104.     L.input();
  105.     L.display();
  106.     return 0;
  107. }
Add Comment
Please, Sign In to add comment