MeShootIn

Exponential regression

Aug 8th, 2019
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.06 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <utility>
  4. #include <math.h>
  5.  
  6. using namespace std;
  7.  
  8. pair <double, double> exponential_regression(vector <double> & x, vector <double> & y){
  9.     int N = x.size();
  10.    
  11.     double S_x, S_lny, S_x_lny, S_x2;
  12.    
  13.     for(int i = 0; i < N; ++i){
  14.         S_x += x[i];
  15.         S_lny += log(y[i]);
  16.         S_x_lny += x[i] * log(y[i]);
  17.         S_x2 += x[i] * x[i];
  18.     }
  19.    
  20.     double a = (N * S_x_lny - S_x * S_lny) / (N * S_x2 - S_x * S_x);
  21.     double b = (S_x2 * S_lny - S_x * S_x_lny) / (N * S_x2 - S_x * S_x);
  22.    
  23.     return make_pair(a, b);
  24. }
  25.  
  26. int main(){
  27.     cout << "Enter the number of points:\n";
  28.     int N;
  29.     cin >> N;
  30.    
  31.     vector <double> x(N);
  32.     vector <double> y(N);
  33.    
  34.     cout << "Enter " << N << " pairs of coordinates (x, y):\n";
  35.     for(int i = 0; i < N; ++i){
  36.         cin >> x[i] >> y[i];
  37.     }
  38.    
  39.     pair <double, double> line = exponential_regression(x, y);
  40.     cout << "a = " << line.first << ", b = " << line.second << "\n";
  41.    
  42.     system("PAUSE");
  43.     return 0;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment