Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <iomanip>
- #include <vector>
- using namespace std;
- typedef vector<double> DoubleVector;
- typedef vector<vector<double>> DoubleMatrix;
- #define N_table 9
- #define xcp 0
- #define ycp 1
- #define tillday 2
- #define form 3
- #define a 0
- #define b 1
- #define x 0
- #define y 1
- double average(DoubleVector &arr) {
- double res = 0;
- for (int i = 0; i < arr.size(); i++) res += arr[i];
- return res / arr.size();
- }
- double geometric_mean(DoubleVector &arr) {
- double res = 1;
- for (int i = 0; i < arr.size(); i++) res *= arr[i];
- return pow(res, 1. / arr.size());
- }
- double harmonic_mean(DoubleVector &arr) {
- double res = 0;
- for (int i = 0; i < arr.size(); i++) res += 1 / arr[i];
- return arr.size() / res;
- }
- double calc_tilday(DoubleVector &X, DoubleVector &Y, double _xcp) {
- int tmp_i = 0;
- for (int i = 0; i < X.size(); i++) {
- if (_xcp == X[i]) return Y[i];
- else if (X[i] < _xcp) tmp_i = i;
- }
- return ((_xcp - X[tmp_i]) / (X[tmp_i + 1] - X[tmp_i])) * (Y[tmp_i + 1] - Y[tmp_i]) + Y[tmp_i];
- }
- void print_table(DoubleMatrix& tabl, int i_func) {
- cout << "Dependency selection table: " << endl;
- cout << "| # | xcp | ycp | ~y | abs(ycp - ~y) / ~y" << endl;
- for (int i = 0; i < N_table; i++) {
- cout << "| " << i + 1 << ' ';
- for (int j = 0; j < 4; j++) cout << "| " << fixed << setprecision(3) << tabl[j][i] << " ";
- if (i == i_func) cout << "+";
- cout << endl;
- }
- }
- void print_arr(DoubleVector &arr, char name_arr) {
- cout << name_arr << " = { " << arr[0];
- for (int i = 1; i < arr.size(); i++) cout << ", " << arr[i];
- cout << " }" << endl;
- }
- DoubleMatrix build_table(DoubleVector& X, DoubleVector& Y) {
- unsigned int size = X.size();
- DoubleMatrix res_tabl(4, vector<double> (N_table));
- DoubleVector tmp_res(3);
- tmp_res[0] = average(X);
- tmp_res[1] = geometric_mean(X);
- tmp_res[2] = harmonic_mean(X);
- for (int i = 0; i < 3; i++) for (int j = i; j < N_table; j += 3) res_tabl[xcp][j] = tmp_res[i];
- tmp_res[0] = average(Y);
- tmp_res[1] = geometric_mean(Y);
- tmp_res[2] = harmonic_mean(Y);
- for (int i = 0; i < size; i += 3) for (int j = i; j < N_table; j++) res_tabl[ycp][j] = tmp_res[i / 3];
- for (int i = 0; i < N_table; i++) {
- res_tabl[tillday][i] = calc_tilday(X, Y, res_tabl[xcp][i]);
- res_tabl[form][i] = fabs((res_tabl[ycp][i] - res_tabl[tillday][i]) / res_tabl[tillday][i]);
- }
- return res_tabl;
- }
- int min(DoubleVector &arr) {
- int min = 0;
- for (int i = 0; i < arr.size(); i++)
- if (arr[min] > arr[i]) min = i;
- return min;
- }
- DoubleMatrix func(DoubleVector &X, DoubleVector &Y, int i_func) {
- unsigned int size = X.size();
- DoubleMatrix res(2, vector<double> (size));
- if (i_func == 0)
- for (int i = 0; i < size; i++) {
- res[y][i] = Y[i];
- res[x][i] = X[i];
- }
- else if (i_func == 1)
- for (int i = 0; i < size; i++) {
- res[y][i] = Y[i];
- res[x][i] = 1. / X[i];
- }
- else if (i_func == 2)
- for (int i = 0; i < size; i++) {
- res[y][i] = Y[i];
- res[x][i] = log(X[i]);
- }
- else if (i_func == 3)
- for (int i = 0; i < size; i++) {
- res[y][i] = log(Y[i]);
- res[x][i] = X[i];
- }
- else if (i_func == 4)
- for (int i = 0; i < size; i++) {
- res[y][i] = log(Y[i]);
- res[x][i] = log(X[i]);
- }
- else if (i_func == 5)
- for (int i = 0; i < size; i++) {
- res[y][i] = log(Y[i]);
- res[x][i] = 1. / X[i];
- }
- else if (i_func == 6)
- for (int i = 0; i < size; i++) {
- res[y][i] = 1. / Y[i];
- res[x][i] = X[i];
- }
- else if (i_func == 7)
- for (int i = 0; i < size; i++) {
- res[y][i] = 1. / Y[i];
- res[x][i] = log(X[i]);
- }
- else if (i_func == 8)
- for (int i = 0; i < size; i++) {
- res[y][i] = 1. / Y[i];
- res[x][i] = 1. / X[i];
- }
- else cout << "ERROR: i_func = " << i_func;
- return res;
- }
- DoubleVector calc_coef(DoubleVector &X, DoubleVector &Y, int i_func) {
- unsigned int size = X.size();
- DoubleVector coef(2);
- DoubleMatrix newXY = func(X, Y, i_func);
- double sumxy = 0,
- sumx = 0,
- sumy = 0,
- sumx2 = 0;
- for (int i = 0; i < size; i++) {
- sumxy += (newXY[x][i] * newXY[y][i]);
- sumx += newXY[x][i];
- sumy += newXY[y][i];
- sumx2 += (newXY[x][i] * newXY[x][i]);
- }
- coef[a] = (sumxy - (1. / size) * sumx * sumy) / (sumx2 - (1. / size) * sumx * sumx);
- coef[b] = (1. / size) * sumy - (coef[a] / size) * sumx;
- if (i_func == 3) {
- coef[a] = exp(coef[a]);
- coef[b] = exp(coef[b]);
- }
- else if (i_func == 4) coef[b] = exp(coef[b]);
- return coef;
- }
- void print_func(DoubleVector coef, int i_func) {
- cout << "Function MNK: " << endl;
- if (i_func == 0) cout << "y = " << coef[b] << " + " << coef[a] << " * x" << endl;
- else if (i_func == 1) cout << "y = " << coef[b] << " + " << coef[a] << " / x" << endl;
- else if (i_func == 2) cout << "y = " << coef[b] << " + " << coef[a] << " * ln(x)" << endl;
- else if (i_func == 3) cout << "y = " << coef[b] << " * " << coef[a] << "^x" << endl;
- else if (i_func == 4) cout << "y = " << coef[b] << " * x^" << coef[a] << endl;
- else if (i_func == 5) cout << "y = exp(" << coef[b] << " + " << coef[a] << " / x)" << endl;
- else if (i_func == 6) cout << "y = 1 / (" << coef[b] << " + " << coef[a] << " * x" << endl;
- else if (i_func == 7) cout << "y = 1 / (" << coef[b] << " + " << coef[a] << " * ln(x)" << endl;
- else if (i_func == 8) cout << "y = x(" << coef[b] << " + " << coef[a] << " * x)" << endl;
- else cout << "ERROR: i_func = " << i_func;
- }
- void go(DoubleVector& X, DoubleVector& Y) {
- if (X.size() != Y.size()) {
- cout << "ERROR: array sizes are not the same." << endl;
- cout << "size X = " << X.size() << "\tsize Y = " << Y.size() << endl;
- return;
- }
- DoubleMatrix table;
- table = build_table(X, Y);
- print_arr(X, 'X');
- print_arr(Y, 'Y');
- cout << endl;
- int i_func = min(table[3]);
- print_table(table, i_func);
- DoubleVector coef = calc_coef(X, Y, i_func);
- cout << endl;
- print_func(coef, i_func);
- }
- int main() {
- // площа зібрана, тис.га / harvested area, thsd.ha
- cout << "\n\tharvested area, thsd.ha\n";
- DoubleVector X1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
- DoubleVector Y1 = {165.5, 183.3, 179, 144.9, 137.4, 142.2, 94.7, 117.5, 155.6, 138.5, 133.3};
- go(X1, Y1);
- // обсяг виробництва, міл.ц / volume of production, mil.centner
- cout << "\n\tvolume of production, mil.centner\n";
- DoubleVector X2 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
- DoubleVector Y2 = {4.9621, 6.8285, 8.1169, 7.469, 6.1961, 6.0782, 4.7459, 6.0993, 7.9461, 7.5925, 6.5105};
- go(X2, Y2);
- // урожайність, ц з 1 га зібраної площі / yield, centner per ha of the harvested area
- cout << "\n\tyield, centner per ha of the harvested area\n";
- DoubleVector X3 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
- DoubleVector Y3 = {30, 37.2, 45.4, 51.5, 45.1, 42.7, 50.1, 51.9, 51.1, 54.8, 48.8};
- go(X3, Y3);
- return 0;
- }
Add Comment
Please, Sign In to add comment