Guest User

Untitled

a guest
Mar 21st, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. double eps = 0.00001; //точность вычисления
  2. double sum = 0;
  3. double temp = x;
  4. int i = 1;
  5. while (abs(temp) >= eps)
  6. {
  7. sum += temp;
  8. temp = temp * (x*x) / ((2 * i + 1) * (2 * i));
  9. i++;
  10. }
  11. cout << "x = " << x << "ty = " << 2 * x + sum << endl;
  12.  
  13. double calc(double x, int i)
  14. {
  15. if (i > 1000) return 1; // Критерий останова рекурсии
  16. return 1+x*x/(2*i+1)/(2*i)*calc(x,i+1);
  17. }
  18.  
  19. double Sinh(double x)
  20. {
  21. return x*calc(x,1);
  22. }
  23.  
  24. int main(int argc, const char * argv[])
  25. {
  26. for(double x = 0; x < 2; x += 0.1)
  27. {
  28. cout << setw(4) << x << setw(15) << Sinh(x) << setw(15) << sinh(x) << endl;
  29. }
  30. }
  31.  
  32. double calc(double x, int i, double term)
  33. {
  34. double fact = x*x/(2*i+1)/(2*i);
  35. term *= fact;
  36. if (fact < 1e-8) return 1; // Критерий останова рекурсии
  37. return 1+fact*calc(x,i+1, term);
  38. }
  39.  
  40. double Sinh(double x)
  41. {
  42. return x*calc(x,1,abs(x));
  43. }
  44.  
  45. double exp2(double x, int e1, int e2, int &i)
  46. {
  47. if(e1 == i || e2 == i) return 1 + x * x / e1 * e2;
  48. return 1 + x * x / (e1 * e2) * exp2(x, e1 + 2, e2 + 2, i);
  49. }
  50. void recurs_gorner(double x, int &i)
  51. {
  52. clock_t t0 = clock();
  53. cout << "x = " << x << "ty = " << 2 * x + exp2(x,2,3,i) << endl;
  54. for (int i = 0; i < 50000000; i++);
  55. clock_t t1 = clock();
  56. cout << "Время (рекурсия): " << (double)(t1 - t0) / CLOCKS_PER_SEC << endl;
  57. }
  58. int main()
  59. {
  60. setlocale(LC_ALL, "rus");
  61. double x = 3.0;
  62. int i = 1;
  63. recurs_gorner(x, i);
  64. _getch();
  65. return 0;
  66. }
Add Comment
Please, Sign In to add comment