Advertisement
osipyonok

math_mod_lab1

Feb 22nd, 2017
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.32 KB | None | 0 0
  1. #include "stdafx.h"//vs2010
  2.  
  3. #include <bits\stdc++.h>
  4. #include <iostream>
  5. #include <complex>
  6. #include <cassert>
  7. #include <vector>
  8. #include <conio.h>
  9. #include <windows.h>
  10. //#include "decimal.h"
  11. #include "gnuplot_i.hpp"
  12.  
  13. using namespace std;
  14. //using namespace dec;
  15.  
  16. #define M_PI 3.14159265358979323846
  17. #define FOUT
  18. #define cyrillic setlocale(LC_CTYPE, "rus")
  19. #define eps 1e-2
  20.  
  21. inline string dtos(double d){ostringstream strs; strs << d; return strs.str();}
  22.  
  23. void wait(){
  24.     FlushConsoleInputBuffer(GetStdHandle(STD_INPUT_HANDLE));
  25.     #ifndef FOUT
  26.         cout << endl << "Press any key to continue..." << endl;
  27.     #endif
  28.     _getch();
  29. }
  30.  
  31.  
  32. string parse_path(char* path){
  33.     string s = path;
  34.     size_t pos = s.find_last_of('\\');
  35.     if(pos != string::npos){
  36.         return s.substr(0 , pos + 1);
  37.     }
  38.     if(s[s.size() - 1] != '\\')s += '\\';
  39.     return s;
  40. }
  41.  
  42. inline void concat(vector<char> & v , string s){
  43.     v.pop_back();
  44.     for(int i = 0 ; i < s.size() ; ++i){
  45.         v.push_back(s[i]);
  46.     }
  47.     v.push_back('\0');
  48. }
  49.  
  50. vector<double> gauss(vector<vector<double> > & A , vector<double> & B){//Gauss
  51.     int n = 6;
  52.     vector<double> X(n);
  53.     for(int j = 0 ; j < n ; ++j){
  54.         for(int i = j + 1 ; i < n ; ++i){
  55.             double k = A[i][j] / A[j][j];
  56.             for(int r = 0 ; r < n ; ++r){
  57.                 A[i][r] -= k * A[j][r];
  58.             }
  59.             B[i] -= k * B[j];
  60.         }
  61.     }
  62.    
  63.     for(int i = n - 1 ; i >= 0 ; --i){
  64.         double res = B[i];
  65.         for(int j = i + 1 ; j < n ; ++j){
  66.             res -= A[i][j] * X[j];
  67.         }
  68.         res /= A[i][i];
  69.         X[i] = res;
  70.         if(X[i] == -0.)X[i] = 0;
  71.     }
  72.     return X;
  73. //    for(int i = 0 ; i < n ; ++i)cout << X[i] << " ";
  74. //    cout << endl;
  75. }
  76.  
  77. int main(int argc, char* argv[]){
  78.     cyrillic;
  79.     cout.setf(ios::fixed);
  80.     freopen("input.txt" , "r" , stdin);
  81.  //   freopen("output.txt" , "w" , stdout);
  82.     cout.precision(20);
  83.  
  84.     string parse_p = parse_path(argv[0]);
  85.     vector<char> path(parse_p.begin() , parse_p.end());
  86.     path.push_back('\0');
  87.     concat(path , "temp.txt");
  88.  
  89.     vector<double> x;
  90.     double tmp;
  91.  
  92.     while(cin >> tmp){
  93.         x.push_back(tmp);
  94.     }
  95.  
  96.     int n = x.size();
  97.  
  98.     vector<double> dft;
  99.  
  100.     FILE * file = fopen(&path[0] , "w");
  101.     fprintf(file , "# X   Y\n");
  102.  
  103.  
  104.     for(int i = 0 ; i < n ; ++i){
  105.         double real = double(0);
  106.         double imag = double(0);
  107.         for(int j = 0 ; j < n ; ++j){
  108.             double angle = double((2 * M_PI * i * j) / n);
  109.             real += x[j] * double(cos(angle));
  110.             imag += x[j] * double(sin(angle));
  111.         }
  112.         double val = sqrt((real * real) + (imag * imag));
  113.         fprintf(file , "  %lf   %lf\n" , (i * 0.01) , val);
  114.         dft.push_back(val);
  115.     }
  116.     fclose(file);
  117.     vector<pair<double , double>> mx; //{coordinate , value}
  118.    
  119.     for(int i = 2 ; i < n / 2 ; ++i){
  120.         if(dft[i] - dft[i - 1] > eps && dft[i] - dft[i + 1] > eps){
  121.             mx.push_back(make_pair(i / 5.0, dft[i]));
  122.         }
  123.     }
  124.  
  125. //    for(int i = 0 ; i < mx.size() ; ++i){
  126. //        cout << mx[i].first << ":" << mx[i].second << endl;
  127. //    }
  128.  
  129.     vector<vector<double> > M(6 , vector<double>(6));
  130.     vector<double> y_(6);
  131.  
  132.     for(int i = 0 ; i < n ; ++i){
  133.         double ii = double(i * 0.01);
  134.  
  135.         for(int j = 0 ; j < 3 ; ++j){
  136.             for(int k = 0 ; k < 3 ; ++k){
  137.                 M[j][k] += pow(ii , 6 - (j + k));
  138.             }
  139.         }
  140.  
  141.         for(int j = 0 ; j < 3 ; ++j){
  142.             M[j][3] += pow(ii , 3 - j) * sin(2 * M_PI * ii * mx[0].first);
  143.             M[3][j] += pow(ii , 3 - j) * sin(2 * M_PI * ii * mx[0].first);
  144.         }
  145.  
  146.         M[3][3] += pow(sin(2 * M_PI * ii * mx[0].first) , 2);
  147.         M[4][4] += pow(sin(2 * M_PI * ii * mx[1].first) , 2);
  148.         M[4][3] += sin(2 * M_PI * ii * mx[0].first) * sin(2 * M_PI * ii * mx[1].first);
  149.         M[3][4] += sin(2 * M_PI * ii * mx[0].first) * sin(2 * M_PI * ii * mx[1].first);
  150.        
  151.         for(int j = 0 ; j < 3 ; ++j){
  152.             M[j][4] += pow(ii , 3 - j) * sin(2 * M_PI * ii * mx[1].first);
  153.             M[4][j] += pow(ii , 3 - j) * sin(2 * M_PI * ii * mx[1].first);
  154.         }
  155.  
  156.         for(int j = 0 ; j < 3 ; ++j){
  157.             M[5][j] += pow(ii , 3 - j);
  158.             M[j][5] += pow(ii , 3 - j);
  159.         }
  160.  
  161.         M[3][5] += sin(2 * M_PI * ii * mx[0].first);
  162.         M[5][3] += sin(2 * M_PI * ii * mx[0].first);
  163.            
  164.         M[4][5] += sin(2 * M_PI * ii * mx[1].first);
  165.         M[5][4] += sin(2 * M_PI * ii * mx[1].first);
  166.  
  167.         M[5][5] = n;
  168.  
  169.         for(int j = 0 ; j < 3 ; ++j){
  170.             y_[j] += pow(ii , 3 - j) * x[i];
  171.         }
  172.  
  173.         y_[3] += sin(2 * M_PI * ii * mx[0].first) * x[i];
  174.         y_[4] += sin(2 * M_PI * ii * mx[1].first) * x[i];
  175.         y_[5] += x[i];
  176.     }
  177.  
  178.     vector<double> a_i = gauss(M , y_);
  179. /*  for(int i = 0 ; i < 6 ; ++i){
  180.         cout << a_i[i] << " ";
  181.     }*/
  182.  
  183.     string res = "";
  184.     res += dtos(a_i[0]) + "*x*x*x+";
  185.     res += dtos(a_i[1]) + "*x*x+";
  186.     res += dtos(a_i[2]) + "*x+";
  187.     res += dtos(a_i[3]) + "*sin(2*pi*x*" + dtos(mx[0].first) +")+";
  188.     res += dtos(a_i[4]) + "*sin(2*pi*x*" + dtos(mx[1].first) +")+";
  189.     res += dtos(a_i[5]);
  190.     cout << endl << res << endl;
  191.  
  192.     Gnuplot gnu("lines");
  193.     string aa = "set style line 1 lc rgb '#0060ad' lt 2 lw 2 pt 0 ps 1.5";
  194.     string bb = &path[0]; bb = "plot '" + bb; bb += "' with linespoints ls 1";
  195.     gnu.set_xrange(0. , 5.);
  196.     gnu.set_title("DFT");
  197.     gnu.plot_points(bb , aa);
  198.     wait();
  199.     gnu.reset_plot();
  200.     gnu.set_title("f(x)");
  201.     gnu.set_xrange(0. , 5.);
  202.     gnu.plot_equation(res);
  203.     wait();
  204.     remove(&path[0]);
  205.  
  206.     return 0;
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement