Advertisement
dark-s0ul

lab1

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