C ++ & quot; Построение серии для решения функции & quot; Почему мое приближение выключено?

Так что мой вопрос касается в основном моей продукции. У меня нет истинного значения atan (x), что бы это ни было. Я знаю, это должно быть просто, что я делаю неправильно?

Пожалуйста, введите число, к которому вы хотите применить arctan, как tan ^ (- 1) * (x)
помните, х не может быть больше 1 или меньше 1: .5

Примерное значение 0,5000 составляет 0,4794.
Истинное значение arctan 0.5000 равно 0.4636

Нажмите любую клавишу для продолжения . , ,

#include <cstdlib>
#include <cmath>
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
double sum = 0.0, x, denom, term, z;
int n, sign = -1;
char again;

do
{
cout << "Please enter the number you would like to apply arctan to"<< " as tan^(-1)*(x) \nremember, x cannot be greater than 1 or less than negative 1:";
cin >> x;
cout << endl;
while (x > 1 || x < -1)
{
cout << "Try again, the domain for arctan is ( -1 < x < 1): ";
cin >> x;
cout << endl;
}

if (x > -1 && x < 1)
{
for (n = 0; n < 19; n++)
{
sign = sign * (-1);
z = (n * 2) + 1;
if (n == 0)
denom = 1.0;
else
denom = denom*(2*n)*(2*n+1);
term = sign*pow(x, z)/denom;
sum = sum + term;
}
}

cout << fixed << setprecision(4) << "The approx arctan of " << x << " is " << sum << endl;
cout << "The true value arctan of " << x << " is " << atan(x) << endl << endl;

cout<<"Press Enter";
cin.get();
cin.ignore();
cout << endl;

cout << "Would you like to continue (Y/N)?";
cin >> again;
cout << endl;
}
while (again == 'Y' || again == 'y');

return 0;
}

-1

Решение

Ваш код расширения Тейлора неверен.

замещать denom = denom*(2*n)*(2*n+1); с denom = 2*n+1; и это работает

1

Другие решения


По вопросам рекламы ammmcru@yandex.ru
Adblock
detector