DacCum

MNK

Apr 10th, 2022 (edited)
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.84 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <iomanip>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. typedef vector<double> DoubleVector;
  9. typedef vector<vector<double>> DoubleMatrix;
  10.  
  11. #define N_table 9
  12. #define xcp 0
  13. #define ycp 1
  14. #define tillday 2
  15. #define form 3
  16. #define a 0
  17. #define b 1
  18. #define x 0
  19. #define y 1
  20.  
  21. double average(DoubleVector &arr) {
  22.     double res = 0;
  23.     for (int i = 0; i < arr.size(); i++) res += arr[i];
  24.  
  25.     return res / arr.size();
  26. }
  27.  
  28. double geometric_mean(DoubleVector &arr) {
  29.     double res = 1;
  30.     for (int i = 0; i < arr.size(); i++) res *= arr[i];
  31.  
  32.     return pow(res, 1. / arr.size());
  33. }
  34.  
  35. double harmonic_mean(DoubleVector &arr) {
  36.     double res = 0;
  37.     for (int i = 0; i < arr.size(); i++) res += 1 / arr[i];
  38.  
  39.     return arr.size() / res;
  40. }
  41.  
  42. double calc_tilday(DoubleVector &X, DoubleVector &Y, double _xcp) {
  43.     int tmp_i = 0;
  44.     for (int i = 0; i < X.size(); i++) {
  45.         if (_xcp == X[i]) return Y[i];
  46.         else if (X[i] < _xcp) tmp_i = i;
  47.     }
  48.  
  49.     return ((_xcp - X[tmp_i]) / (X[tmp_i + 1] - X[tmp_i])) * (Y[tmp_i + 1] - Y[tmp_i]) + Y[tmp_i];
  50. }
  51.  
  52. void print_table(DoubleMatrix& tabl, int i_func) {
  53.     cout << "Dependency selection table: " << endl;
  54.     cout << "| # |    xcp    |    ycp    |    ~y    |    abs(ycp - ~y) / ~y" << endl;
  55.     for (int i = 0; i < N_table; i++) {
  56.         cout << "| " << i + 1 << ' ';
  57.         for (int j = 0; j < 4; j++) cout << "| " << fixed << setprecision(3) << tabl[j][i] << "     ";
  58.         if (i == i_func) cout << "+";
  59.         cout << endl;
  60.     }
  61. }
  62.  
  63. void print_arr(DoubleVector &arr, char name_arr) {
  64.     cout << name_arr << " = { " << arr[0];
  65.     for (int i = 1; i < arr.size(); i++) cout << ", " << arr[i];
  66.     cout << " }" << endl;
  67. }
  68.  
  69. DoubleMatrix build_table(DoubleVector& X, DoubleVector& Y) {
  70.     unsigned int size = X.size();
  71.     DoubleMatrix res_tabl(4, vector<double> (N_table));
  72.  
  73.     DoubleVector tmp_res(3);
  74.     tmp_res[0] = average(X);
  75.     tmp_res[1] = geometric_mean(X);
  76.     tmp_res[2] = harmonic_mean(X);
  77.     for (int i = 0; i < 3; i++) for (int j = i; j < N_table; j += 3) res_tabl[xcp][j] = tmp_res[i];
  78.  
  79.     tmp_res[0] = average(Y);
  80.     tmp_res[1] = geometric_mean(Y);
  81.     tmp_res[2] = harmonic_mean(Y);
  82.     for (int i = 0; i < size; i += 3) for (int j = i; j < N_table; j++) res_tabl[ycp][j] = tmp_res[i / 3];
  83.  
  84.     for (int i = 0; i < N_table; i++) {
  85.         res_tabl[tillday][i] = calc_tilday(X, Y, res_tabl[xcp][i]);
  86.         res_tabl[form][i] = fabs((res_tabl[ycp][i] - res_tabl[tillday][i]) / res_tabl[tillday][i]);
  87.     }
  88.  
  89.     return res_tabl;
  90. }
  91.  
  92. int min(DoubleVector &arr) {
  93.     int min = 0;
  94.     for (int i = 0; i < arr.size(); i++)
  95.         if (arr[min] > arr[i]) min = i;
  96.  
  97.     return min;
  98. }
  99.  
  100. DoubleMatrix func(DoubleVector &X, DoubleVector &Y, int i_func) {
  101.     unsigned int size = X.size();
  102.     DoubleMatrix res(2, vector<double> (size));
  103.     if (i_func == 0)
  104.         for (int i = 0; i < size; i++) {
  105.             res[y][i] = Y[i];
  106.             res[x][i] = X[i];
  107.         }
  108.     else if (i_func == 1)
  109.         for (int i = 0; i < size; i++) {
  110.             res[y][i] = Y[i];
  111.             res[x][i] = 1. / X[i];
  112.         }
  113.     else if (i_func == 2)
  114.         for (int i = 0; i < size; i++) {
  115.             res[y][i] = Y[i];
  116.             res[x][i] = log(X[i]);
  117.         }
  118.     else if (i_func == 3)
  119.         for (int i = 0; i < size; i++) {
  120.             res[y][i] = log(Y[i]);
  121.             res[x][i] = X[i];
  122.         }
  123.     else if (i_func == 4)
  124.         for (int i = 0; i < size; i++) {
  125.             res[y][i] = log(Y[i]);
  126.             res[x][i] = log(X[i]);
  127.         }
  128.     else if (i_func == 5)
  129.         for (int i = 0; i < size; i++) {
  130.             res[y][i] = log(Y[i]);
  131.             res[x][i] = 1. / X[i];
  132.         }
  133.     else if (i_func == 6)
  134.         for (int i = 0; i < size; i++) {
  135.             res[y][i] = 1. / Y[i];
  136.             res[x][i] = X[i];
  137.         }
  138.     else if (i_func == 7)
  139.         for (int i = 0; i < size; i++) {
  140.             res[y][i] = 1. / Y[i];
  141.             res[x][i] = log(X[i]);
  142.         }
  143.     else if (i_func == 8)
  144.         for (int i = 0; i < size; i++) {
  145.             res[y][i] = 1. / Y[i];
  146.             res[x][i] = 1. / X[i];
  147.         }
  148.     else cout << "ERROR: i_func = " << i_func;
  149.  
  150.     return res;
  151. }
  152.  
  153. DoubleVector calc_coef(DoubleVector &X, DoubleVector &Y, int i_func) {
  154.     unsigned int size = X.size();
  155.     DoubleVector coef(2);
  156.     DoubleMatrix newXY = func(X, Y, i_func);
  157.  
  158.     double sumxy = 0,
  159.         sumx = 0,
  160.         sumy = 0,
  161.         sumx2 = 0;
  162.     for (int i = 0; i < size; i++) {
  163.         sumxy += (newXY[x][i] * newXY[y][i]);
  164.         sumx += newXY[x][i];
  165.         sumy += newXY[y][i];
  166.         sumx2 += (newXY[x][i] * newXY[x][i]);
  167.     }
  168.  
  169.     coef[a] = (sumxy - (1. / size) * sumx * sumy) / (sumx2 - (1. / size) * sumx * sumx);
  170.     coef[b] = (1. / size) * sumy - (coef[a] / size) * sumx;
  171.  
  172.     if (i_func == 3) {
  173.         coef[a] = exp(coef[a]);
  174.         coef[b] = exp(coef[b]);
  175.     }
  176.     else if (i_func == 4) coef[b] = exp(coef[b]);
  177.  
  178.     return coef;
  179. }
  180.  
  181. void print_func(DoubleVector coef, int i_func) {
  182.     cout << "Function MNK: " << endl;
  183.     if (i_func == 0) cout << "y = " << coef[b] << " + " << coef[a] << " * x" << endl;
  184.     else if (i_func == 1) cout << "y = " << coef[b] << " + " << coef[a] << " / x" << endl;
  185.     else if (i_func == 2) cout << "y = " << coef[b] << " + " << coef[a] << " * ln(x)" << endl;
  186.     else if (i_func == 3) cout << "y = " << coef[b] << " * " << coef[a] << "^x" << endl;
  187.     else if (i_func == 4) cout << "y = " << coef[b] << " * x^" << coef[a] << endl;
  188.     else if (i_func == 5) cout << "y = exp(" << coef[b] << " + " << coef[a] << " / x)" << endl;
  189.     else if (i_func == 6) cout << "y = 1 / (" << coef[b] << " + " << coef[a] << " * x" << endl;
  190.     else if (i_func == 7) cout << "y = 1 / (" << coef[b] << " + " << coef[a] << " * ln(x)" << endl;
  191.     else if (i_func == 8) cout << "y = x(" << coef[b] << " + " << coef[a] << " * x)" << endl;
  192.     else cout << "ERROR: i_func = " << i_func;
  193. }
  194.  
  195. void go(DoubleVector& X, DoubleVector& Y) {
  196.    
  197.     if (X.size() != Y.size()) {
  198.         cout << "ERROR: array sizes are not the same." << endl;
  199.         cout << "size X = " << X.size() << "\tsize Y = " << Y.size() << endl;
  200.         return;
  201.     }
  202.  
  203.     DoubleMatrix table;
  204.     table = build_table(X, Y);
  205.     print_arr(X, 'X');
  206.     print_arr(Y, 'Y');
  207.     cout << endl;
  208.     int i_func = min(table[3]);
  209.     print_table(table, i_func);
  210.  
  211.  
  212.     DoubleVector coef = calc_coef(X, Y, i_func);
  213.     cout << endl;
  214.     print_func(coef, i_func);
  215.  
  216. }
  217.  
  218. int main() {
  219.  
  220.     // площа зібрана, тис.га / harvested area, thsd.ha
  221.     cout << "\n\tharvested area, thsd.ha\n";
  222.     DoubleVector X1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
  223.     DoubleVector Y1 = {165.5, 183.3, 179, 144.9, 137.4, 142.2, 94.7, 117.5, 155.6, 138.5, 133.3};
  224.     go(X1, Y1);
  225.  
  226.     // обсяг виробництва, міл.ц / volume of production, mil.centner
  227.     cout << "\n\tvolume of production, mil.centner\n";
  228.     DoubleVector X2 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
  229.     DoubleVector Y2 = {4.9621, 6.8285, 8.1169, 7.469, 6.1961, 6.0782, 4.7459, 6.0993, 7.9461, 7.5925, 6.5105};
  230.     go(X2, Y2);
  231.  
  232.     // урожайність, ц з 1 га зібраної площі / yield, centner per ha of the harvested area
  233.     cout << "\n\tyield, centner per ha of the harvested area\n";
  234.     DoubleVector X3 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
  235.     DoubleVector Y3 = {30, 37.2, 45.4, 51.5, 45.1, 42.7, 50.1, 51.9, 51.1, 54.8, 48.8};
  236.     go(X3, Y3);
  237.  
  238.     return 0;
  239. }
  240.  
Add Comment
Please, Sign In to add comment