简体   繁体   English

用泰勒级数计算 sin(x)

[英]Calculating sin(x) with Taylor series

Task: According to the Taylor Series of sin(x) calculate with using a double function named mysin pass it to a double variable.任务:根据泰勒级数 sin(x) 计算,使用名为mysin的双精度 function 将其传递给双精度变量。 Take ax value from user and use the mysin function to calculate sin(x).从用户处获取 ax 值并使用 mysin function 计算 sin(x)。

Sin()x 泰勒级数公式

Problem is program gives me wrong value of sin(x).问题是程序给了我错误的 sin(x) 值。 I have been trying to solve that issue about 4 hours but couldn't find it.我一直试图解决这个问题大约 4 个小时,但找不到。 Is it because of sin(x) function or have I missed something in my code?是因为 sin(x) function 还是我错过了代码中的某些内容?

My Code:我的代码:

#include <stdio.h>

double mysin(double x)
{
    double value = x;
    double sum = x;
    int neg_pos = 1;
    int fac = 1;
    
    int counter = 0;
    
    while(1)
    {
        neg_pos *= -1;
        fac += 2;
        
        value = value/(fac*(fac-1));
        value = value*x*x*neg_pos;

        sum += value;
        
        //printf("Hello");
        counter++;
        if (counter == 100) break;
    }
    return sum;
}

int main()
{
    double number;
    scanf("%lf",&number);
    printf("%g",mysin(number));
    //printf("%g",number);
}

The problem is that you're multiplying by neg_pos each step, which toggles between +1 and -1.问题是你每一步都乘以neg_pos ,它在 +1 和 -1 之间切换。 That means the terms change sign only half the time, whereas they should change sign each time.这意味着术语只有一半的时间改变符号,而它们每次都应该改变符号。

The fix is to just multiply by -1 each time rather than neg_pos .解决方法是每次只乘以 -1 而不是neg_pos

Here's a working, slightly-simplified form of your program that calculates sin for a range of numbers from 0 to 3, showing the stdlib calculation to compare.这是你的程序的一个工作的、稍微简化的形式,它计算从 0 到 3 的数字范围的 sin,显示要比较的 stdlib 计算。

#include <math.h>
#include <stdio.h>

double mysin(double x) {
    double value = x;
    double sum = x;
    int fac = 1;
    
    for (int counter = 0; counter < 100; counter++) {
        fac += 2;
        value = -value*x*x/fac/(fac-1);
        sum += value;
    }
    return sum;
}

int main() {
    for (double x = 0.0; x < 3.0; x += 0.1) {
       printf("%g: %g %g\n", x, mysin(x), sin(x));
    }
}

You can also avoid the separate fac and counter variables, perhaps like this:您还可以避免使用单独的faccounter变量,可能像这样:

double mysin(double x) {
    double term=x, sum=x;
    for (int f = 0; f < 100; f++) {
        term = -term*x*x/(2*f+2)/(2*f+3);
        sum += term;
    }
    return sum;
}

From what I'm understanding you are not calculating the power correctly Firtsly use this:据我了解,您没有正确计算功率首先使用这个:

#include <math.h>

Then create a factorial function:然后创建一个阶乘 function:

int factorial(int x){
   int result = 1;
   for(int i = 1; i < x; i++){
      result += result * i;
   }
   return result;
}

And finally:最后:

while(1)
 {
    neg_pos *= -1;
    fac += 2;
    
    power = pow(x,fac);
    fac = factorial(fac);

    sum += power/fac;
    
    //printf("Hello");
    counter++;
    if (counter == 100) break;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM