Advertisement
PikingFish

4

Sep 17th, 2020 (edited)
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.60 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3. #include <iomanip>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. int precision, spacing;
  9.  
  10. double x0, eps, k;
  11.  
  12. double abs(double x)
  13. {
  14.     if (x > 0)
  15.         return x;
  16.     else
  17.         return -x;
  18. }
  19.  
  20. bool continueWorking()
  21. {
  22.     string response;
  23.     cout << "Continue working?(y,n)";
  24.     cin >> response;
  25.     cout << endl;
  26.     return response == "y" || response == "Y";
  27. }
  28.  
  29. double f(double x, int k)
  30. {
  31.     return x - k * cos(x);
  32. }
  33.  
  34. double y1(double x)
  35. {
  36.     return x - 5 * cos(x);
  37. }
  38.  
  39. double y2(double x)
  40. {
  41.     return x - 10 * cos(x);
  42. }
  43.  
  44. double derivedF(double x, int k)
  45. {
  46.     return 1 + k * sin(x);
  47. }
  48.  
  49. void menu()
  50. {
  51.     precision = 0;
  52.    
  53.     cout << "x0 = ";
  54.     cin >> x0;
  55.     cout << "k = ";
  56.     cin >> k;
  57.     cout << "eps = ";
  58.     cin >> eps;
  59.     cout << endl;
  60.    
  61.     double counter = eps;
  62.     while(counter < 1)
  63.     {
  64.         precision++;
  65.         counter *= 10;
  66.     }
  67.    
  68.     spacing = precision + 4;
  69. }
  70.  
  71. void task()
  72. {
  73.     double current, last, beginOfInterval, endOfInterval, root;
  74.    
  75.     int n_max = 100000;
  76.     int j = 0;
  77.    
  78.     menu();
  79.    
  80.     current = x0;
  81.    
  82.     do          //Simple
  83.     {
  84.         j++;
  85.  
  86.         last = current;
  87.         current = last - f(last, k);
  88.     }
  89.     while((abs(last - current) > eps) && (j < n_max));
  90.    
  91.     cout << "Simple iteration: ";
  92.     cout << fixed << setw(spacing) << setprecision(precision) << current << endl;
  93.     cout << "Number of iterations = " << j << endl << endl;
  94.    
  95.     j = 0;
  96.     current = x0;
  97.    
  98.     do      //Neuton
  99.     {
  100.         j++;
  101.  
  102.         last = current;
  103.         current = last - f(last, k) / derivedF(last, k);
  104.     }
  105.     while((abs(last - current) > eps) && (j < n_max));
  106.  
  107.     cout << "Neuton method: ";
  108.     cout << fixed << setw(spacing) << setprecision(precision) << current << endl;
  109.     cout << "Number of iterations = " << j << endl << endl;
  110.    
  111.     j = 0;
  112.    
  113.     beginOfInterval = -k;   //halving
  114.     endOfInterval = k;
  115.     root = (beginOfInterval + endOfInterval) / 2;
  116.    
  117.     while(abs(f(root, k) ) > eps)
  118.     {
  119.         j++;
  120.        
  121.         if (f(beginOfInterval, k) * f(root, k) < 0)
  122.             endOfInterval = root;
  123.         else
  124.             beginOfInterval = root;
  125.        
  126.         root = (beginOfInterval + endOfInterval) / 2;
  127.     }
  128.    
  129.     cout << "Halving method: ";
  130.     cout << fixed << setw(spacing) << setprecision(precision) << root << endl;
  131.     cout << "Number of iterations = " << j << endl << endl;
  132. }
  133.  
  134. int universal(double (*f)(double) )
  135. {
  136.     double beginOfInterval, endOfInterval, root, xl, xr;
  137.    
  138.     bool hasRoots = false;
  139.    
  140.     precision = 0;
  141.    
  142.     cout << "eps (in format 10^) = ";
  143.     cin >> eps;
  144.     cout << "xl = ";
  145.     cin >> xl;
  146.     cout << "xr = ";
  147.     cin >> xr;
  148.     cout << "Roots: " << endl;
  149.    
  150.     double counter = eps;
  151.     while(counter < 1)
  152.     {
  153.         precision++;
  154.         counter *= 10;
  155.     }
  156.    
  157.     spacing = precision + 4;
  158.    
  159.     int j = 0;
  160.    
  161.     double step = 0.1;
  162.    
  163.     for(double i = xl; i < xr; i += step)
  164.     {
  165.         beginOfInterval = i;
  166.         endOfInterval = i + step;
  167.         root = (beginOfInterval + endOfInterval) / 2;
  168.        
  169.         if(f(beginOfInterval) * f(endOfInterval) < 0)
  170.         {
  171.             hasRoots = true;
  172.            
  173.             while(abs(f(root)) > eps)
  174.             {
  175.                 j++;
  176.                        
  177.                 if (f(beginOfInterval) * f(root) < 0)
  178.                     endOfInterval = root;
  179.                 else
  180.                     beginOfInterval = root;
  181.                
  182.                 root = (beginOfInterval + endOfInterval) / 2;
  183.             }
  184.                
  185.             cout << fixed << setw(spacing) << setprecision(precision) << root << endl;
  186.         }
  187.     }
  188.    
  189.     if(hasRoots == false)
  190.         cout << "None." << endl << endl;
  191.     else
  192.         cout << endl;
  193.    
  194.     return 0;
  195. }
  196.  
  197. int main()
  198. {
  199.     int answer;
  200.    
  201.     cout << "We are solving: x - k * cos(x) = 0" << endl;
  202.    
  203.     do
  204.     {
  205.         cout << "Type 3 to use universal function and any number to do tasks 1 or 2: ";
  206.         cin >> answer;
  207.         cout << endl;
  208.        
  209.         if(answer == 3)
  210.         {
  211.             universal(y1);
  212.             universal(y2); 
  213.         }
  214.         else
  215.             task();
  216.     }
  217.     while(continueWorking());
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement