泰勒级数 C++另一个值
我必须编写使用泰勒级数计算值 cos(x) 的程序。对于 a= -7 到 7 程序工作。 问题是当我使用值 a>7 和相同的 a<-7 时。
#include <iostream>
#include <cmath>
using namespace std;
unsigned long long silnia(int n)
{
unsigned long long a = 1;
while(n)
a *= n--;
return a;
}
void taylor(int x)
{
long double suma=0;
for (int n=0; n<=10; n++)
{
suma+=pow(-1,n)*(pow(x,2*n)/silnia(2*n));
cout << pow(-1,n) << " * " << pow(x,2*n) <<" / " << silnia(2*n) << " = " << pow(-1,n)*(pow(x,2*n)/silnia(2*n))<<endl;
}
cout << "taylor: "<<suma<< endl;
}
int main() {
int a=5;
taylor(a);
cout << "cos: " << cos(a);
}
输出 a=5 的 onli taylor 和 cos:
taylor: 0.283664
cos: 0.283662
输出 a = 9
1 * 1 / 1 = 1
-1 * 81 / 2 = -40.5
1 * 6561 / 24 = 273.375
-1 * 531441 / 720 = -738.112
1 * 4.30467e+007 / 40320 = 1067.63
-1 * 3.48678e+009 / 3628800 = -960.864
1 * 2.8243e+011 / 479001600 = 589.621
-1 * 2.28768e+013 / 87178291200 = -262.414
1 * 1.85302e+015 / 20922789888000 = 88.5647
-1 * 1.50095e+017 / 6402373705728000 = -23.4436
1 * 1.21577e+019 / 2432902008176640000 = 4.99719
taylor: -0.149111
cos: -0.91113
抱歉我的英语不好,感谢您的帮助!
I have to write program which calculates value cos(x) with use Taylor Series. for a= -7 to 7 program work.
problem is when i use value a>7 and the same a<-7.
#include <iostream>
#include <cmath>
using namespace std;
unsigned long long silnia(int n)
{
unsigned long long a = 1;
while(n)
a *= n--;
return a;
}
void taylor(int x)
{
long double suma=0;
for (int n=0; n<=10; n++)
{
suma+=pow(-1,n)*(pow(x,2*n)/silnia(2*n));
cout << pow(-1,n) << " * " << pow(x,2*n) <<" / " << silnia(2*n) << " = " << pow(-1,n)*(pow(x,2*n)/silnia(2*n))<<endl;
}
cout << "taylor: "<<suma<< endl;
}
int main() {
int a=5;
taylor(a);
cout << "cos: " << cos(a);
}
output onli taylor and cos for a=5:
taylor: 0.283664
cos: 0.283662
output for a = 9
1 * 1 / 1 = 1
-1 * 81 / 2 = -40.5
1 * 6561 / 24 = 273.375
-1 * 531441 / 720 = -738.112
1 * 4.30467e+007 / 40320 = 1067.63
-1 * 3.48678e+009 / 3628800 = -960.864
1 * 2.8243e+011 / 479001600 = 589.621
-1 * 2.28768e+013 / 87178291200 = -262.414
1 * 1.85302e+015 / 20922789888000 = 88.5647
-1 * 1.50095e+017 / 6402373705728000 = -23.4436
1 * 1.21577e+019 / 2432902008176640000 = 4.99719
taylor: -0.149111
cos: -0.91113
sorry for my bad english and thanks for help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
泰勒级数是指围绕特定枢轴(最常见的是 0)近似函数。但是,当你离这个支点越来越远时,近似值就会变得越来越错误。
这是由 Wolfram alpha 生成的图,显示了在不同程度(=导数的最高指数)下起作用的
cos(x)
和 Tailor Series 近似:,最佳近似值(10 度)仅接近 ~±6 的值。如果您想要更接近真实值的值,则必须增加步数或更改枢轴!
要了解有关裁缝系列的更多信息,您可以进行更多研究或观看此直观视频 3b1b。
Taylor series are mean to approximate a function around a specific pivot (most commonly 0). But, as you go further from that pivot, the approximation becomes more and more wrong.
Here is a plot, generated by Wolfram alpha, which shows
cos(x)
and Tailor Series approximation that function at varying degrees(=highest exponent of derivative):As you can see, the best approximation(10th degree) is only close for values up to ~±6. If you want values that are closer to the true values, you will have to increase your steps or change your pivot!
To learn more about Tailor Series you can do some more research or watch this intuitive video by 3b1b.