C Programming (Turbo / Visual C++ Compiler) - Using Sine and Cosine Series to find TAN Value
I have given here a C program to find the value of TAN using Sine & cosine Series and without using the library function. This program will print the values got from the series function MyTan and library function tan.
sine series = x - x^3/3! + x^5/5! - x^7 / 7! + x^9 / 9! - .......
cosine series = 1 - x^2/2! + x^4/4! - x^6 / 6! + X^8 / 8! - ....
Tan Series = sine series / cosine series
Source Code
#include <math.h>
#include <stdio.h>
const double PI = 3.14159265;
// tan series = sine series / cosine series
double MyTan(double x)
{
double sineresult = x;
double cosineresult = 1;
double sqx = 0;
double fact = 0;
int index = 0;
int term = 0;
double termfactor = 0;
{
sqx = x * x * x;
fact = 2 * 3;
index = 3;
for(term = 1; term < 20; term++)
{
termfactor = 0;
termfactor = sqx / fact;
if(term % 2)
{
sineresult = sineresult - termfactor;
}
else
{
sineresult = sineresult + termfactor;
}
index++;
fact *= index;
index++;
fact *= index;
sqx *= (x*x);
}
}
{
sqx = x * x;
fact = 2;
index = 2;
for(term = 1; term < 20; term++)
{
termfactor = 0;
termfactor = sqx / fact;
if(term %2)
{
cosineresult = cosineresult - termfactor;
}
else
{
cosineresult = cosineresult + termfactor;
}
index++;
fact *= index;
index++;
fact *= index;
sqx *= (x*x);
}
}
return sineresult / cosineresult;
}
int main()
{
double i = 0;
for(i = 0; i <= 360; i += 4.75)
{
printf("%.4lf = %.4lf\t%.4lf\n", i,
tan(i * PI / 180), MyTan(i * PI / 180));
//printf("%.4lf\n", tan(i * PI / 180) - MyTan(i * PI / 180));
}
return 0;
}
Output
0.0000 = 0.0000 0.0000
4.7500 = 0.0831 0.0831
9.5000 = 0.1673 0.1673
14.2500 = 0.2540 0.2540
19.0000 = 0.3443 0.3443
23.7500 = 0.4400 0.4400
28.5000 = 0.5430 0.5430
33.2500 = 0.6556 0.6556
38.0000 = 0.7813 0.7813
42.7500 = 0.9244 0.9244
47.5000 = 1.0913 1.0913
52.2500 = 1.2915 1.2915
57.0000 = 1.5399 1.5399
61.7500 = 1.8611 1.8611
66.5000 = 2.2998 2.2998
71.2500 = 2.9459 2.9459
76.0000 = 4.0108 4.0108
80.7500 = 6.1402 6.1402
85.5000 = 12.7062 12.7062
90.2500 = -229.1818 -229.1818
95.0000 = -11.4301 -11.4301
99.7500 = -5.8197 -5.8197
104.5000 = -3.8667 -3.8667
109.2500 = -2.8636 -2.8636
114.0000 = -2.2460 -2.2460
118.7500 = -1.8228 -1.8228
123.5000 = -1.5108 -1.5108
128.2500 = -1.2685 -1.2685
133.0000 = -1.0724 -1.0724
137.7500 = -0.9083 -0.9083
142.5000 = -0.7673 -0.7673
147.2500 = -0.6432 -0.6432
152.0000 = -0.5317 -0.5317
156.7500 = -0.4296 -0.4296
161.5000 = -0.3346 -0.3346
166.2500 = -0.2447 -0.2447
171.0000 = -0.1584 -0.1584
175.7500 = -0.0743 -0.0743
180.5000 = 0.0087 0.0087
185.2500 = 0.0919 0.0919
190.0000 = 0.1763 0.1763
194.7500 = 0.2633 0.2633
199.5000 = 0.3541 0.3541
204.2500 = 0.4505 0.4505
209.0000 = 0.5543 0.5543
213.7500 = 0.6682 0.6682
218.5000 = 0.7954 0.7954
223.2500 = 0.9407 0.9407
228.0000 = 1.1106 1.1106
232.7500 = 1.3151 1.3151
237.5000 = 1.5697 1.5697
242.2500 = 1.9007 1.9007
247.0000 = 2.3559 2.3559
251.7500 = 3.0326 3.0326
256.5000 = 4.1653 4.1653
261.2500 = 6.4971 6.4971
266.0000 = 14.3007 14.3007
270.7500 = -76.3900 -76.3900
275.5000 = -10.3854 -10.3854
280.2500 = -5.5301 -5.5301
285.0000 = -3.7321 -3.7321
289.7500 = -2.7852 -2.7852
294.5000 = -2.1943 -2.1943
299.2500 = -1.7856 -1.7856
304.0000 = -1.4826 -1.4826
308.7500 = -1.2460 -1.2460
313.5000 = -1.0538 -1.0538
318.2500 = -0.8925 -0.8925
323.0000 = -0.7536 -0.7536
327.7500 = -0.6310 -0.6310
332.5000 = -0.5206 -0.5206
337.2500 = -0.4193 -0.4193
342.0000 = -0.3249 -0.3249
346.7500 = -0.2355 -0.2355
351.5000 = -0.1495 -0.1495
356.2500 = -0.0655 -0.0655
Press any key to continue . . .
|