Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.69 KB | None | 0 0
  1. //Tomasz Janik
  2.  
  3. #include <cmath>
  4. #include <iomanip>
  5. #include <vector>
  6. #include <stdlib.h>
  7.  
  8. #include<iostream>
  9. using namespace std;
  10.  
  11.  
  12. class FindRoot {
  13. private:
  14.     double(*function)(double);
  15.     double pointLeft;
  16.     double pointRight;
  17.     int numberOfIterations;
  18.     double epsilon;
  19.     double delta;
  20.  
  21.     bool checkSigns(double valueA, double valueB) {
  22.         return ((valueA >= 0 && valueB >= 0) || (valueA < 0 && valueB < 0));
  23.     }
  24.  
  25.     double nextSecant(double prev1, double prev2, double prev1Value, double prev2Value) {
  26.         return (prev2Value * prev1 - prev1Value * prev2) / (prev2Value - prev1Value);
  27.         //return prev1 - (prev1Value * (prev1 - prev2)) / (prev1Value - prev2Value);
  28.         //return (prev1Value * prev2 - prev2Value * prev1) / (prev1Value - prev2Value);
  29.     }
  30.  
  31.     bool precissionStop(double value) {
  32.         return abs(value) <= this->epsilon;
  33.     }
  34.  
  35. public:
  36.     FindRoot(double(*f)(double), double a, double b, int M, double epsilon, double delta) {
  37.         this->function = f;
  38.         this->pointLeft = a;
  39.         this->pointRight = b;
  40.         this->numberOfIterations = M;
  41.         this->epsilon = epsilon;
  42.         this->delta = delta;
  43.     }
  44.  
  45.     double solve() {
  46.         double valueA = function(pointLeft);
  47.         double valueB = function(pointRight);
  48.  
  49.         if (precissionStop(valueA))
  50.             return pointLeft;
  51.  
  52.         if (precissionStop(valueB))
  53.             return pointRight;
  54.  
  55.         while ((abs(pointLeft - pointRight) > delta) && checkSigns(valueA, valueB)) {
  56.             double temp = nextSecant(pointLeft, pointRight, valueA, valueB);
  57.  
  58.             valueA = valueB;
  59.             pointLeft = pointRight;
  60.             valueB = function(temp);
  61.             pointRight = temp;
  62.  
  63.             if (precissionStop(valueB)) {
  64.                 return temp;
  65.             }
  66.         }
  67.  
  68.         while ((abs(pointLeft - pointRight) > delta) && abs(pointRight - pointLeft) > 1.0f / 128.0f) {
  69.             double c = (pointLeft + pointRight) / 2;
  70.             double value = function(c);
  71.             if (precissionStop(value)) return c;
  72.  
  73.             if (checkSigns(valueA, value)) {
  74.                 pointLeft = c;
  75.                 valueA = value;
  76.             }
  77.             else {
  78.                 pointRight = c;
  79.                 valueB = value;
  80.             }
  81.         }
  82.  
  83.         while ((abs(pointLeft - pointRight) > delta)) {
  84.             double temp = nextSecant(pointLeft, pointRight, valueA, valueB);
  85.  
  86.             valueA = valueB;
  87.             pointLeft = pointRight;
  88.             valueB = function(temp);
  89.             pointRight = temp;
  90.  
  91.             if (precissionStop(valueB)) {
  92.                 return temp;
  93.             }
  94.         }
  95.  
  96.         return pointRight;
  97.     }
  98. };
  99.  
  100. double wyznaczMiejsceZerowe(double(*f)(double), double a, double b, int M, double epsilon, double delta) {
  101.     FindRoot solver(f, a, b, M, epsilon, delta);
  102.     return solver.solve();
  103. }
  104.  
  105.  
  106. double wielomian(double x) { return (((x - 6)*x + 11)*x) - 6; }
  107. double wielomianSinExp(double x)
  108. {
  109.     return ((((x - 6)*x + 11)*x) - 4 + sin(15 * x))*exp(-x * x);
  110. }
  111. double kwadrat(double x) { return (x*x - 2); }
  112. double kwadrat100(double x) { return 1e100*(x*x - 2); }
  113. double kwadrat_10(double x) { return 1e-10*(x*x - 2); }
  114.  
  115. int main() {
  116.     cout.precision(17);                                               // Spodziewany wynik
  117.     cout << wyznaczMiejsceZerowe(wielomian, 0, 4, 20, 1e-15, 1e-14) << endl;      // 1 lub 2 lub 3
  118.     cout << wyznaczMiejsceZerowe(wielomian, 0, 40, 20, 1e-15, 1e-14) << endl;     // 1 lub 2 lub 3
  119.     cout << wyznaczMiejsceZerowe(wielomian, 1, 2, 2, 1e-15, 1e-14) << endl;       // 1 lub 2  
  120.     cout << wyznaczMiejsceZerowe(wielomian, -150, 1.9, 20, 1e-15, 1e-14) << endl; // 1
  121.     cout << wyznaczMiejsceZerowe(wielomian, 1.5, 2.99, 20, 1e-15, 1e-14) << endl; // 2
  122.     cout << wyznaczMiejsceZerowe(wielomian, 2.01, 40, 20, 1e-15, 1e-14) << endl;  // 3
  123.     cout << wyznaczMiejsceZerowe(wielomian, 1.5, 6, 20, 1e-15, 1e-14) << endl;    // 1 lub 2 lub 3
  124.  
  125.     cout << wyznaczMiejsceZerowe(wielomianSinExp, -1, 3, 60, 1e-60, 1e-14) << endl;  // 0.43636925909804245
  126.     cout << wyznaczMiejsceZerowe(wielomianSinExp, -3, 3, 60, 1e-160, 1e-14) << endl; // 0.43636925909804245
  127.  
  128.     cout << wyznaczMiejsceZerowe(kwadrat, 0, 4, 15, 1e-11, 1e-14) << endl;          // 1.414213562373095
  129.     cout << wyznaczMiejsceZerowe(kwadrat100, 0, 4, 15, 1e-11, 1e-14) << endl;       // 1.414213562373095
  130.     cout << wyznaczMiejsceZerowe(kwadrat_10, 0, 4, 10, 1e-10, 1e-14) << endl;       // każdy punkt z przedziału [1, 1.73205]
  131.     cout << wyznaczMiejsceZerowe(kwadrat_10, 0, 4, 15, 1e-160, 1e-14) << endl;      // 1.414213562373095
  132.     return 0;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement