Taylor Series

This is the place for queries that don't fit in any of the other categories.

Taylor Series

Postby GabrielSilva » Mon Jun 10, 2013 10:05 pm

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
              d = (d + 1.0)*(d + 2.0)
              c = c + 1

    for t in y:
        r = r + t
    print r
    print y
Posts: 8
Joined: Mon May 20, 2013 12:20 am

Re: Taylor Series

Postby casevh » Mon Jun 10, 2013 10:56 pm

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.

Posts: 103
Joined: Sat Feb 09, 2013 7:35 am

Re: Taylor Series

Postby casevh » Wed Jun 12, 2013 6:30 am


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.

Posts: 103
Joined: Sat Feb 09, 2013 7:35 am

Return to General Coding Help

Who is online

Users browsing this forum: Yahoo [Bot] and 9 guests