Caneq

lb2.2.8.2

Oct 29th, 2018
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.41 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, el;
  12.     int i = 0;
  13.     double x = 0;
  14.     while (x <= xn) {
  15.         double cosxt = 1;
  16.         int ii = 1;
  17.             do { //cos по Тейлору
  18.                 double fact = 1;
  19.                     for (int i = 2; i <= ii*2; i++) { //Вычисление факториала
  20.                         fact *= i;
  21.                     }
  22.             char sig = ii % 2 == 0 ? 1 : -1;
  23.             el = sig * pow(x, ii * 2) / fact; //Слагаемое для cos
  24.             cosxt += el;
  25.             ii++;
  26.             } while (fabs(el) > epsilon);
  27.             cout << "Количество слагаемых для косинуса = " << ii << endl;
  28.         double sinxt = x;
  29.         ii = 1;
  30.         do { //sin по Тейлору
  31.             double fact = 1;
  32.             for (int i = 2; i <= ii * 2 + 1; i++) { //Вычисление факториала
  33.                 fact *= i;
  34.             }
  35.             char sig = ii % 2 == 0 ? 1 : -1;
  36.             el = sig * pow(x, ii * 2 + 1) / fact; //Слагаемое для sin
  37.             sinxt += el;
  38.             ii++;
  39.         } while (fabs(el) > epsilon);
  40.             cout << "Количество слагаемых для синуса   = " << ii << endl;
  41.         double xt = cosxt + sinxt;
  42.         cout << "Значение по Тейлору                 = " << xt << endl;
  43.         cout << "Значение по встроенным функциям     = " << cos(x) + sin(x) << endl;
  44.         i++;
  45.         x = i*0.1;
  46.     }
  47.     return 0;
  48. }
Add Comment
Please, Sign In to add comment