- Code: Select all
`def h(x):`

x = float(x)

y =[1]

d = 2.0

j = 1.0

k = 1.0

r = 0

c = 0

while c <= 600:

k = ((-1.0)*(x**(2)))/d

j = j*k

y.append(j)

d = (d + 1.0)*(d + 2.0)

c = c + 1

for t in y:

r = r + t

print r

print y

3 posts
• Page **1** of **1**

I'm trying to write a code for cosines in Taylor Series, but I'm having some trobous, can you help me with that ?

- Code: Select all
`def h(x):`

x = float(x)

y =[1]

d = 2.0

j = 1.0

k = 1.0

r = 0

c = 0

while c <= 600:

k = ((-1.0)*(x**(2)))/d

j = j*k

y.append(j)

d = (d + 1.0)*(d + 2.0)

c = c + 1

for t in y:

r = r + t

print r

print y

- GabrielSilva
**Posts:**8**Joined:**Mon May 20, 2013 12:20 am

Could you tell us what problems you are having?

If it is generating an exception, please include the entire error message. If the answer is not what you expect, let us know what you expected and what answer you received.

casevh

If it is generating an exception, please include the entire error message. If the answer is not what you expect, let us know what you expected and what answer you received.

casevh

- casevh
**Posts:**94**Joined:**Sat Feb 09, 2013 7:35 am

Hi,

I tried your code. You did a good job with the suggestions I made to your previous post. You are very close.

The results you are getting are just a little too small. Your are printing all the terms in the series which is a great idea for debugging. The terms in the series are decreasing too quickly. That means that k is getting too small. If you print the value for d, the values are: 2, 12, 182, 33672, 1133904602, .... The values for d should be: 2 (2*1), 12 (4*3), 30 (6*5), 56 (8*7), ....

The value for d should be calculated from the number of loops you've performed in your while loop. You count the number of loops with c. Try printing the values for for c and d and see if you can figure out how to calculate d from c.

Please let me know if you get it working.

casevh

I tried your code. You did a good job with the suggestions I made to your previous post. You are very close.

The results you are getting are just a little too small. Your are printing all the terms in the series which is a great idea for debugging. The terms in the series are decreasing too quickly. That means that k is getting too small. If you print the value for d, the values are: 2, 12, 182, 33672, 1133904602, .... The values for d should be: 2 (2*1), 12 (4*3), 30 (6*5), 56 (8*7), ....

The value for d should be calculated from the number of loops you've performed in your while loop. You count the number of loops with c. Try printing the values for for c and d and see if you can figure out how to calculate d from c.

Please let me know if you get it working.

casevh

- casevh
**Posts:**94**Joined:**Sat Feb 09, 2013 7:35 am

3 posts
• Page **1** of **1**

Users browsing this forum: Bing [Bot] and 6 guests