Sad Ive been working on writing some math libraries/functions in various languages for practice and Im trying to make one for sin/cos. I cant figure out how these ****ing Taylor series things work. I cant get the bloody things to return anything close to a correct answer.

Ive tried it in Basic, Python and Java and I still get the wrong answer.

am I missing something?
That's because Taylor series are very inaccurate if you're not approximating fairly close to where it is centered.
so how come so many places say they are what you should use for implementing trig functions from scratch?

Im using the one it says to use for sin() and it really doesnt work at all,
Because they're not using Taylor Series.
wtf?....

http://www.efunda.com/math/taylor_series/trig.cfm

http://en.wikipedia.org/wiki/Taylor_series

http://en.wikipedia.org/wiki/Trigonometric_function

any other recommendations for how to do this then?
yup....thats what Im trying to do....
That is a series, not a taylor series. The difference between that and a Taylor Series is that a taylor series needs a ridiculous number of terms to even approach being a useful approximation of sin x, where the series that defines sin x only needs to be taken out to say, 5 terms, to be reasonably accurate for all x.
1: why do all these websites call ^that a Taylor series?
2: why doesn't that equation work if its what im using?



Code:

def factorial(n):
   a = 1;
   x = 1
   while a <= n:
      x = x*a
      a = a + 1
   return x
   
def power(n, e):
   x = 1
   for i in range(0, e):
      x = x*n
   return x
   
def sin(x):
   return (power(-1, 15)/factorial((2*15) + 1))*(power(x, (2*15) + 1))
   
   
print ""
sin(2)

print ""
print sin(.75 * 3.14159265)
You're very confused. Your code is only calculating one term of the series, which is going to be inaccurate no matter which approximation you are using.

EDIT: Oops, I was wrong; it is a Taylor Series. Scratch most of what I said.
okay. so that partially explains my confusion.


Code:
def factorial(n):
   a = 1;
   x = 1
   while a <= n:
      x = x*a
      a = a + 1
   return x
   
def power(n, e):
   x = 1
   for i in range(0, e):
      x = x*n
   return x
   
def sin(x):
   ans = 0
   for n in range(1,20):
      ans = (power(-1, n)/factorial((2*n) + 1))*(power(x, (2*n) + 1))
   return ans
   
print ""
print sin(.75 * 3.14159265)


should that do it?
I can check, just a sec.
elfprince13 wrote:

Code:

print ""


the "" aren't needed. just 'print' works
thats a left over from when I was testing power() and factorial()
You have an unnecessary semicolon. Perhaps that is the reason why your code is not valid.
?

that should work though right?

cause its not working in Basic or Java either.
jpez wrote:
You have an unnecessary semicolon. Perhaps that is the reason why your code is not valid.


umm, no he doesn't

and i just tested that, it totally does not work at all, lol
I didn't think so, and it doesn't have syntax erros. just a logic error somewhere.
You're not adding the terms up. Your code should be:

Code:
def sin(x):
     ans = 0
     for n in range(1,20):
           ans += (power(-1, n)/factorial((2*n) + 1))*(power(x, (2*n) + 1))
     return ans

Code:

def factorial(n):
   a = 1;
   x = 1
   while a <= n:
      x = x*a
      a = a + 1
   return x
   
def power(n, e):
   x = 1
   for i in range(0, e):
      x = x*n
   return x
   
def sin(x):
   ans = 0
   for n in range(1,20):
      ans = ans + (power(-1, n)/factorial((2*n) + 1))*(power(x, (2*n) + 1))
   return ans
   
print ""
print sin(.75 * 3.14159265)


that doesnt seem to work either.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 2
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement