C ++ Taylor系列和Trig表

时间:2014-02-04 04:58:12

标签: c++ taylor-series

好的,我正在创建一个程序,在单个表中输出余弦,sin和其他触发值,以0为增量,以15为增量。作为赋值的一部分,我们需要显示系统余弦和系统正弦使用C ++中给定的sin和cos命令,以及使用泰勒系列计算的正弦和余弦值。当我使用泰勒系列计算正弦和余弦值时,我的程序出错了。我评论了一些代码,希望能让它更容易查看。任何和所有建议将不胜感激。此外,我将发布程序的示例输出。

 Angle  Sys Cos   Cosine  Sys Sin    Sine  Tangent   Cotangent   Secant Cosecant

     0   1.0000   1.0000   0.0000   0.0000   0.0000      inf
    15   0.9659   0.0341   0.2588  -0.9659  -0.9659     -inf
    30   0.8660   0.1340   0.5000  -0.8660  -0.8660     -inf
    45   0.7071   0.2929   0.7071  -0.7071  -0.7071     -inf
    60   0.5000   0.5000   0.8660  -0.5000  -0.5000     -inf
    75   0.2588   0.7412   0.9659  -0.2588  -0.2588     -inf
    90   0.0000   1.0000   1.0000   0.0000   0.0000     -inf
   105  -0.2588   1.2588   0.9659   0.2588   0.2588     -inf
   120  -0.5000   1.5000   0.8660   0.5000   0.5000     -inf
   135  -0.7071   1.7071   0.7071   0.7071   0.7071     -inf
   150  -0.8660   1.8660   0.5000   0.8660   0.8660     -inf
   165  -0.9659   1.9659   0.2588   0.9659   0.9659     -inf
   180  -1.0000   2.0000   0.0000   1.0000   1.0000     -inf
   195  -0.9659   1.9659  -0.2588   0.9659   0.9659     -inf
   210  -0.8660   1.8660  -0.5000   0.8660   0.8660     -inf
   225  -0.7071   1.7071  -0.7071   0.7071   0.7071     -inf
   240  -0.5000   1.5000  -0.8660   0.5000   0.5000     -inf
   255  -0.2588   1.2588  -0.9659   0.2588   0.2588     -inf
   270  -0.0000   1.0000  -1.0000  -0.0000  -0.0000     -inf
   285   0.2588   0.7412  -0.9659  -0.2588  -0.2588     -inf
   300   0.5000   0.5000  -0.8660  -0.5000  -0.5000     -inf
   315   0.7071   0.2929  -0.7071  -0.7071  -0.7071     -inf
   330   0.8660   0.1340  -0.5000  -0.8660  -0.8660     -inf
   345   0.9659   0.0341  -0.2588  -0.9659  -0.9659     -inf
   360   1.0000  -0.0000  -0.0000  -1.0000  -1.0000   0.9501

Process returned 0 (0x0)   execution time : 0.044 s
Press any key to continue.

源代码:

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

// Function Prototypes..
//  These are the only function you need to write
//      See Sample functions below main()
double Factorial(double);   // For loop version
double Sine(double);        // Taylor Series Sine
double Cosine(double);      // Taylor Series Cosine
double Tangent(double);     // Tangent(x) = Sine(x)/Cosine(x)
double Cotangent(double);   // Cotangent(x) = Cosine(x)/Sine(x)
double Secant(double);      // Secant(x) = 1.0/Cosine(x)
double Cosecant(double);    // Cosecant(x) = 1.0/Sine(x)

void PrintTrigTable(void);  // Print Trignometric Table


// This main function is complete
int main()
{
    PrintTrigTable();

    return 0;
}

// PrintTrigTable - write this function
void PrintTrigTable()
{
cout << fixed<<setw(6)<<"Angle";
cout <<fixed << setw(9) << "Sys Cos" << setw(9) << "Cosine" << setw(9) << "Sys Sin" << setw(8) << "Sine";
cout << fixed << setw(9) << "Tangent" << setw(12) << "Cotangent" << setw(9) << "Secant" << setw(9) << "Cosecant" <<endl;
for (double x = 0.0; x <= 360.0; x += 15.0)

{

    cout << fixed << setw (6) << setprecision(0) << x;

    cout << fixed << setw (9) << setprecision(4)<< cos(x*3.1415926/180);

    cout << fixed << setw (9) << setprecision(4)<< Cosine(x);

    cout << fixed << setw (9) << setprecision(4)<< sin(x*3.1415926/180);

    cout << fixed << setw (9) << setprecision(4)<< Sine(x);

    cout << fixed << setw (9) << setprecision(4)<< Tangent(x);

    cout << fixed << setw (9) << setprecision(4)<< Cotangent(x) <<endl;

    //cout << fixed << setw (9) << setprecision(4)<< Secant(x)<<endl;

    //cout << fixed << setw (9) << setprecision(4)<< Cosecant(x) <<endl;

}
}
// Factorial - write this function
double Factorail(double angle)
{
double fact = 1;
for (double i =1; i <= angle; i++)
fact *=i;
return(fact);
}

// Sine - write this function
double Sine(double angle)
{
double PI = 3.1415926;
double mysin= 0, mysin2= 9999;
double anglerad = angle*PI/180.0;
 double  c =1., counter, fact;
while (abs(mysin2-mysin)> .0001)
{

     mysin2= mysin;
     fact = 1;
    for ( double k =1; k <= counter; k++)
    fact *=k;
    mysin= mysin + pow(-1,c)*(pow(anglerad,counter)/fact);
    c = c+1;
    counter = counter +2;

}
return(mysin);
}

double Cosine(double angle)
{
double PI = 3.1415926;
double mycos= 1, mycos2= 9999;
double anglerad = angle*PI/180.0;
double c =1, counter, fact;
while (abs(mycos2-mycos)> .0001)
{

     mycos2= mycos;
     fact = 1;
    for ( double k =1; k <= counter; k++)
    fact *=k;
    mycos= mycos + pow(-1,c)*(pow(anglerad,counter)/fact);
    c = c+1;
    counter = counter +2;

}
return(mycos);
}
// Tangent - Tangent(x) = Sine(x)/Cosine(x)
double Tangent(double angle)
{
    double inf = (Sine(angle)/Cosine(angle));
    if (inf > 100)
    {
        inf =1.0/0.0;
    }
    else if (inf < -100)
    {
        inf = -1.0/0.0;
    }
    return inf;
}

// Cotangent - write this function -
double Cotangent(double angle)
{
    double inf = (Cosine(angle)/Sine( angle));
    if (inf > 100)
    {
        inf =1.0/0.0;
    }
    else if (inf < -100)
    {
        inf = -1.0/0.0;
    }
    return inf;
}

// Secant - Secant(x) = 1.0/Cosine(x)
double Secant(double angle)
{
    double inf = (1.0/Cosine(angle));
    if (inf > 100)
    {
        inf =1.0/0.0;
    }
    else if (inf < -100)
    {
        inf = -1.0/0.0;
    }
    return inf;
}

// Cosecant - write this function
double Cosecant(double angle)
{
   double inf = (1.0/Sine(angle));
    if (inf > 100)
    {
        inf =1.0/0.0;
    }
    else if (inf < -100)
    {
        inf = -1.0/0.0;
    }
    return inf;
}

0 个答案:

没有答案