Advertisement
Caneq

lb2.2.8.3

Oct 29th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.12 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #define _USE_MATH_DEFINES
  4. #include <math.h>  
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10.     setlocale(LC_ALL, "rus");
  11.     double xn = 2 * M_PI, epsilon = 1e-6;
  12.     int i = 0;
  13.     double x = 0;
  14.     while (x <= xn) {
  15.         double cosxt = 1;
  16.         int k = 2;
  17.         double el = -x*x / 2;
  18.         cosxt += el;
  19.  
  20.         while (fabs(el) > epsilon) {
  21.             k += 2;
  22.             el = -el*x*x / (k*(k - 1)); //Слагаемое для cos
  23.             cosxt += el;
  24.         }
  25.  
  26.         cout << "Количество слагаемых для косинуса = " << k / 2 << endl;
  27.         double sinxt = x;
  28.         k = 3;
  29.         el = -x*x*x / 6; //fact(3) = 6
  30.         sinxt += el;
  31.  
  32.         while (fabs(el) > epsilon) {
  33.             k += 2;
  34.             el = -el*x*x / (k*(k - 1));  //Слагаемое для sin
  35.             sinxt += el;
  36.         }
  37.  
  38.         cout << "Количество слагаемых для синуса   = " << (k - 1) / 2 << endl;
  39.         double xt = cosxt + sinxt;
  40.         cout << "Значение по Тейлору                 = " << cosxt + sinxt << endl;
  41.         cout << "Значение по встроенным функциям     = " << cos(x) + sin(x) << endl;
  42.         i++;
  43.         x = i*0.1;
  44.     }
  45.     return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement