+92 332 4229 857 99ProjectIdeas@Gmail.com

Calculating cosine (in radians) using Taylor series (C++)


Calculate an approximate value cosine (in radians) using Taylor series
calculateCOSINE() calculates and returns the cosine of the given value in radians and it takes the value whose cosine is to be calculated. This function uses:
calculateFACT() to determine the factorial.

Code

#include "stdafx.h"
#include "iostream"
#include "conio.h"
#include "math.h"

using namespace std;

double calculateFACT(double number)
{
double factorial=1;

 for(int i=number;i>0;i--)
  factorial=factorial*i;

return factorial;
}

double calculateCOSINE(double m)
{
    double n=1;
       int x=1;

       for(int i=2;i<12;i=i+2)
       {
              n=n+pow(-1.0,x)*(pow(m,i))/calculateFACT(i);
              x++;
       }
return n;
}

int main()
{
       cout<< calculateCOSINE( 0.0 ) ; cout<<endl;
       cout<< calculateCOSINE( 0.1 ) ; cout<<endl;
       cout<< calculateCOSINE( 0.2 ) ; cout<<endl;
       cout<< calculateCOSINE( 0.3 ) ; cout<<endl;
       cout<< calculateCOSINE( 0.4 ) ; cout<<endl;
       cout<< calculateCOSINE( 0.5 ) ; cout<<endl;
       cout<< calculateCOSINE( 0.6 ) ; cout<<endl;
       cout<< calculateCOSINE( 0.7 ) ; cout<<endl;
       cout<< calculateCOSINE( 0.8 ) ; cout<<endl;
       cout<< calculateCOSINE( 0.9 ) ; cout<<endl;
       cout<< calculateCOSINE( 1.0 ) ; cout<<endl;

       _getche();
       return 0;
}

Output
1
0.995004
0.980067
0.955336
0.921061
0.877583
0.825336
0.764842
0.696707
0.62161
0.540302

Related articles

0 comments: