Th3NiKo

Sieci neuronowe - ćwiczenia 4

Apr 17th, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.62 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <vector>
  4. #include <math.h>
  5. #include <time.h>
  6. #include <stdlib.h>
  7.  
  8. using namespace std;
  9.  
  10. double c = 0.01;
  11. double eps = 0.00000001;
  12.  
  13.  
  14. double calcFirstX (vector<double> vec) {
  15.     return 4.0*vec[0] - 2.0* vec[1] - 2.0;
  16. }
  17. double calcFirstY (vector<double> vec) {
  18.     return 4.0*vec[1] - 2.0* vec[0] - 2.0*vec[2];
  19. }
  20. double calcFirstZ (vector<double> vec) {
  21.     return 2.0* vec[2] - 2.0*vec[1];
  22. }
  23. double calcSecondX (vector<double> vec) {
  24.     return 12 * pow(vec[0], 3.0) + 12 * pow(vec[0], 2.0) - 24 * vec[0];
  25. }
  26. double calcSecondY (vector<double> vec) {
  27.     return 24 * vec[1] - 24;
  28. }
  29.  
  30. void gradientFirst (vector<double> input) {
  31.     vector<double> vectorNew;
  32.     bool znalazlem = true;
  33.     double max = 0.0;
  34.     //Zerowanie
  35.     for (int i=0; i<3; i++) {
  36.         vectorNew.push_back(0.0);
  37.     }
  38.  
  39.     while (znalazlem) {
  40.         //Odejmowanie od pochodnej
  41.         vectorNew[0] = input[0] - c * calcFirstX(input);
  42.         vectorNew[1] = input[1] - c * calcFirstY(input);
  43.         vectorNew[2] = input[2] - c * calcFirstZ(input);
  44.         max = 0.0;
  45.         for (int i=0; i<3; i++) {
  46.  
  47.             if (fabs(vectorNew[i] - input[i]) > max) {
  48.                 max = fabs(vectorNew[i] - input[i]);
  49.             }
  50.         }
  51.         //Jezeli max mniejszy od bledu
  52.         if (max < eps) {
  53.             printf("Punkt: %f, %f, %f\nWartosc w punkcie: %f", vectorNew[0], vectorNew[1], vectorNew[2], 2 * pow(vectorNew[0], 2.0)
  54.                                                                 + 2 * pow(vectorNew[1], 2.0)
  55.                                                                 + vectorNew[2]*vectorNew[2]
  56.                                                                 - 2 * vectorNew[0] * vectorNew[1]
  57.                                                                 - 2 * vectorNew[2] *vectorNew[1]
  58.                                                                 - 2* vectorNew[0]
  59.                                                                 + 3);
  60.             znalazlem = false;
  61.         }
  62.         input = vectorNew;
  63.  
  64.     }
  65. }
  66.  
  67. void gradientSecond (vector<double> input) {
  68.     vector<double> vectorNew;
  69.     bool znalazlem = true;
  70.     double max = 0.0;
  71.     for (int i=0; i<2; i++) {
  72.         vectorNew.push_back(0.0);
  73.     }
  74.     while (znalazlem) {
  75.         vectorNew[0] = input[0] - c * calcSecondX(input);
  76.         vectorNew[1] = input[1] - c * calcSecondY(input);
  77.  
  78.         max = 0.0;
  79.         for (int i=0; i<2; i++) {
  80.             if (fabs(vectorNew[i] - input[i]) > max) {
  81.                 max = fabs(vectorNew[i] - input[i]);
  82.             }
  83.         }
  84.         if (max < eps) {
  85.             printf("Punkt: %lf, %lf\nWartosc w punkcie: %lf", vectorNew[0], vectorNew[1], vectorNew[0]*vectorNew[0]*vectorNew[0]*vectorNew[0] * 3
  86.                                                             + 4* vectorNew[0]*vectorNew[0]*vectorNew[0]
  87.                                                             - 12 * vectorNew[0]*vectorNew[0]
  88.                                                             + 12*vectorNew[1] * vectorNew[1]
  89.                                                             - 24 * vectorNew[1]);
  90.             znalazlem = false;
  91.         }
  92.         input = vectorNew;
  93.  
  94.     }
  95. }
  96.  
  97. int main(int argc, char** argv) {
  98.  
  99.     srand(time(NULL));
  100.  
  101.  
  102.     //Pierwsza funkcja
  103.     /////////////////////////////////////
  104.     printf("\n\n\n");
  105.     cout << "Przyklad a)" << endl;
  106.     vector<double> vectorFirst;
  107.     vectorFirst.push_back((double)rand());
  108.     vectorFirst.push_back((double)rand());
  109.     vectorFirst.push_back((double)rand());
  110.     gradientFirst(vectorFirst);
  111.     printf("\n\n\n");
  112.  
  113.     //Druga funkcja
  114.     ////////////////////////////////////
  115.     cout << "Przyklad b) " << endl;
  116.     vector<double> vectorSecond;
  117.     vectorSecond.push_back(4.0);
  118.     vectorSecond.push_back(4.0);
  119.     gradientSecond(vectorSecond);
  120.     printf("\n\n\n");
  121.  
  122.     printf("\n\n\n");
  123.  
  124.  
  125.  
  126.  
  127.     return 0;
  128. }
Add Comment
Please, Sign In to add comment