Calculate an approximate value tan (in radians) using Taylor series
calculateTAN() calculates and returns the tan of the given value in radians and it takes the value whose tan is to be calculated. This function uses:
calculateFACT() to determine the factorial.
calculateSINE() to calculate sine.
calculateCOSINE() to calculate cosine.
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;
}
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;
}
double calculateTAN(double m)
{
return calculateSINE ( m ) / calculateCOSINE ( m );
}
int main()
{
cout<< calculateTAN( 0.0 ) ; cout<<endl;
cout<< calculateTAN( 0.1 ) ; cout<<endl;
cout<< calculateTAN( 0.2 ) ; cout<<endl;
cout<< calculateTAN( 0.3 ) ; cout<<endl;
cout<< calculateTAN( 0.4 ) ; cout<<endl;
cout<< calculateTAN( 0.5 ) ; cout<<endl;
cout<< calculateTAN( 0.6 ) ; cout<<endl;
cout<< calculateTAN( 0.7 ) ; cout<<endl;
cout<< calculateTAN( 0.8 ) ; cout<<endl;
cout<< calculateTAN( 0.9 ) ; cout<<endl;
cout<< calculateTAN( 1.0 ) ; cout<<endl;
_getche();
return 0;
}
Output
0
0.100335
0.20271
0.309335
0.422782
0.546246
0.68393
0.841663
1.02798
1.25617
1.54844
Related articles
0 comments:
Post a Comment