I'm very new to Python, and am trying to approximate the sine function using this series.
My code looks like this:
import math
def sin(x,n):
sine = 0
for i in range(n):
sign = (-1)**i
sine = sine + ((x**(2.0*i-1))/factorial(2**i-1))*sign
return sine
This does not return the answer that I was hoping for, but I am very confused and can't find my mistake... or maybe I'm just going about this the wrong way entirely (as I said, I'm very new to python and to programming in general).
It seems similar to the program that I had to write a while ago to approximate pi given this series:
def piApprox(n):
pi = 0
for i in range(n):
sign = (-1)**i
pi = pi + 1.0/(2*i+1)*sign
return 4*pi
I don't know if that is useful in any way, but it's what I was trying to use that to figure out my sine method. Any help fixing this or pointing me in the right direction would be greatly appreciated!