Norvager

Интер+Апрок

Nov 11th, 2021 (edited)
704
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.47 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <iostream>
  3. #include <math.h>
  4. #include <cmath>
  5. #include <vector>
  6. using namespace std;
  7. vector<pair<double, double>> func_arg;
  8. double f(double x){
  9.     return log(15.3 * x * x);
  10. }
  11.  
  12. double Lagrange(double point, double left, double right) {
  13.     double L = 0;
  14.     double P;
  15.     for (int i = left; i <= right; i++) {
  16.         P = 1;
  17.         for (int j = left; j <= right; j++) {
  18.             if (j == i) {
  19.                 continue;
  20.             }
  21.             P *= (point - func_arg[j].second) / (func_arg[i].second - func_arg[j].second);
  22.         }
  23.         L += P * func_arg[i].first;
  24.     }
  25.     return L;
  26. }
  27.  
  28. vector<double> findCoefficients() {
  29.     int m = 3;
  30.     vector<vector<double>> matrix(3, vector<double>(3, 0));
  31.     double* B = new double[m];
  32.     B[0] = 0;
  33.     B[1] = 0;
  34.     B[2] = 0;
  35.     for (int i = 0; i < 14; i++) {
  36.         matrix[0][0] += pow(func_arg[i].second, 4);
  37.         matrix[0][1] += pow(func_arg[i].second, 3);
  38.         matrix[0][2] += pow(func_arg[i].second, 2);
  39.         B[0] += func_arg[i].first * pow(func_arg[i].second, 2);
  40.  
  41.         matrix[1][0] += pow(func_arg[i].second, 3);
  42.         matrix[1][1] += pow(func_arg[i].second, 2);
  43.         matrix[1][2] += func_arg[i].second;
  44.         B[1] += func_arg[i].first * func_arg[i].second;
  45.  
  46.         matrix[2][0] += pow(func_arg[i].second, 2);
  47.         matrix[2][1] += func_arg[i].second;
  48.         matrix[2][2] += 1;
  49.         B[2] += func_arg[i].first;
  50.     }
  51.     cout << "Matrix: \n";
  52.     for (int i = 0; i < 3; i++) {
  53.         printf("a*%.4lf + b*%.4lf + c*%.4lf = %.4lf\n", matrix[i][0], matrix[i][1], matrix[i][2], B[i]);
  54.     }
  55.     for (int i = 0; i < 3; i++) {
  56.         double n = matrix[i][i];
  57.         for (int j = 0; j < 3; j++) {
  58.             if (i == j) {
  59.                 matrix[i][j] = 0;
  60.                 continue;
  61.             }
  62.             matrix[i][j] = matrix[i][j] / (-n);
  63.             B[i] /= n;
  64.         }
  65.     }
  66.     double* Res_prev = new double[m];
  67.     memcpy(Res_prev, B, sizeof(double) * 3 + 1);
  68.     double* Res = new double[m];
  69.     double inaccuracy;
  70.     double eps = 0.01;
  71.     while (true) {
  72.         for (int i = 0; i < 3; i++)
  73.             Res[i] = matrix[i][0] * Res_prev[0] + matrix[i][1] * Res_prev[1] + matrix[i][2] * Res_prev[2] + B[i];
  74.  
  75.         inaccuracy = 0;
  76.         for (int i = 0; i < 3; i++) {
  77.             inaccuracy += pow((Res[i] - Res_prev[i]), 2);
  78.         }
  79.         inaccuracy = sqrt(inaccuracy);
  80.         if (inaccuracy < eps) {
  81.             break;
  82.         }
  83.  
  84.         Res_prev = Res;
  85.     }
  86.     printf("\nsearch completed...");
  87.     printf("coefficients:\na = %.4lf, b = %.4lf, c = %.4lf", Res[0], Res[1], Res[2]);
  88.     vector<double> ans;
  89.     ans.push_back(Res[0]);
  90.     ans.push_back(Res[1]);
  91.     ans.push_back(Res[2]);
  92.     return ans;
  93. }
  94.  
  95. double f_apr(vector<double>coefficients, double x) {
  96.     return coefficients[0] * pow(x, 2) + coefficients[1] * x + coefficients[2];
  97. }
  98.  
  99. int main() {
  100.     double xi;
  101.     for (int i = 1; i <= 14; i++) {
  102.         xi = 0.2 * i;
  103.         func_arg.push_back(make_pair(f(xi), xi));
  104.         printf("%d: x = %.2lf  y = %.4lf\n", i-1, func_arg[i-1].second, func_arg[i-1].first);
  105.     }
  106.     vector<double> coefficients = findCoefficients();
  107.  
  108.     double left = 1;
  109.     double right = 3;
  110.     double point = 0.5;
  111.     double node = 0.6;
  112.  
  113.     double z1 = func_arg[13].second + 1;
  114.     double z2 = func_arg[13].second + 5;
  115.  
  116.     printf("\nApproximation/Prediction points:\n%.4lf and %.4lf", z1, z2);
  117.  
  118.     printf("\n\nnode:");
  119.     printf("\nFunction(%.2lf) = %.4lf", node, f(node));
  120.     printf("\nLagrange_polynom(%.2lf) = %.4lf", node, Lagrange(node, left, right));
  121.     printf("\nApproximation(%.2lf) = %.4lf", node, f_apr(coefficients, node));
  122.  
  123.     printf("\n\npoint:");
  124.     printf("\nFunction(%.2lf) = %.4lf", point, f(point));
  125.     printf("\nLagrange_polynom(%.2lf) = %.4lf", point, Lagrange(point, left, right));
  126.     printf("\nApproximation(%.2lf) = %.4lf", point, f_apr(coefficients, point));
  127.  
  128.     printf("\n\nx_apr1:");
  129.     left = 0;
  130.     right = 13;
  131.     printf("\nFunction(%.2lf) = %.4lf", z1, f(z1));
  132.     printf("\nLagrange_polynom(%.2lf) = %.4lf", z1, Lagrange(z1, left, right));
  133.     printf("\nApproximation(%.2lf) = %.4lf", z1, f_apr(coefficients, z1));
  134.  
  135.     printf("\n\nx_apr2:");
  136.     printf("\nFunction(%.2lf) = %.4lf", z2, f(z2));
  137.     printf("\nLagrange_polynom(%.2lf) = %.4lf", z2, Lagrange(z2, left, right));
  138.     printf("\nApproximation(%.2lf) = %.4lf", z2, f_apr(coefficients, z2));
  139.     return 0;
  140. }
Add Comment
Please, Sign In to add comment