Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- #define _USE_MATH_DEFINES
- #include <math.h>
- using namespace std;
- int main()
- {
- setlocale(LC_ALL, "rus");
- double xn = 2 * M_PI, epsilon = 1e-6, el;
- int i = 0;
- double x = 0;
- while (x <= xn) {
- double cosxt = 1;
- int ii = 1;
- do { //cos по Тейлору
- double fact = 1;
- for (int i = 2; i <= ii*2; i++) { //Вычисление факториала
- fact *= i;
- }
- char sig = ii % 2 == 0 ? 1 : -1;
- el = sig * pow(x, ii * 2) / fact; //Слагаемое для cos
- cosxt += el;
- ii++;
- } while (fabs(el) > epsilon);
- cout << "Количество слагаемых для косинуса = " << ii << endl;
- double sinxt = x;
- ii = 1;
- do { //sin по Тейлору
- double fact = 1;
- for (int i = 2; i <= ii * 2 + 1; i++) { //Вычисление факториала
- fact *= i;
- }
- char sig = ii % 2 == 0 ? 1 : -1;
- el = sig * pow(x, ii * 2 + 1) / fact; //Слагаемое для sin
- sinxt += el;
- ii++;
- } while (fabs(el) > epsilon);
- cout << "Количество слагаемых для синуса = " << ii << endl;
- double xt = cosxt + sinxt;
- cout << "Значение по Тейлору = " << xt << endl;
- cout << "Значение по встроенным функциям = " << cos(x) + sin(x) << endl;
- i++;
- x = i*0.1;
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment