-3

This is what I have to program:

sen(x) = (x/1!) - (x^3/3!) + (x^5/5!) - (x^7/7!) + ...

So far I have this:

def seno(x, n):
    for i in range(1, n+1, 2):
        result = (x**i/math.factorial(i))
        result1 = (x**i/math.factorial(i))
        result2 = (x**i/math.factorial(i))
        result3 = (x**i/math.factorial(i))
    return math.sin(result-result1 + result2 - result3)

The thing I can’t understand is how to actually change the i value for each result.

Another thing is I can’t use any non-built-in function. So no imports except for the math.

EDIT: Thank you for the quick reply.

Daniel Monteiro
  • 67
  • 1
  • 1
  • 8
  • 1
    You seem to be trying to compute the sine using the taylor expansion manually. Why are you using `math.sin()` on the result then? Your assignments to the variables `result*` all have the same right-hand side, so they will all end up with the same value. – Sven Marnach Nov 14 '15 at 15:40
  • Thought `math.sin()` would be required. – Daniel Monteiro Nov 14 '15 at 15:40
  • Did you actually try running it? From looking at the code, I'd expect the function to always return 0. – Sven Marnach Nov 14 '15 at 15:41
  • Possible duplicate of [How does Python for loop work?](http://stackoverflow.com/questions/1292189/how-does-python-for-loop-work) – GingerPlusPlus Nov 14 '15 at 15:42
  • Yes i know that it will return 0. – Daniel Monteiro Nov 14 '15 at 15:43
  • 1
    I think you first need to get a clear understanding of what you are trying to achieve. From the requirement that you can only import functions from `math`, I infer that this is homework for some course you are taking (since this requirement wouldn't make sense in any other context). The aim _seems_ to be to approximate the sine with a Taylor series. The function `math.sin()` already does this for you (or actually asks your computer's hardware to do it). If you want to write a software implementation of the Taylor approximation, you shouldn't call `math.sin()`. – Sven Marnach Nov 14 '15 at 15:50

2 Answers2

1

You are using the for loop incorrectly. Each iteration will compute one term of the series; you need to accumulate those values rather than trying to set 4 results at a time.

def seno(x, n):
    sign = 1
    result = 0
    for i in range(1, n+1, 2):
        term = x**i/math.factorial(i)
        result += sign * term
        sign *= -1  # Alternate the sign of the term
    return result
chepner
  • 497,756
  • 71
  • 530
  • 681
  • @SvenMarnach: `i` is always odd in this loop, so I don't think your suggestion would work. – tom10 Nov 14 '15 at 15:53
1

It looks like you're doing a Taylor Series approximation of Sine.

You probably shouldn't declare separate result1, result2, etc. Instead, you compute each value in a loop, and accumulate it in a single result variable.

def seno(x,n):
    result = 0
    sign = 1 # Sign starts out positive
    for i in range(1, n+1, 2):
        result += x**i/math.factorial(i)
        sign *= -1 # use negative sign on odd terms
    return result

Note that you don't actually call math.sin on the result. The whole point of using a Taylor Series approximation is to estimate the value of math.sin(x) without actually calling that function.

You can optimize this loop a bit more. You can do a strength reduction on math.factorial by accumulating the answer rather than recomputing the whole factorial value on each iteration. You can also do a similar strength reduction on the x**i term, and roll the sign-switching logic into the update logic for exp as well.

def seno(x,n):
    result = 0.0
    fact = 1.0   # start with '1!'
    exp = x      # start with 'x¹'
    xx = x*x     # xx = x²
    for i in range(1, n+1, 2):
        result += exp / fact
        exp *= -xx  # update exponential term to 'xⁱ', and swap sign
        fact *= (i+1) * (i+2) # update factorial term to '(i+2)!'
    return result
DaoWen
  • 32,589
  • 6
  • 74
  • 101