简体   繁体   English

如何编写一个程序来估计正弦函数作为 C 中的泰勒级数?

[英]How to write a program to estimate the sine function as a Taylor series in C?

I'm new to programming and I cannot figure out what is wrong with my code.我是编程新手,无法弄清楚我的代码有什么问题。 If there is any way that someone could help me understand what is wrong with my program I would really appreciate it.如果有人可以帮助我了解我的程序有什么问题,我将不胜感激。 I have already been working on this problem for two hours and I know I am making it harder than it needs to be.我已经在这个问题上工作了两个小时,我知道我让它变得比需要的更难。 This is my new code.这是我的新代码。 my new output is 0.000000476837158.我的新输出是 0.000000476837158。

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main() {
         double x, sinx, z, factorial, term;
         int j, n, i;
         x=0.5;
         n=10;
         i=0;
         z=sin(0.5);
         for(int i=0; i<=n; i++){
                      factorial = 1;
                      for(j=2*i +1; j<=i; j++)
                      {
                               factorial = factorial * j;
                               }
                      term=(pow(-1,i)*pow(x,2*i+1))/(factorial);
                      sinx=term++;
                      i=i++;
                                   }
                      printf("sin(0.5) is approximately %.15lf\n", sinx);
                      printf("sin(0.5) by default function is %.15lf\n", z);
                      system("pause");
                      return 0;
                      }

This loop doesn't make sense.这个循环没有意义。

 for(j=0; j<=i; j++){
     factorial=2*j+1;
     }

It doesn't do anything different than它没有做任何不同的事情

factorial = 2*i+1;

If you wanted to calculate a factorial(like i! ) than I'd do the following如果您想计算阶乘(如i! ),我会执行以下操作

// initialize `factorial`
factorial = 1;
for(j=1; j<=i; j++)
{
    //multiply `factorial` by `j`.  
    factorial = factorial * j;
}

you keep using sinx and term , but they're never initialized.您一直在使用sinxterm ,但它们从未被初始化。 This means that they'll contain garbage values.这意味着它们将包含垃圾值。


i++ incriments on it's own, you shouldn't have to use any extra assignment operators. i++ incriments 是它自己的,你不应该使用任何额外的赋值运算符。

change改变

i=i++;

to simply简单地

i++;

or better yet, use a for loop instead of a while loop或者更好的是,使用for循环而不是while循环

for(int i=0; i<=n; i++)

The first occurrence of this statement此语句第一次出现

     sinx=sinx+term;

will be one of your problems since neither of the variables on the rhs of the assignment has been initialised when you first use them.将是您的问题之一,因为当您第一次使用它们时,赋值的rhs上的变量都没有被初始化。 Subsequent executions of the same line, inside the loop, will propagate the junk value of sinx throughout the computation.在循环内对同一行的后续执行将在整个计算过程中传播sinx的垃圾值。

I expect there are other problems too but haven't examined the code very closely.我预计还有其他问题,但还没有非常仔细地检查代码。

Your i th term is -1 i x 2i+1 /i, which grows fast .你的i 项是 -1 i x 2i+1 /i,它增长得很快 You should have printed out the first couple of terms to discover the problem.您应该打印出前几个术语来发现问题。

(There are other problems too, like i=i++; , but they can wait.) (还有其他问题,比如i=i++; ,但他们可以等待。)

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

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