Advertisement
Guest User

lab 8.2

a guest
Dec 14th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3. #include <iomanip>
  4. using namespace std;
  5. typedef double(*uf)(double, double, int &);
  6. void tabl(double, double, double, double, uf);
  7. double y(double, double, int &);
  8. double s(double, double, int &);
  9. int main()
  10. {
  11. cout << setw(8) << "x" << setw(15) << "y(x)" << setw(10) << "k" << endl;
  12. tabl(0.1, 1, 0.1, 0.00001, y);
  13. cout << endl;
  14. cout << setw(8) << "x" << setw(15) << "s(x)" << setw(10) << "k" << endl;
  15. tabl(0.1, 1, 0.1, 0.00001, s);
  16. return 0;
  17. }
  18. void tabl(double a, double b, double h, double eps, uf fun)
  19. {
  20. int k = 0;
  21. double sum;
  22. for (double x = a; x<b + h / 2; x += h)
  23. {
  24. sum = fun(x, eps, k);
  25. cout << setw(8) << x << setw(15) << sum << setw(10) << k << endl;
  26. }
  27. }
  28. double y(double x, double eps, int &k)
  29. {
  30. return ((exp(x) + exp(-x)) / 2);
  31. }
  32. double s(double x, double eps, int &k)
  33. {
  34. double a, c, sum;
  35. sum = a = c =1;
  36. k = 1;
  37. while (fabs(c)>eps)
  38. {
  39. c = x*x / (2 * k*(2 * k - 1));
  40. a *= c;
  41. sum += a;
  42. k++;
  43. }
  44. return sum;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement