Advertisement
Guest User

Untitled

a guest
May 21st, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.95 KB | None | 0 0
  1. bool criterionOfNewton(const matrix <double> &x, double eps) {
  2.     for (int i = 0; i < x.rowSize(); i++) {
  3.         for (int j = 0; j < x.colSize(); j++) {
  4.             if (abs(x[i][j] > eps)) return false;
  5.         }
  6.     }
  7.     return true;
  8. }
  9.  
  10. matrix <double> getNewton(int n, double eps, int k,int m, const matrix <double> &first, int &number) {
  11.     matrix <double> Jx(n, n);
  12.     matrix <double> X(n, 1);
  13.     matrix <double> Fx(n, 1);
  14.     matrix <double> FL(n, n);
  15.     matrix <double> FU(n, n);
  16.     matrix <double> FP(n, n);
  17.  
  18.     int numberOfIterations = 0;
  19.     int countForDet;
  20.    
  21.     //Jx = getJx(first);
  22.     //getLUP(Jx,FL,FU, FP, countForDet);
  23.     matrix <double> X_next(first);
  24.    
  25.     do  {
  26.         if (numberOfIterations < m) {
  27.             Jx = getJx(X_next);
  28.             getLUP(Jx,FL,FU,FP,countForDet);
  29.         }
  30.         X = X_next;
  31.         Fx = getFx(X);
  32.  
  33.         //getSystemSolutions(Jx,-Fx,X_next);
  34.         getSystemSolutions(FL, FU, FP, -Fx, X_next);
  35.         numberOfIterations++;
  36.         X_next += X;
  37.        
  38.     } while (!criterionOfNewton(X_next - X, eps) && numberOfIterations < k);
  39.     //cout << "number of iterations: " << numberOfIterations << endl;
  40.     number = numberOfIterations;
  41.     return X;
  42. }
  43.  
  44. // all for 3
  45. void ALLNEWTON() {
  46.     //double sol = getNewton(&myFunction, &myDxFunction, 0, 0.000000001);
  47.     //cout <<"SOL: "  << sol  <<" res: " << myFunction(sol) << endl;
  48.  
  49.     matrix <double> X(10, 1);
  50.     matrix <double> FIRST(10, 1);
  51.     FIRST[0][0] = 0.5;
  52.     FIRST[1][0] = 0.5;
  53.     FIRST[2][0] = 1.5;
  54.     FIRST[3][0] = -1.0;
  55.     FIRST[4][0] = -0.5;
  56.     FIRST[5][0] = 1.5;
  57.     FIRST[6][0] = 0.5;
  58.     FIRST[7][0] = -0.5;
  59.     FIRST[8][0] = 1.5;
  60.     FIRST[9][0] = -1.5;
  61.     int numberOfIterations = 0;
  62.     double average_time = 0;
  63.  
  64.     /*for (int m = 1; m < 17; m++) {
  65.     for (int i = 0; i < 10; i++) {
  66.     clock_t start = clock();
  67.     X = getNewton(10, 0.0000001, 50, m, FIRST,numberOfIterations);
  68.     clock_t end = clock();
  69.     average_time += (double)(end - start)/CLOCKS_PER_SEC;
  70.     }
  71.     cout << "x[4]=-0.5 , m= " << m << " , average time= " << average_time / 10 << endl;
  72.     cout << "numberOfIterations: " << numberOfIterations << endl << endl;
  73.     average_time = 0;
  74.     }
  75.     X[4][0] = -0.2;
  76.     cout << endl;
  77.     for (int m = 1; m < 17; m++) {
  78.     for (int i = 0; i < 10; i++) {
  79.     clock_t start = clock();
  80.     X = getNewton(10, 0.0000001, 50, m, FIRST,numberOfIterations);
  81.     clock_t end = clock();
  82.     average_time += (double)(end - start) / CLOCKS_PER_SEC;
  83.     }
  84.     cout << "x[4]=-0.2 , m= " << m << " , average time= " << average_time / 10 << endl;
  85.     cout << "numberOfIterations: " << numberOfIterations << endl << endl;
  86.     average_time = 0;
  87.     }
  88.     */
  89.  
  90.     clock_t start = clock();
  91.     X = getNewton(10, 0.0000001, 900, 1000, FIRST, numberOfIterations);
  92.     clock_t end = clock();
  93.  
  94.     cout << "FIRST[4][0]=-0.5\n";
  95.     cout << "Program execution time: " << (double)(end - start) / CLOCKS_PER_SEC << " sec" << endl;
  96.     cout << "number Of Iterations " << numberOfIterations << endl;
  97.     cout << "solutions:\n" << X << endl;
  98.  
  99.     cout << "F(x)= \n" << getFx(X) << endl;
  100.  
  101.     cout << "/////////////////////////////////\n";
  102.     cout << "FIRST[4][0]=-0.2\n";
  103.     FIRST[4][0] = -0.2;
  104.     start = clock();
  105.     X = getNewton(10, 0.0000001, 900, 1000, FIRST, numberOfIterations);
  106.     end = clock();
  107.  
  108.     cout << "Program execution time: " << (double)(end - start) / CLOCKS_PER_SEC << " sec" << endl;
  109.     cout << "number Of Iterations " << numberOfIterations << endl;
  110.     cout << "solutions:\n" << X << endl;
  111.  
  112.     cout << "F(x)= \n" << getFx(X) << endl;
  113.  
  114.     cout << "/////////////////////////////////\n";
  115.     cout << "FIRST[4][0]=-0.5\n";
  116.     FIRST[4][0] = -0.5;
  117.     average_time = 0;
  118.     double min_time = 100000000000;
  119.     int min_m = 1;
  120.     for (int m = 1; m < 7; m++) {
  121.         for (int i = 0; i < 10; i++) {
  122.             clock_t start = clock();
  123.             X = getNewton(10, 0.0000001, 500, m, FIRST, numberOfIterations);
  124.             clock_t end = clock();
  125.             average_time += (double)(end - start) / CLOCKS_PER_SEC;
  126.         }
  127.         cout << "x[4]=-0.5 , m= " << m << " , average time= " << average_time / 10 << endl;
  128.         cout << "numberOfIterations: " << numberOfIterations << endl << endl;
  129.         if (average_time < min_time) {
  130.             min_time = average_time;
  131.             min_m = m;
  132.         }
  133.         average_time = 0;
  134.     }
  135.     cout << endl << "m= " << min_m << " min time= " << min_time / 10 << endl;
  136.  
  137.  
  138.     cout << "/////////////////////////////////\n";
  139.     cout << "FIRST[4][0]=-0.2\n";
  140.     matrix <double> N0(10, 1, 0); // нулевой вектор
  141.     FIRST[4][0] = -0.2;
  142.     average_time = 0;
  143.     min_time = 100000000000;
  144.     min_m = 1;
  145.     for (int m = 1; m < 13; m++) {
  146.         for (int i = 0; i < 10; i++) {
  147.             clock_t start = clock();
  148.             X = getNewton(10, 0.0000001, 500, m, FIRST, numberOfIterations);
  149.             clock_t end = clock();
  150.             average_time += (double)(end - start) / CLOCKS_PER_SEC;
  151.         }
  152.         if (average_time < min_time && cmp(getFx(X), N0)) {
  153.             min_time = average_time;
  154.             min_m = m;
  155.         }
  156.         cout << "x[4]=-0.2 , m= " << m << " , average time= " << average_time / 10 << endl;
  157.         if (cmp(getFx(X), N0)) cout << "numberOfIterations: " << numberOfIterations << endl << endl;
  158.         else cout << "not answer" << endl << endl;
  159.         average_time = 0;
  160.     }
  161.     cout << endl << "m= " << min_m << " min time= " << min_time / 10 << endl;
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement