Advertisement
SeriousVenom

NewMethods

Mar 11th, 2021
567
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.89 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. double function(double x);
  6. double fi(double x);
  7. void simple_iteration();
  8. void method_chord();
  9. bool sign(double a);
  10.  
  11. int main() {
  12.     int menu = 0;
  13.     cout << "Choice method:\n1.Method Simple Iteration\n2.Method Chord\nEnter:"; cin >> menu;
  14.     if (menu == 1) {
  15.         simple_iteration();
  16.     }
  17.     if (menu == 2) { method_chord(); }
  18.     return 0;
  19. }
  20.  
  21. double function(double x) {
  22.     return pow(x, 3) + 3 * x + 1;
  23. }
  24.  
  25. double fi(double x) {
  26.     return (-1 - pow(x, 3) / 3);
  27. }
  28.  
  29. void simple_iteration() {
  30.     double a, b, eps;
  31.     int n = 0;
  32.     cout << "~~~~~Method Simple Iteration~~~~~" << endl;
  33.     cout << "Enter left border: "; cin >> a;
  34.     cout << "Enter right border: "; cin >> b;
  35.     cout << "Enter epsilon: "; cin >> eps;
  36.     double x_i, x = a;
  37.  
  38.     do
  39.     {
  40.         x_i = x;
  41.         x = fi(x_i);
  42.         n++;
  43.     } while (fabs(x_i - x) >= eps);
  44.  
  45.     cout << endl << "Root of the equation: " << x << endl;
  46.     cout << "Count of iterations: " << n << endl;
  47.     printf("Checking the resultating root: %1.10f\n", function(x));
  48.  
  49. }
  50.  
  51. void method_chord()
  52. {
  53.     double a, b, eps;
  54.     int n = 0;
  55.     cout << "~~~~~Method Chord~~~~~" << endl;
  56.     cout << "Enter left border: "; cin >> a;
  57.     cout << "Enter right border: "; cin >> b;
  58.     cout << "Enter epsilon: "; cin >> eps;
  59.     double x_i, x = a;
  60.  
  61.     if (sign(a) == true)
  62.     {
  63.         x = b;
  64.         do
  65.         {
  66.             x_i = x;
  67.             x = x_i - ((function(x_i)) / (function(x_i) - function(a))) * (x_i - a);
  68.             n++;
  69.         } while (fabs(x_i - x) >= eps);
  70.     }
  71.     else
  72.     {
  73.         x = a;
  74.         do
  75.         {
  76.             x_i = x;
  77.             x = x_i - ((function(x_i)) / (function(x_i) - function(b))) * (x_i - b);
  78.             n++;
  79.         } while (fabs(x_i - x) >= eps);
  80.     }
  81.  
  82.     cout << endl << "Root of the equation: " << x << endl;
  83.     cout << "Count of iterations: " << n << endl;
  84.     printf("Checking the resultating root: %1.10f\n", function(x));
  85.  
  86. }
  87.  
  88. bool sign(double a)
  89. {
  90.     if (function(a) > 0) return true;
  91.     else if (function(a) < 0) return false;
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement