Your program should accept two values from the user (the angle x and the value of n) and then should compute and print the value of sin(x).
To make the program, do following tasks.
Write two functions, i.e. function to calculate factorial and function to calculate power having following prototypes.
double Factorial (int n); //Factorial function prototype
double Power(double x, int y); //Power function prototype
Use these functions in your main function to compute the series.
Till now, I've written the following program but I am not able to get the right answer.
using namespace std;
double fact (int f); //declaration of factorial function
double power(double x, int y); //declaration of power function
int x=0; //value of x in the series
float sum_pos = 0;
cout << "Enter the value of x: " << endl;
cin >> x;
1) you have "x" declared as int instead of double in main()
and the "p" variable declared as int instead of double in power()
2) You should use "double" instead of "float"
3) fyi : "x" is in radians, not degrees
4) There is no need to go all the way to "i=1000" ..., write a small program
that just prints out fact(i), you will see a problem.
5) The equation that you are using is best suited for x values near zero.
You can use trig identities to make the value of "x" smaller before
calculating the sin (example : sin(x + 2*pi) = sin(x)... So there is no need
to use any value greater than 2*pi. And actually, that range can be reduced
significantly using other trig identities).
Last edited by Philip Nicoletti; November 8th, 2012 at 07:06 AM.
Also note that the above formula calculates sin(x) where x is in radians, not in degrees.
if you are trying to manually check if a certain value matches what you get on a calculator, then you need to set your calculator to radians.
a 30° angle would be Pi/6 radians so your function should be returning sin( pi/6 ) or sin( 0.5235987755983 ) as being (more or less) equal to 0.5
But as philip posted above, your real problem is in trying to make your program "too good". You're causing a problem by making the loop go to 1000.
What type of course is this for ? If a numerical analysis course, the
error for truncated a Taylo series should be given in the text book.
At any rate ... you ahve an alternating series that is monotomically
decreasing, the absolute value of the error when stopping the series
at a certain term is less than or equal to the first dropped term.
In pratical terms, choose a tolerance (say 10e-6), and stop iterating
when the term that you calculate is less than or equal to the tolerance.
Computers deal with numerical types that have operational constraints.
A double for instance has an upper bound of the largest number it can contain. This upper bound has a define DBL_MAX which is 1.797E+308. If you are making calculations and any (intermediate) value you calculate exceeds the operational constraints you get overflow (or underflow, or one of several other undesired effects). Once this happens, all bets are off concerning the end result, you could get results that make absolutely no sense at all.
Dealing with floating point numbers correctly posses additional problems. Floating point values are NOT accurate values, they are approximations. Each value has a slight error to it. Depending on how you deal with the numbers you can continuously accumulate more error into your intermediate results (or worse, scale/multiply it). This too can throw your results off whack.
For taylor series specifically there are ways to calculate how much iterations you need to achieve the desired precision. And when dealing with floating point, there are ways to reduce error accumulation. And there are ways to figure out if floating point error would exceed the desired precision meaning calculation isn't possible with the current algorithm or the used floating point type.
I doubt all the above is relevant for you, if this is a simple course, then see if you have an iteration value in the text book. If that is not there, you will need to to find an iteration in such a way that none of the intermediates overflow. In short, find an i where power(i) and fact(i) never exceed 1.797E+308 (to avoid precision loss, you probably do NOT want to not the highest possible i that meets this requirement, but want an i that's a bit lower)