1.11M Members

Here's the formula to compute sinx and cosx in C++.

here is the code I wrote at first to calculate it:
http://codepad.org/cbdGB8wL and it gives me good results.

The problem is my lecturer would prefer me to use the formula as attached , but I get stucked, how can I code the formula above?
here's the code that I think should work but it don't:

Please note that you can only enter (0 to 2pi radian) since this is just part of the program and I'm testing on it.

ie, input 1.570796326794897 which is 90 degree should produce 1 but the modified code there gives me wrong values...

Can someone guides me to fix the problem here, I keep thinking and the logic should work there, I couldn't figure out why...? Thanks for your help, I appreciate that!

Now onto the formula. It looks like you just plugged in the formula given without understanding it. What your teacher is trying to get you to do is approximate sin/cos using a Maclaurin series. If you do not know what that means then I don't blame you. It often is not taught until 1st year university (thats when I was taught it). However I taught myself them a little earlier. If you understand basic calculus (taking derivatives) then you will find that the wikipedia page for Taylor Series (https://en.wikipedia.org/wiki/Taylor_series) is remarkably complete!

So, what formula are you trying to get. The formula given is in a minimal form, but it is the same as the Maclaurin series (http://blogs.ubc.ca/infiniteseriesmodule/units/unit-3-power-series/taylor-series/maclaurin-expansion-of-sinx/) You just need to make some modifications to get there. To do this just notice a few things:

1) The sign of the Maclaurin approximation always switches between + and -. This is accomplished by multiplying by -term because multiplying by negatives switches signs.
2) The bottoms of the fraction are i! This is accomplished in a very clever way by using the previous two i's and multiplying them. If you expand term_i-2 you would see that you end up with the factorial of i on the bottom, even though you only multiply 3 things, because the previous i's are all stored in term_i-2.
3) The terms are the terms of the Maclaurin series, you need to add them all together to get your answer. So just adding an extra 'x' won't cut it.

As such I think if you have the full equation you should be able to get it working. Here is what the true equation is:

sin(x)=sum of all term_i
where term_i=-(term_i-1)*((x*x)/(i*(i-1))) and i is only odd (or only even for cos(x)).

As such the error in your code is where you set sine=x+term. You don't want to add term to x. You want to add term to sine and keep adding as you go. You might think that this will make sine really big, but remember that half of the terms are negative! I guess what I am trying to say is that sine=x+term should really be sine=sine+term or sine+=term and that sine=0; should be replaced with sine=x or sine=term.

Hi Labdabeta, thanks for the links and guides! I fixed the code, Great thanks!

You
This article has been dead for over six months: Start a new discussion instead
Post: