在C ++中使用Taylor's Series查找sinx值

时间:2014-03-14 22:14:02

标签: c++ sin taylor-series

我正在尝试用C ++编写一个代码块,用Taylor系列计算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;
}

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

  

这是我的自制罪:南晟   这是API罪:0.850904

我知道我犯了一个很大的逻辑错误,但我找不到它。这是我使用C ++的第二周。我对Java更熟悉。我编写了同样的东西,它的工作绝对完美。答案相互匹配。 感谢您的时间和关注!

3 个答案:

答案 0 :(得分:3)

    fact
  1. ,您错过了回复: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有疑问,请参阅文档,例如here

答案 1 :(得分:0)

您的代码似乎有一些逻辑错误。这是我纠正的一个:

#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 value\t";
    cin>>n;

    n = radians(n);
    cout<< "\nthe value in radians is\t"<< n << "\n";

    output = sin(n);

    cout<< "\nsine of the given value is\t"<< output;
    return 0;
}

这个程序的目的是使用自定义函数而不是库来简化其他人的学习。

此程序中有四个用户定义的函数。前三个用户定义的函数&#39;弧度()&#39;,&#39; factorial()&#39;,&#39; power() &#39;,显然是执行操作的简单函数,顾名思义。

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

答案 2 :(得分:0)

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;
}