+92 332 4229 857 99ProjectIdeas@Gmail.com

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


Calculate an approximate value sine (in radians) using Taylor series
calculateSINE() calculates and returns the sine of the given value in radians and it takes the value whose sine 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 calculateSINE(double m)
{
 int x=1;
       for(int i=3;i<13;i=i+2)
       {
              m=m+pow(-1.0,x)*(pow(m,i))/calculateFACT(i);
              x++;
       }
return m;
}

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

       _getche();
       return 0;
}

Output
0
0.0998334
0.198669
0.295519
0.389408
0.479376
0.564472
0.643739
0.716201
0.780848
0.836626

Related articles

0 comments: