Reputation: 33
I wanted to find an integral ((sin x)^8, {x,0,2*Pi}) and tried to write a simple program, without any external modules as "math", which calculate Taylor series and summarize it for interval (0,2*Pi) but an errors
Traceback (most recent call last):
File "E:\python\ShAD\sin.py", line 27, in <module>
sum+=(ser(i*2*3.1415926/k))**8
File "E:\python\ShAD\sin.py", line 21, in ser
sin_part+=((-1)**(j-1))*(a**(2j-1))/(fact(2*j-1))
ZeroDivisionError: 0.0 to a negative or complex power
suddenly occurs. And I just don't see where something is divided by zero or have power of complex number, all variables have only real positive value. "k" is a value both for terms of series quantity and interval (0,2*Pi) division.
sum=0
k=20
res=0
def fact(t):
if t==0 or t==1:
res=1
else:
res=1
for l in range(2,t+1):
res=res*l
return res
def ser(a):
sin_part=a
for j in range(2,k):
print fact(2*j-1)
sin_part+=((-1)**(j-1))*(a**(2j-1))/(fact(2*j-1))
print 'yay'
return sin_part
for i in range(0,k-1):
sum+=(ser(i*2*3.1415926/k))**8
print sum
Upvotes: 1
Views: 1240
Reputation: 353059
And I just don't see where something is divided by zero or have power of complex number, all variables have only real positive value.
Not true. On the first iteration of
for i in range(0,k-1):
sum+=(ser(i*2*3.1415926/k))**8
you have i=0
, so the argument to ser
is 0, so a == 0
, and you have (a**(2j-1))
, which takes 0 to a complex power.
Maybe you meant a**(2*j-1)
? Python uses j
for the unit imaginary, and so 2j-1
is a complex number.
Upvotes: 2