thespeedracer38

Least Square Fit Method by Me

Feb 14th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.84 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <cmath>
  4.  
  5. using namespace std;
  6.  
  7. class LeastSquareFit{
  8.     double **A, *X, *B, *x, *y;
  9.     double *sum_x, *sum_yx;
  10.     int m, n;
  11.     public:
  12.         LeastSquareFit(int, int);
  13.         ~LeastSquareFit();
  14.         void input();
  15.         void calcSumX();
  16.         void calcSumYX();
  17.         void calcAMatrix();
  18.         void calcBMatrix();
  19.         void performGaussElimination();
  20.         void display();
  21. };
  22.  
  23. void LeastSquareFit::performGaussElimination(){
  24.     int i, j, k;
  25.     double multiplication_factor;
  26.     // Obtaining triangular equations
  27.     for(k = 1; k <= n-1; k++){
  28.         for(i = k + 1; i <= n; i++){
  29.             multiplication_factor = -(A[k][k] / A[i][k]);
  30.             for(j = 1; j <= n; j++){
  31.                 A[i][j] = A[k][j] + (multiplication_factor * A[i][j]);
  32.             }
  33.             B[i] = B[k] + (multiplication_factor * B[i]);
  34.         }
  35.     }
  36.     // Applying back substitution to obtain the solution
  37.     X[n] = B[n] / A[n][n];
  38.     double sum;
  39.     for(i = n - 1; i >= 1; i--){
  40.         sum = 0;
  41.         for(j = i + 1; j <= n; j++){
  42.             sum += A[i][j] * X[j];
  43.         }
  44.         X[i] = (B[i] - sum) / A[i][i];
  45.     }
  46. }
  47.  
  48. LeastSquareFit::~LeastSquareFit(){
  49.     delete[] x;
  50.     delete[] y;
  51.     delete[] X;
  52.     delete[] B;
  53.     delete[] sum_x;    
  54.     delete[] sum_yx;
  55.     for(int i = 0; i < (LeastSquareFit::n + 1); i++){
  56.         delete[] A[i];
  57.     }      
  58. }
  59.  
  60. LeastSquareFit::LeastSquareFit(int m, int n){
  61.     LeastSquareFit::m = m;
  62.     LeastSquareFit::n = n;
  63.     x = new double[LeastSquareFit::m];
  64.     y = new double[LeastSquareFit::m];
  65.     X = new double[LeastSquareFit::n + 1];
  66.     B = new double[LeastSquareFit::n + 1];
  67.     A = new double*[LeastSquareFit::n + 1];
  68.     for(int i = 0; i < (LeastSquareFit::n + 1); i++){
  69.         A[i] = new double[LeastSquareFit::n + 1];
  70.     }
  71.     sum_x = new double[(LeastSquareFit::n * LeastSquareFit::n) + 1];
  72.     sum_yx = new double[LeastSquareFit::n + 1];
  73. }
  74.  
  75. void LeastSquareFit::calcSumX(){
  76.     sum_x[0] = 1;
  77.     int limit = n * n;
  78.     for(int i = 1; i <= limit; i++){
  79.         sum_x[i] = 0;
  80.         for(int j = 0; j < m; j++){
  81.             sum_x[i] += pow(x[j], i);
  82.         }
  83.     }
  84. }
  85.  
  86. void LeastSquareFit::calcSumYX(){
  87.     int limit = n + 1;
  88.     for(int i = 0; i < limit; i++){
  89.         sum_yx[i] = 0;
  90.         for(int k = 0; k < m; k++){
  91.             sum_yx[i] += (y[k] * pow(x[k], i));
  92.         }
  93.     }
  94. }
  95.  
  96. void LeastSquareFit::calcAMatrix(){
  97.     A[0][0] = m;
  98.     int rows = n + 1;
  99.     int cols = n + 1;
  100.     for(int i = 0; i < rows; i++){
  101.         for(int j = 0; j < cols; j++){
  102.             if(i != 0 || j != 0){
  103.                 A[i][j] = sum_x[i + j];
  104.             }
  105.         }
  106.     }
  107. }
  108.  
  109. void LeastSquareFit::calcBMatrix(){
  110.     int limit = n + 1;
  111.     for(int i = 0; i < limit; i++){
  112.         B[i] = sum_yx[i];
  113.     }
  114. }
  115.  
  116. void LeastSquareFit::input(){
  117.     for(int i = 0; i < m; i++){
  118.         cout << "x[" << i << "] = ";
  119.         cin >> x[i];
  120.         cout << "y[" << i << "] = ";
  121.         cin >> y[i];
  122.     }
  123. }
  124.  
  125. void LeastSquareFit::display(){
  126.     calcSumX();
  127.     calcSumYX();
  128.     calcAMatrix();
  129.     calcBMatrix();
  130.     cout << "Displaying A matrix\n";
  131.     int rows = n + 1;
  132.     int cols = n + 1;
  133.     for(int i = 0; i < rows; i++){
  134.         for(int j = 0; j < cols; j++){
  135.             cout << A[i][j] << "\t";
  136.         }
  137.         cout << endl;
  138.     }
  139.     cout << "\nDisplaying B matrix\n";
  140.     int limit = n + 1;
  141.     for(int i = 0; i < limit; i++){
  142.             cout << B[i] << "\n";
  143.     }
  144.     cout << "\nDisplaying sum_yx matrix\n";
  145.     for(int i = 0; i < limit; i++){
  146.             cout << sum_yx[i] << ",";
  147.     }
  148.     cout << "\nDisplaying sum_yx matrix\n";
  149.     limit = (n * n) + 1;
  150.     for(int i = 0; i < limit; i++){
  151.             cout << sum_x[i] << ",";
  152.     }
  153.     performGaussElimination();
  154.     cout << "\n\nDisplaying Triangular matrix\n";
  155.     for(int i = 0; i < rows; i++){
  156.         for(int j = 0; j < cols; j++){
  157.             cout << A[i][j] << "\t";
  158.         }
  159.         cout << endl;
  160.     }
  161. }
  162.  
  163. int main(){
  164.     int n, m;
  165.     system("cls");
  166.     cout << "Enter the degree of polynomial: ";
  167.     cin >> n;
  168.     cout << "Enter the number of points in experiment: ";
  169.     cin >> m;
  170.     LeastSquareFit obj(m, n);
  171.     obj.input();
  172.     obj.display();
  173.     cin.ignore();
  174.     cin.get();
  175.     return 0;
  176. }
Add Comment
Please, Sign In to add comment