Advertisement
dark-s0ul

lab1

Jan 17th, 2018
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.90 KB | None | 0 0
  1. #define _USE_MATH_DEFINES
  2.  
  3. #include <cmath>
  4. #include <cstdio>
  5.  
  6. double mysinh(double x, double eps, int &k, double &rn) {
  7.     double s = 0;
  8.     double u = x;
  9.     k = 1;
  10.  
  11.     while (fabs(u) >= eps) {
  12.         s += u;
  13.         u = x * x / (2 * k * (2 * k + 1)) * u;
  14.         k++;
  15.     }
  16.  
  17.     rn = u;
  18.     return s;
  19. }
  20.  
  21. int main() {
  22.     double a = -1.8;
  23.     double b = 1.9;
  24.     double x = (a + b) * 0.5;
  25.     double h = (b - a) / 9.0;
  26.  
  27.     int n = 0;
  28.     double rn = 0;
  29.  
  30.     printf("|  eps  | n  |  abs error  |   rem term   |\n");
  31.     for (double eps = 1e-2; eps > 1e-14; eps *= 1e-2) {
  32.         double y = mysinh(x, eps, n, rn);
  33.         printf("| %.0e | %2i | %8.5e | %12.5e |\n", eps, n, fabs(sinh(x) - y), fabs(rn));
  34.     }
  35.  
  36.     printf("\n|   xi   |  abs error  |   rem term   |\n");
  37.     for (int i = 0; i < 10; i++) {
  38.         double x = a + h * i;
  39.         double y = mysinh(x, 1e-8, n, rn);
  40.         printf("| %6.2f | %8.5e | %12.5e |\n", x, fabs(sinh(x) - y), fabs(rn));
  41.     }
  42.     return 0;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement