How would Taylor Series work?

232 Views Asked by At

I wish to calculate sine of any given an angle without using the functions that come with programming language and devices. I have written a small code in Python which can be found here. Using the sine, I calculate cosine, tangent and cotangent as well.

The code aside, I used Taylor Series for trigonometric calculations. I don't know LaTex so let me explain what I did:

$$\sin x = \sum_{n = 0}^{\infty} \frac{(-1)^n}{(2n+1)!}x^{2n+1}$$

It's all in the code... But how would this series work, even if the results is an extremely large number. Could you explain this series a little bit, and that how this large number is the sine of a given angle, which is supposed be smaller than 1?

2

There are 2 best solutions below

2
On BEST ANSWER

Your code is buggy. You have:

def sine(d):
    serial = 0
    for i in (0, d):
        serial += ((-1)**i / (factorial((2 * i) + 1))) * d ** ((2*i) + 1)
    return serial 

You can only evaluate partial sums. You need to have a parameter corresponding to how many terms to take. Something like:

def sine(x,k):
    serial = 0
    for i in range(k):
        serial += ((-1)**i / (factorial((2 * i) + 1))) * x ** ((2*i) + 1)
    return serial 

For example:

>>> sine(math.pi/4,10)
0.7071067811865475
>>> 1/math.sqrt(2)
0.7071067811865475

In your original code, you seem to misunderstand how for-loops work. The line

for i in (0,d):

loops through the 2-element tuple consisting of 0 and d. Thus -- your code never did anything other than add two terms, and not terms which were actually correct. You were adding the first term in all cases as well as the term d(which would only make sense when d is an int). Thus, when you evaluated sine(45) you were simply evaluating 2 nonadjacent terms of sin(45 radians), which is why you saw -20481491060.906067. Note that even though the series for sine converges for all x, the farther away from the origin x is the more terms you need. For x = 45 you need a fair number of terms to get good convergence. Just 2 (nonadjacent) terms are not enough.

Finally, the most Pythonic way to evaluate partial sums is to use a comprehension:

def sine(x,k):
    return sum(((-1)**i / (factorial((2 * i) + 1))) * x ** ((2*i) + 1) for i in range(k))

is a 1-line definition which is equivalent to the code I gave above.

0
On

Taylor Series is a representation of a function as an infinite sum over some interval, centered at some point. The interval (Radius of Convergence) for which the representation is valid depends from function to function and is the interval on which the infinite sereis converges.

To see why the inifnite series converges on $\mathbb{R}$, note that although some of the summands might initially be bigger than $1$ we also have some negative terms in the sequence. Eventually as the factorial grows much faster than the exponential function the summands will quickly start approaching to zero. This is just a intuitive way to see why the sequence always converges on $\mathbb{R}$, but it's not very difficult to turn it into a rigorous proof.