Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.61 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. double f(double x)
  5. {
  6.     return tan(x) - 1;
  7.     }
  8.  
  9. double solve_bis(double a, double b, double eps, double c)
  10. {
  11.     double q;
  12.     while (abs(a - b) > eps)
  13.     {
  14.         q =(a + b) / 2;
  15.         if ((f(a) - c) * (f(q) - c) < 0)
  16.             b = q;
  17.         else
  18.             a = q;
  19.     }
  20.     return q;
  21. }
  22. double solve_chord(double a, double b, double eps, double c)
  23. {
  24.     double fa, fb, fc, k, l, c1, c2;
  25.     c1 = f(a);
  26.     c2 = f(b);
  27.     while (abs(c2 - c1) > eps) {
  28.         c2 = c1;
  29.         fa = f(a) - c;
  30.         fb = f(b) - c;
  31.         k = (fa - fb) / (a - b);
  32.         l = fa - k * a; // b
  33.         c1 = -l/k;
  34.         fc = f(c1) - c;
  35.         if ((fa * fc) <= 0)
  36.             b = c1;
  37.         if ((fb * fc) <= 0)
  38.             a = c1;
  39.     }
  40.     return c1;
  41. }
  42.  
  43.  
  44. int solve(int method, double a, double b, double eps, double c)
  45. {
  46.     if ((f(a) - c)*(f(b) - c) < 0)
  47.     {
  48.         if (method == 1)
  49.              {
  50.                 printf("%.6f\n", solve_bis( a, b, eps, c));
  51.                 return 0;
  52.              }
  53.         else if (method == 2)
  54.              {
  55.                 printf("%.6f\n", solve_chord(a, b,  eps, c));
  56.                 return 0;
  57.              }
  58.         else if (method == 3)
  59.         {
  60.                 printf("%.6f\n", solve_bis( a, b, eps, c));
  61.                 printf("%.6f\n",solve_chord(a, b, eps, c));
  62.                 return 0;
  63.         }
  64.     }
  65.  return 1;
  66.  
  67. }
  68.  
  69. int main()
  70. {
  71.  double c;
  72.  double a, b;
  73.  double eps;
  74.  int method;
  75.  
  76.  scanf ("%d%lf%lf%lf%lf", &method, &a, &b, &eps, &c);
  77.  solve(method, a, b, eps, c);
  78.  
  79.  
  80.  
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement