John Richardson February 2016

Python function returning first value twice

I've written this function to calculate sin(x) using Taylor series to any specified degree of accuracy, 'N terms', my problem is the results aren't being returned as expected and I can't figure out why, any help would be appreciated.

What is am expecting is: 1 6.28318530718 2 -35.0585169332 3 46.5467323429 4 -30.1591274102 5 11.8995665347 6 -3.19507604213 7 0.624876542716 8 -0.0932457590621 9 0.0109834031461

What I am getting is: 1 None 2 6.28318530718 3 -35.0585169332 4 46.5467323429 5 -30.1591274102 6 11.8995665347 7 -3.19507604213 8 0.624876542716 9 -0.0932457590621

Thanks in advance.

def factorial(x):
    if x <= 1:
        return 1
    else:
        return x * factorial(x-1)

def sinNterms(x, N):
    x = float(x)

    while N >1:
        result = x
        for i in range(2, N):
            power = ((2 * i)-1)
            sign = 1
            if i % 2 == 0:
                sign = -1
            else:
                sign = 1
            result = result + (((x ** power)*sign) / factorial(power))
        return result

pi = 3.141592653589793
for i in range(1,10):
    print i, sinNterms(2*pi, i)

Answers


sabbahillel February 2016

I see that you are putting the return under the for which will break it out of the while loop. You should explain if this is what you mean to do. However, given the for i in range(1,10): means that you will ignore the first entry and return None when the input argument i is 1. Is this really what you wanted? Also, since you always exit after the calculation, you should not do a while N > 1 but use if N > 1 to avoid infinite recursion.

The reason why your results are off is because you are using range incorrectly. range(2, N) gives you a list of numbers from 2 to N-1. Thus range(2, 2) gives you an empty list.

You should calculate the range(2, N+1)

def sinNterms(x, N):
    x = float(x)

    while N >1:
        result = x
        for i in range(2, N):

Your comment explains that you have the lines of code in the wrong order. You should have

def sinNterms(x, N):
    x = float(x)

    result = x
    # replace the while with an if since you do not need a loop
    # Otherwise you would get an infinite recursion
    if N > 1:
        for i in range(2, N+1):
            power = ((2 * i)-1)
            sign = 1
            if i % 2 == 0:
                sign = -1
            # The else is not needed as this is the default
            # else:
            #     sign = 1
            # use += operator for the calculation
            result += (((x ** power)*sign) / factorial(power))
    # Now return the value with the indentation under the if N > 1
    return result

Note that in order to handle things set factorial to return a float not an int.

An alternative method that saves some calculations is

def sinNterms(x, N):
    x = float(x)
    lim = 1e-12
    result = 0
    sign = 1
    # This range gives the odd numbers, saves calculation.
    for i in range 

Post Status

Asked in February 2016
Viewed 3,893 times
Voted 11
Answered 1 times

Search




Leave an answer