C Programming (Turbo / Visual C++ Compiler) - Using Sine Series to find Sin Value
I have given here a C program to find the value of sin using Sine Series and without using the library function. This program will print the values got from the series function MySin and library function sin.
sin series = x - x^3/3! + x^5/5! - x^7 / 7! + x^9 / 9! - .......
Source Code
#include <math.h>
#include <stdio.h>
const double PI = 3.14159265;
// sin series = x - x^3/3! + x^5/5! - x^7 / 7!
double MySin(double x)
{
double sqx = x * x * x;
double sineresult = x;
double fact = 2 * 3;
int index = 3;
int term = 0;
double termfactor = 0;
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);
}
return sineresult;
}
int main()
{
double i = 0;
for(i = 0; i <= 360; i += 4.75)
{
printf("%.4lf = %.4lf\t%.4lf\n", i,
sin(i * PI / 180), MySin(i * PI / 180));
//printf("%.4lf\n", sin(i * PI / 180) - MySin(i * PI / 180));
}
return 0;
}
Output
0.0000 = 0.0000 0.0000
4.7500 = 0.0828 0.0828
9.5000 = 0.1650 0.1650
14.2500 = 0.2462 0.2462
19.0000 = 0.3256 0.3256
23.7500 = 0.4027 0.4027
28.5000 = 0.4772 0.4772
33.2500 = 0.5483 0.5483
38.0000 = 0.6157 0.6157
42.7500 = 0.6788 0.6788
47.5000 = 0.7373 0.7373
52.2500 = 0.7907 0.7907
57.0000 = 0.8387 0.8387
61.7500 = 0.8809 0.8809
66.5000 = 0.9171 0.9171
71.2500 = 0.9469 0.9469
76.0000 = 0.9703 0.9703
80.7500 = 0.9870 0.9870
85.5000 = 0.9969 0.9969
90.2500 = 1.0000 1.0000
95.0000 = 0.9962 0.9962
99.7500 = 0.9856 0.9856
104.5000 = 0.9681 0.9681
109.2500 = 0.9441 0.9441
114.0000 = 0.9135 0.9135
118.7500 = 0.8767 0.8767
123.5000 = 0.8339 0.8339
128.2500 = 0.7853 0.7853
133.0000 = 0.7314 0.7314
137.7500 = 0.6724 0.6724
142.5000 = 0.6088 0.6088
147.2500 = 0.5410 0.5410
152.0000 = 0.4695 0.4695
156.7500 = 0.3947 0.3947
161.5000 = 0.3173 0.3173
166.2500 = 0.2377 0.2377
171.0000 = 0.1564 0.1564
175.7500 = 0.0741 0.0741
180.5000 = -0.0087 -0.0087
185.2500 = -0.0915 -0.0915
190.0000 = -0.1736 -0.1736
194.7500 = -0.2546 -0.2546
199.5000 = -0.3338 -0.3338
204.2500 = -0.4107 -0.4107
209.0000 = -0.4848 -0.4848
213.7500 = -0.5556 -0.5556
218.5000 = -0.6225 -0.6225
223.2500 = -0.6852 -0.6852
228.0000 = -0.7431 -0.7431
232.7500 = -0.7960 -0.7960
237.5000 = -0.8434 -0.8434
242.2500 = -0.8850 -0.8850
247.0000 = -0.9205 -0.9205
251.7500 = -0.9497 -0.9497
256.5000 = -0.9724 -0.9724
261.2500 = -0.9884 -0.9884
266.0000 = -0.9976 -0.9976
270.7500 = -0.9999 -0.9999
275.5000 = -0.9954 -0.9954
280.2500 = -0.9840 -0.9840
285.0000 = -0.9659 -0.9659
289.7500 = -0.9412 -0.9412
294.5000 = -0.9100 -0.9100
299.2500 = -0.8725 -0.8725
304.0000 = -0.8290 -0.8290
308.7500 = -0.7799 -0.7799
313.5000 = -0.7254 -0.7254
318.2500 = -0.6659 -0.6659
323.0000 = -0.6018 -0.6018
327.7500 = -0.5336 -0.5336
332.5000 = -0.4617 -0.4617
337.2500 = -0.3867 -0.3867
342.0000 = -0.3090 -0.3090
346.7500 = -0.2292 -0.2292
351.5000 = -0.1478 -0.1478
356.2500 = -0.0654 -0.0654
Press any key to continue . . .
|