Advertisement
hugol

Untitled

May 11th, 2015
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.17 KB | None | 0 0
  1. #include <iostream>
  2. #include <assert.h>
  3. #include <vector>
  4. #include <math.h>
  5.  
  6. using namespace std;
  7.  
  8. #define eps 1e-15
  9.  
  10. #define ILOSC_PODPRZEDZIALOW_POSZUKIWAN_MZ 1e7
  11. #define PRZEDZIAL_WASKI_PRAWOPODOBNIE_ASYMPTOTA 1e-20
  12. #define MAX_ILOSC_ITERACJI 100000
  13.  
  14. // krance przedzialu poszukiwan mz
  15. #define A -7
  16. #define B 7
  17.  
  18. double FX(double x)
  19. {
  20.     //return log(x+2.0)-2*pow(x,2.0)+1;
  21.     //return pow(x,3.0)-2*x+2; 
  22.     //return 1/x;
  23.     return -5*pow(x,3.0)+8*pow(x,2.0)+3*x-1;
  24.  
  25.     //return tan(x);
  26. }
  27.  
  28. double FXd(double x)
  29. {
  30.     return -15*x*x+16*x+3;
  31.     //return 1+tan(x)*tan(x);
  32. }
  33.  
  34.  
  35. struct solution_t
  36. {
  37.     double solution;
  38.     int iter;
  39.     solution_t(double solution, int iter)
  40.     {
  41.         solution_t::solution = solution;
  42.         solution_t::iter = iter;
  43.     }
  44. };
  45.  
  46. int bisekcja(double a, double b, double *solution, double fx(double), double fxd(double))
  47. {
  48.     assert( fx(a)*fx(b) < 0 );
  49.    
  50.     double x = a;
  51.     double x1;
  52.     for(int i=0; i<MAX_ILOSC_ITERACJI; i++)
  53.     {
  54.         if (abs(a-b) < PRZEDZIAL_WASKI_PRAWOPODOBNIE_ASYMPTOTA)
  55.             return -1;
  56.  
  57.         x1 = (a+b) /2;
  58.         if ( abs( fx(x1) ) < eps )
  59.         {
  60.             *solution = x1;
  61.             return i;
  62.         }
  63.        
  64.         // dzielenie przedzialu
  65.         if ( fx(a)*fx(x1) < 0)
  66.             b = x1;
  67.         else if ( fx(x1)*fx(b) < 0)
  68.             a = x1;
  69.         x = x1;
  70.     }
  71.     //incase?
  72.     return -1;
  73. }
  74.  
  75. int metoda_newtona(double a, double b, double *solution, double fx(double), double fxd(double))
  76. {
  77.     double x = (a+b)/2;
  78.     double x1;
  79.     for(int i=0; i<MAX_ILOSC_ITERACJI; i++)
  80.     {
  81.         x1 = x - fx(x)/fxd(x);
  82.  
  83.         if (abs(x-x1) < PRZEDZIAL_WASKI_PRAWOPODOBNIE_ASYMPTOTA)
  84.             return -1;
  85.         if (abs( fx(x1) ) < eps)
  86.         {
  87.             *solution = x1;
  88.             return i;
  89.         }
  90.         x = x1;
  91.         if (x1 < a || x1 > b)
  92.             return -1;
  93.     }
  94.     return -1;
  95. }
  96.  
  97. double lokalizacja_i_znajdowanie_mz(double a, double b, vector<solution_t> &out,
  98.                                   int metoda(double,double, double*,double fx(double), double fxd(double)),
  99.                                   double fx(double), double fxd(double))
  100. {
  101.     double szerokosc = b-a;
  102.     double krok = szerokosc/ILOSC_PODPRZEDZIALOW_POSZUKIWAN_MZ;
  103.     double val = a;
  104.     double rozwiazanie;
  105.     while(val < b)
  106.     {
  107.         // rozne znaki
  108.         if ( fx(val)*fx(val + krok) < 0 )
  109.         {
  110.             int ret = metoda(val, val + krok, &rozwiazanie, fx, fxd);
  111.             if (ret >= 0 )
  112.             {
  113.                 // dodawanie tylko roznych rozwiazan
  114.                 //if (out.size()>0 && abs(out[out.size()-1].solution - rozwiazanie) > eps
  115.                 //  || out.size() == 0)
  116.                 out.push_back(solution_t(rozwiazanie, ret));
  117.             }
  118.         }
  119.         val = val + krok;
  120.     }
  121.     return krok;
  122. }
  123.  
  124. int main()
  125. {
  126.     vector<solution_t> rozwiazania;
  127.     double szerokosc_przedzialu = lokalizacja_i_znajdowanie_mz(A, B, rozwiazania, bisekcja, FX, FXd);
  128.  
  129.     cout << "przedzial (" << A << "," << B
  130.         << ") podzielony na podprzedzialy poszukiwan o szerokosci: " << szerokosc_przedzialu << endl;
  131.     cout << "bisekcja:\n";
  132.     for(int i=0; i<rozwiazania.size(); i++)
  133.         cout << "x" << i << ": " << rozwiazania[i].solution << ", iter: " << rozwiazania[i].iter << endl;
  134.  
  135.     rozwiazania.clear();
  136.     lokalizacja_i_znajdowanie_mz(A, B, rozwiazania, metoda_newtona, FX, FXd);
  137.  
  138.     cout << "\nmetoda_newtona:\n";
  139.     for(int i=0; i<rozwiazania.size(); i++)
  140.         cout << "x" << i << ": " << rozwiazania[i].solution << ", iter: " << rozwiazania[i].iter << endl;
  141.  
  142.     return 0;
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement