Nathan Neven
Nathan Neven

Reputation: 37

Calculating Pi Using Taylor Series

I am trying to calculate the value for Pi using Taylor Series. Below is the code that I have, but when I run the program I get a list of 1's and 0's.

Here is my code:

from math import *
import numpy as np

n= 100
x= 1
series = []

for i in range(0,n):
    value = (x**(2*i+1)*(-1)**i)/(2*i+1)
    series.append(value)

print series    

Upvotes: 0

Views: 2528

Answers (2)

trmiller
trmiller

Reputation: 183

I believe you're running into the same issue as this one, which is trying to divide one list by another list. Take a look at the suggestions there and I think you'll find your answer.

Upvotes: 0

michaelpri
michaelpri

Reputation: 3651

The error message is telling you all you need to know. You are trying to divide two lists even though you might not think it looks like it. [] in Python indicate a list, even though they can be used as brackets in real math. All you have to do is change

value = [x**(2*i+1)*(-1)**i]/[2*i+1]

to

value = (x**(2*i+1)*(-1)**i)/(2*i+1)

Upvotes: 1

Related Questions