mishavi

ЛР 3/Метод наименьших квадратов

May 10th, 2017
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.95 KB | None | 0 0
  1.  
  2. #include "stdafx.h"
  3. #include <iostream>
  4. #include <stdio.h>
  5.  
  6. #include <stdlib.h>
  7.  
  8. #include <cmath>
  9. #include <iomanip>
  10. #include <vector>
  11. #define _USE_MATH_DEFINES
  12. #include <math.h>
  13. #define _CRT_SECURE_NO_WARNINGS
  14.  
  15. using namespace std;
  16. int m;
  17. double ** getData(int a, int b, int m)
  18. {
  19.    
  20.    
  21.  
  22.     double **f = new double*[2];
  23.     f[0] = new double[m];
  24.     f[1] = new double[m];
  25.  
  26.     /*apr = new double[u];
  27.     pog = new double[u];
  28.     double e = 0;*/
  29.  
  30.     // x[i] = a + (i - 1) * (b - a) / (m - 1)
  31.     // i = 1, m
  32.     // f(x[j]0, phi 9x[j],c)
  33.     // d(xj) f (xj) (xj,c)
  34.  
  35.  
  36.  
  37.  
  38.     for (int i = 1; i <= m; i++)
  39.     {
  40.         f[0][i - 1] = a + (double)(i - 1) * (b - a) / (m - 1); //x[i]
  41.         f[1][i - 1] = (double)4 * f[0][i - 1] - (double)7 * sin(f[0][i - 1]); //y[i]
  42.         printf("%5.3lf - %7.3lf\n", f[0][i - 1], f[1][i - 1]);
  43.     }
  44.     return f;
  45.     for (int j = 0; j <21; j++)
  46.  
  47.     {
  48.         long double **g;
  49.         g = new long double*[2];
  50.         g[1] = new long double[21];
  51.         g[2] = new long double[21];
  52.         g[1][j] = a + j*(b - a) / 20;
  53.         g[2][j] = pow(f[1][j], 2.0);
  54.         /*for (int i = 0; i <=2; ++i)
  55.         {
  56.             g[2][j] = f[1];
  57.         }*/
  58.     }
  59. }
  60.  
  61. // Вычисление коэффициентов аппроксимирующей прямой
  62. void getApprox(double **x, double *a, double *b, int n)
  63. {
  64.     double sumx = 0;
  65.     double sumy = 0;
  66.     double sumx2 = 0;
  67.     double sumxy = 0;
  68.     for (int i = 0; i<n; i++) {
  69.         sumx += x[0][i];
  70.         sumy += x[1][i];
  71.         sumx2 += x[0][i] * x[0][i];
  72.         sumxy += x[0][i] * x[1][i];
  73.     }
  74.     *a = (n*sumxy - (sumx*sumy)) / (n*sumx2 - sumx*sumx);
  75.     *b = (sumy - *a*sumx) / n;
  76.     return;
  77. }
  78.  
  79.  
  80. int main()
  81.  
  82. {
  83.     double **x, a, b;
  84.     int A, B, M, N;
  85.     double **f;
  86.     f = new double *[2];
  87.     f[0] = new double[11];
  88.     f[1] = new double[11];
  89.  
  90.     int j;
  91.  
  92.     system("chcp 1251");
  93.     system("cls");
  94.  
  95.     printf("Input A: ");
  96.     scanf_s("%d", &A);
  97.  
  98.     printf("Input B: ");
  99.     scanf_s("%d", &B);
  100.  
  101.     printf("Input M: ");
  102.     scanf_s("%d", &M);
  103.  
  104.     printf("Input N: ");
  105.     scanf_s("%d", &N);
  106.  
  107.     x = getData(A, B, M);
  108.  
  109.     std::cout << "\n\n\tf(x)" << "\t\tapr(x)" << "\tpog" << std::endl;
  110.    
  111.     /*bila здесь ошибка в цикле*/
  112.     for (int i = 0; i <21; i++)
  113.     {
  114.  
  115.  
  116.         double **g;
  117.         g = new double*[2];
  118.         g[1] = new double[21];
  119.         g[2] = new double[21];
  120.         double *apr, *pog;
  121.         apr = new double[21];
  122.         pog = new double[21];
  123.  
  124.         double e = 0;
  125.         for (int j = 0; j < 11; j++)
  126.  
  127.   /*double **f;
  128.         double *f = new double *[2];
  129.         f[0] = new double[m];
  130.         f[1] = new double[m];
  131.  
  132.         int j;*/
  133.        
  134.         if ((g[2][i] >= f[0][j]) && (g[2][i] <= f[0][j + 1]))
  135.         {
  136.            
  137.  
  138.             apr[i] = f[1][j] + (g[1][i] - f[0][j])*(f[1][j + 1] - f[1][j]) / (f[0][j + 1] - f[0][j]);
  139.             pog[i] = g[2][i] - apr[i];
  140.         }
  141.        
  142.  
  143.         if ((i >= 14 && i <= 17) || (i >= 19 && i <= 20))
  144.  
  145.  
  146.             std::cout << "\n" << i + 1 << ":" << setw(15) << g[2][i] << setw(15) << apr[i] << setw(15) << pog[i];
  147.         else
  148.             std::cout << "\n" << i + 1 << ":" << setw(15) << g[2][i] << setw(15) << apr[i] << setw(15) << pog[i];
  149.         std::cout << "\n";
  150.         system("pause");
  151.     }
  152.  
  153.  
  154. }
Advertisement
Add Comment
Please, Sign In to add comment