簡體   English   中英

泰勒級數的此代碼不適用於n = 1或0以外的任何其他值。為什么?

[英]This code of taylor series doesn't work for n= 1 or anything other than 0. Why?

首先,讓我告訴您,我正在學習編程。

今天,我嘗試通過使用taylor序列找到余弦的近似值。 當我設置n = 0時,我的代碼給出了1的正確結果。但是當我設置n = 1或其他值時,我的代碼沒有給出正確的結果。

我不明白問題出在哪里。 有人可以幫忙嗎?

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(int argc, char *argv[])
{
    float xnot = atof(argv[1]);
    float n = atof(argv[2]);
    float cosine = cos(xnot*(3.14159265/180));
    float result;
    printf("%.4f\n", cosine);
    float min;


    float d, c, b;
    c = 1;
    d = 2 * n; 
    for(b = 1; b <= d; b++){
        c = c * b; /*value of the factorial is in c*/
    }
    c = c;
    float power;
    power = pow((-1), n);
    xnot = pow(xnot, 2*n);


    for(min = 0; min <= n; min++)
    {

        result += ((power * xnot) / c);
    }
    printf("%.4f", result);
}

實施泰勒級數時,必須為每個“ n”值重新計算項的值。 在這里,看起來您已經計算出-1^n的值(作為xnot )以獲取n的最大值,然后您每次乘以該值即可。 錯了 x^2n / (2n)!的值相同x^2n / (2n)! -遞增n ,必須為n每個值重新計算,然后求和。

祝你好運。

您需要在for循環內重做所有計算。 保留盡可能多的原始代碼,可能類似於:

int n = atoi(argv[2]);  // n is an integer
...
...
float result = 1;  // The first term (n=0) gives 1

for(int i = 1; i <= n; i++)   // Start the loop from 1
{
    float d, c, b;
    c = 1;
    d = 2 * i;     // Use i instead of n
    for(b = 1; b <= d; b++){
        c = c * b; /*value of the factorial is in c*/
    }
    float power;
    power = pow((-1), i);    // Use i instead of n
    xnot = pow(xnot, 2*i);   // Use i instead of n

    result += ((power * xnot) / c);
}

可以對代碼進行優化(以提高性能和精度),但是如上所述,我試圖使其與原始代碼保持接近。

使用泰勒級數計算正弦或余弦時,還需要考慮角度象限,以最大程度地減少誤差增長。 以下是一個簡短的示例:

#define TSLIM 20    /* Series Limit (no. of terms) */
...
/** cos with taylor series expansion to n = TSLIM
 *  (no function reliance, quadrants handled)
 */
double cosenfq (const double deg)
{
    double fp = deg - (int64_t)deg, /* save fractional part of deg */
        qdeg = (int64_t)deg % 360,  /* get equivalent 0-359 deg angle */
        rad, cose_deg = 1.0;        /* radians, cose_deg */
    int pos_quad = 1,               /* positive quadrant flag 1,4  */
        sign = -1;                  /* taylor series term sign */

    qdeg += fp;                     /* add fractional part back to angle */

    /* get equivalent 0-90 degree angle, set pos_quad flag */
    if (90 < qdeg && qdeg <= 180) {         /* in 2nd quadrant */
        qdeg = 180 - qdeg;
        pos_quad = 0;
    }
    else if (180 < qdeg && qdeg <= 270) {   /* in 3rd quadrant */
        qdeg = qdeg - 180;
        pos_quad = 0;
    }
    else if (270 < qdeg && qdeg <= 360)     /* in 4th quadrant */
        qdeg = 360 - qdeg;

    rad = qdeg * M_PI / 180.0;      /* convert to radians */

    /* compute Taylor-Series expansion for sine for TSLIM / 2 terms */
    for (int n = 2; n < TSLIM; n += 2, sign *= -1) {
        double p = rad;
        uint64_t f = n;

        for (int i = 1; i < n; i++)     /* pow */
            p *= rad;

        for (int i = 1; i < n; i++)     /* nfact */
            f *= i;

        cose_deg += sign * p / f;       /* Taylor-series term */
    }

    return pos_quad ? cose_deg : -cose_deg;
}

限制為20個學期,最大錯誤約為1.2E-15 (與math.h cos()相比)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM