在C++用泰勒系列寻找sinx值

In C++ finding sinx value with Taylor's Series

本文关键字:寻找 sinx 系列 C++      更新时间:2023-10-16

我正试图用c++写一段代码,用泰勒级数计算sinX值。

#include <iostream>
using namespace std;
// exp example
#include <cstdio>      // printf
#include <cmath>       //  exp
double toRadians(double angdeg)         //convert to radians to degree
{                                       //x is in radians
    const double PI = 3.14159265358979323846;
    return angdeg / 180.0 * PI;
}
double fact(double x)       //factorial function 
{                           //Simply calculates factorial for denominator
    if(x==0 || x==1)
        return 1;
    else
        x * fact(x - 1);
}
double mySin(double x)      //mySin function
{
    double sum = 0.0;
    for(int i = 0; i < 9; i++)
    {
        double top = pow(-1, i) * pow(x, 2 * i + 1);  //calculation for nominator
        double bottom = fact(2 * i + 1);              //calculation for denominator
        sum = sum + top / bottom;                     //1 - x^2/2! + x^4/4! - x^6/6!
    }
    return sum;
}
int main()
{
    double param = 45, result;
    result = mySin(toRadians(param)); //This is my sin value
    cout << "Here is my homemade sin : " << result << endl;
    result = sin(param);              //This is library value
    cout << "Here is the API sin : " << result << endl;
    return 0;
}

所以我的程序工作没有任何错误。我的输出就是:

这是我自制的sin: nan
这是API sin:0.850904

我知道我犯了一个很大的逻辑错误,但是我找不出来。这是我使用c++的第二周。我对Java比较熟悉。我也写了同样的代码,而且运行得非常完美。答案是一致的。感谢您的时间和关注!

  1. fact中,您错过了返回:x*fact(x-1);应该是return x*fact(x-1);。如果打开警告,可以看到编译器发出抱怨。例如,在GCC中,调用g++ -Wall program.cpp将得到Warning: control reaches end of non-void function作为阶乘函数。

  2. API sin也需要以弧度为单位的角度,因此将result=sin(param);更改为result=sin(toRadians(param));。一般来说,如果对API有疑问,请参阅文档,如这里。

你的代码似乎有一些逻辑错误。下面是我的更正:

#include <iostream>
using namespace std;
double radians(double degrees)  // converts degrees to radians
{
    double radians;
    double const pi = 3.14159265358979323846;
    radians = (pi/180)*degrees;
    return radians;
}
double factorial(int x)  //calculates the factorial
{
    double fact = 1;
    for(; x >= 1 ; x--)
    {
        fact = x * fact;
    }
    return fact;
}
double power(double x,double n)  //calculates the power of x
{
    double output = 1;
    while(n>0)
    {
         output =( x*output);
         n--;
    }
    return output;
}
float sin(double radians)  //value of sine by Taylors series
{
   double a,b,c;
   float result = 0;
   for(int y=0 ; y!=9 ; y++)
   {
      a=  power(-1,y);
      b=  power(radians,(2*y)+1);
      c=  factorial((2*y)+1);
      result = result+ (a*b)/c;
   }
   return result;
}
double n,output;
int main()
{
    cout<<"enter the valuet";
    cin>>n;
    n = radians(n);
    cout<< "nthe value in radians ist"<< n << "n";
    output = sin(n);
    cout<< "nsine of the given value ist"<< output;
    return 0;
}

这个程序的目的是使用自定义函数而不是库,使其他人更容易学习。

这个程序中有四个用户定义的函数。前三个用户定义的函数radians(), factorial(), power(),显然是简单的函数,执行的操作就像它们的名字一样。

第四个函数'sin()'以弧度为输入,弧度由函数'radians()'给出。sin函数在函数的'for(int y= 0;y!=9;y++)'循环中使用泰勒级数逐项迭代,直到第九次迭代来计算输出。for()循环迭代一般的数学表达式:Term(n)=(-1)^n .(x^(2n+1))/(2n+1)!

sin(x)= x- x^3/3! + x^5/5! -x^7/7! + x^9/9!
=x-x^3/2*3 (1- x^2/4*5 + x^4/4*5*6*7 + x^6/4*5*6*7*8*9)
=x - x^3/2*3 {1- x^2/4*5(1- x^2/6*7 + x^4/6*7*8*9)}
=x - x^3/2*3 [{1- x^2/4*5 ( 1- x^2/6*7 (1- x^2/8*9))}]
=x(1 - x^2/2*3 [{1- x^2/4*5 ( 1- x^2/6*7 (1- x^2/8*9))}])
double sin_series_recursion(double x, int n){
    static double r=1;
    if(n>1){
        r=1-((x*x*r)/(n*(n-1)));
        return sin_series_recursion(x,n-2);

    }else return r*x;
}