泰勒级数 C++另一个值

发布于 2025-01-12 08:08:44 字数 1321 浏览 0 评论 0原文

我必须编写使用泰勒级数计算值 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

定格我的天空 2025-01-19 08:08:44

泰勒级数是指围绕特定枢轴(最常见的是 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):
enter image description here

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文