C 泰勒级数

发布于 2024-12-01 13:29:30 字数 559 浏览 3 评论 0原文

我正在尝试编写一个程序来使用泰勒级数计算 cos(x) 函数,到目前为止我已经得到了:

int factorial(int a){

if(a < 0)
    return 0;
else if(a==0 || a==1)
    return 1;
else
    return a*(factorial(a-1));
}

double Tserie(float angle, int repetitions){
    double series = 0.0;
    float i;

for(i = 0.0; i < repeticiones; i++){
    series += (pow(-1, i) * pow(angle, 2*i))/factorial(2*i);
    printf("%f\n", (pow(-1, i) * pow(angle, 2*i))/factorial(2*i));
}
return series;

}

对于我的示例,我使用角度 = 90 和重复 = 20 来计算 cos(90)但这是无用的,我只是不断获得接近无限的值,任何帮助将不胜感激。

I'm trying to make a program to calculate the cos(x) function using taylor series so far I've got this:

int factorial(int a){

if(a < 0)
    return 0;
else if(a==0 || a==1)
    return 1;
else
    return a*(factorial(a-1));
}

double Tserie(float angle, int repetitions){
    double series = 0.0;
    float i;

for(i = 0.0; i < repeticiones; i++){
    series += (pow(-1, i) * pow(angle, 2*i))/factorial(2*i);
    printf("%f\n", (pow(-1, i) * pow(angle, 2*i))/factorial(2*i));
}
return series;

}

For my example I'm using angle = 90, and repetitions = 20, to calculate cos(90) but it's useless I just keep getting values close to the infinite, any help will be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

余生共白头 2024-12-08 13:29:30

一方面,角度以弧度为单位,因此对于 90 度角,您需要传递 M_PI/2

另外,对于像阶乘这样简单的东西,你应该避免使用递归函数,迭代地编写它会花费 1/4 的精力,而且性能会好得多。您实际上甚至不需要它,您可以将阶乘保留在临时变量中,然后在每一步将其乘以 2*i*(2*i-1) 。请记住,在此步骤中您很快就会遇到可再现性/精确度的障碍。

您也不需要实际调用 pow 来计算 -1 的 i 次方,一个简单的 i%2?1:-1就足够了。这样,速度会更快,并且不会随着 i 的增加而失去精度。

哦,不要让 i float,它是一个整数,让它成为一个整数。事实上,你已经泄漏了很多精度,为什么会让情况变得更糟......

最重要的是,你将 cos 逼近于 0 左右,但将其称为 pi/2 。这样做你会得到非常高的错误。

For one thing, the angle is in radians, so for a 90 degree angle, you'd need to pass M_PI/2.

Also, you should avoid recursive functions for something as trivial as factorials, it would take 1/4 the effort to write it iteratively and it would perform a lot better. You don't actually even need it, you can keep the factorial in a temporary variable and just multiply it by 2*i*(2*i-1) at each step. Keep in mind that you'll hit a representability/precision wall really quickly at this step.

You also don't need to actually call pow for -1 to the power of i, a simple i%2?1:-1 would suffice. This way it's faster and it won't lose precision as you increase i.

Oh and don't make i float, it's an integer, make it an integer. You're leaking precision a lot as it is, why make it worse..

And to top it all off, you're approximating cos around 0, but are calling it for pi/2. You'll get really high errors doing that.

浅沫记忆 2024-12-08 13:29:30

泰勒级数用于数学余弦函数,其参数以弧度为单位。所以 90 可能并不像你想象的那样。

此外,参数从 0 开始的时间越长,级数需要的项就越多。一般来说,在您开始看到连续项变小之前,项的数量需要与参数的大小相当,并且比顺序中的项要多得多。以获得收敛。遗憾的是,对于 x=90,20 是很少使用的术语。

另一个问题是您将阶乘计算为 int。阶乘函数增长得非常快——已经达到 13 了!普通的 C int (在 32 位机器上)会溢出,因此第六个之外的项无论如何都将是完全错误的。

事实上,阶乘和 90 的幂很快就会变得太大,甚至无法用 double 来表示。如果您希望有机会看到级数收敛,则不得从头开始计算每一项,而是使用以下公式从前一项导出它:

nextTerm = - prevTerm * x * x / (2*i-1) / (2*i);

The Taylor series is for the mathematical cosine function, whose arguments is in radians. So 90 probably doesn't mean what you thought it meant here.

Furthermore, the series requires more terms the longer the argument is from 0. Generally, the number of terms need to be comparable to the size of the argument before you even begin to see the successive terms becoming smaller, and many more than that in order to get convergence. 20 is pitifully few terms to use for x=90.

Another problem is then that you compute the factorial as an int. The factorial function grows very fast -- already for 13! an ordinary C int (on a 32-bit machine) will overflow, so your terms beyond the sixth will be completely wrong anyway.

In fact the factorials and the powers of 90 quickly become too large to be represented even as doubles. If you want any chance of seeing the series converge, you must not compute each term from scratch but derive it from the previous one using a formula such as

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