Advertisement
SeriousVenom

LastMethods

Mar 18th, 2021
961
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.53 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. double function(double x);
  6. double fi(double x);
  7. double derivative(double x);
  8. double derivative_2(double x);
  9. int check_for_newton(double a, double b);
  10. void simple_iteration();
  11. void method_chord();
  12. void method_newton();
  13. void method_chebishev();
  14. bool sign(double a);
  15.  
  16. int main() {
  17.     int menu = 0;
  18.     cout << "Choice method:\n1.Method Simple Iteration\n2.Method Chord\n3.Method Newton\n4.Method Chebishev\nEnter:"; cin >> menu;
  19.     if (menu == 1) {
  20.         simple_iteration();
  21.     }
  22.     if (menu == 2) { method_chord(); }
  23.     if (menu == 3) { method_newton(); }
  24.     if (menu == 4) { method_chebishev(); }
  25.     return 0;
  26. }
  27.  
  28. double function(double x) {
  29.     return pow(x, 3) + 3 * x + 1;
  30. }
  31.  
  32. double fi(double x) {
  33.     return (-1 - pow(x, 3) / 3);
  34. }
  35.  
  36. void simple_iteration() {
  37.     double a, b, eps;
  38.     int n = 0;
  39.     cout << "~~~~~Method Simple Iteration~~~~~" << endl;
  40.     cout << "Enter left border: "; cin >> a;
  41.     cout << "Enter right border: "; cin >> b;
  42.     cout << "Enter epsilon: "; cin >> eps;
  43.     double x_i, x = a;
  44.  
  45.     do
  46.     {
  47.         x_i = x;
  48.         x = fi(x_i);
  49.         n++;
  50.     } while (fabs(x_i - x) >= eps);
  51.  
  52.     cout << endl << "Root of the equation: " << x << endl;
  53.     cout << "Count of iterations: " << n << endl;
  54.     printf("Checking the resultating root: %1.10f\n", function(x));
  55.  
  56. }
  57.  
  58. void method_chord()
  59. {
  60.     double a, b, eps;
  61.     int n = 0;
  62.     cout << "~~~~~Method Chord~~~~~" << endl;
  63.     cout << "Enter left border: "; cin >> a;
  64.     cout << "Enter right border: "; cin >> b;
  65.     cout << "Enter epsilon: "; cin >> eps;
  66.     double x_i, x = a;
  67.  
  68.     if (sign(a) == true)
  69.     {
  70.         x = b;
  71.         do
  72.         {
  73.             x_i = x;
  74.             x = x_i - ((function(x_i)) / (function(x_i) - function(a))) * (x_i - a);
  75.             n++;
  76.         } while (fabs(x_i - x) >= eps);
  77.     }
  78.     else
  79.     {
  80.         x = a;
  81.         do
  82.         {
  83.             x_i = x;
  84.             x = x_i - ((function(x_i)) / (function(x_i) - function(b))) * (x_i - b);
  85.             n++;
  86.         } while (fabs(x_i - x) >= eps);
  87.     }
  88.  
  89.     cout << endl << "Root of the equation: " << x << endl;
  90.     cout << "Count of iterations: " << n << endl;
  91.     printf("Checking the resultating root: %1.10f\n", function(x));
  92.  
  93. }
  94.  
  95.  
  96. void method_newton()
  97. {
  98.     double a, b, eps;
  99.     int n = 0;
  100.     cout << "~~~~~Method Newton~~~~~" << endl;
  101.     cout << "Enter left border: "; cin >> a;
  102.     cout << "Enter right border: "; cin >> b;
  103.     cout << "Enter epsilon: "; cin >> eps;
  104.     double x_i, x = 0;
  105.     if (check_for_newton(a, b) == -1)
  106.     {
  107.         x = a;
  108.         cout << "As x0 we take the left border\n";
  109.     }
  110.     else if (check_for_newton(a, b) == 1)
  111.     {
  112.         x = b;
  113.         cout << "As x0 we take the right border\n";
  114.     }
  115.  
  116.     cout << "Function(a) * Derivative^2(a) = " << function(a) * derivative_2(a) << endl;
  117.     cout << "Function(b) * Derivative^2(b) = " << function(b) * derivative_2(b) << endl;
  118.     cout << "X0 = " << x << endl;
  119.  
  120.     do {
  121.         x_i = x;
  122.         x = x_i - (function(x_i)) / derivative(x_i);
  123.         n++;
  124.     } while (fabs(x_i - x) >= eps);
  125.  
  126.     cout << endl << "Root of the equation: " << x << endl;
  127.     cout << "Count of iterations: " << n << endl;
  128.     printf("Checking the resultating root: %1.15f\n", function(x));
  129. }
  130.  
  131.  
  132. void method_chebishev()
  133. {
  134.     double a, b, eps;
  135.     int n = 0;
  136.     cout << "~~~~~Method Chebishev~~~~~" << endl;
  137.     cout << "Enter left border: "; cin >> a;
  138.     cout << "Enter right border: "; cin >> b;
  139.     cout << "Enter epsilon: "; cin >> eps;
  140.     double x_i, x = 0;
  141.     if (check_for_newton(a, b) == -1)
  142.     {
  143.         x = a;
  144.         cout << "As x0 we take the left border\n";
  145.     }
  146.     else if (check_for_newton(a, b) == 1)
  147.     {
  148.         x = b;
  149.         cout << "As x0 we take the right border\n";
  150.     }
  151.  
  152.     cout << "Function(a) * Derivative^2(a) = " << function(a) * derivative_2(a) << endl;
  153.     cout << "Function(b) * Derivative^2(b) = " << function(b) * derivative_2(b) << endl;
  154.     cout << "X0 = " << x << endl;
  155.  
  156.     do {
  157.         x_i = x;
  158.         //x = x_i - ((function(x_i)) / derivative(x_i)) - ((derivative_2(x_i) * pow(function(x_i),2)) / (2 * pow(derivative(x_i), 3)));
  159.         x = x_i - ((function(x_i)) / derivative(x_i)) - ((derivative_2(x_i) * function(x_i) * function(x_i)) / (2 * pow(derivative(x_i), 3)));
  160.  
  161.         n++;
  162.     } while (fabs(x_i - x) >= eps);
  163.  
  164.     cout << endl << "Root of the equation: " << x << endl;
  165.     cout << "Count of iterations: " << n << endl;
  166.     printf("Checking the resultating root: %1.10f\n", function(x));
  167. }
  168.  
  169.  
  170. bool sign(double a)
  171. {
  172.     if (function(a) > 0) return true;
  173.     else if (function(a) < 0) return false;
  174. }
  175.  
  176. double derivative(double x)
  177. {
  178.     return 3 * pow(x, 2) + 3;
  179. }
  180.  
  181.  
  182. double derivative_2(double x)
  183. {
  184.     return 6 * x;
  185. }
  186.  
  187.  
  188. int check_for_newton(double a, double b)
  189. {
  190.     if (function(a) * derivative_2(a) > 0)return -1;
  191.     else if (function(b) * derivative_2(b) > 0)return 1;
  192.     else return 0;
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement