Alright so I want to write a program that will compute trig functions (sin, cos, tan, arcsin, etc.). Basically I'm ignoring the included math library in python and hard coding it myself. My issue is that I'm fairly new to programming and not sure how to go about coding a series (Taylor series). I've already managed to figure out the factorials but I'm just lost on the series. Any help would be greatly appreciated :D

thanks a lot.

To approximate for instance the sine of x (x in radians) you can use the Taylor series expansion:
x - x**3/3! + x**5/5! - x**7/7! + ...

However, to get even a remotely accurate number the cutoff for the convergence is large. Your factorials will be huge very quickly too and make floating point numbers impossible.

There are other series expansions available that converge much quicker.

To approximate for instance the sine of x (x in radians) you can use the Taylor series expansion:
x - x**3/3! + x**5/5! - x**7/7! + ...

However, to get even a remotely accurate number the cutoff for the convergence is large. Your factorials will be huge very quickly too and make floating point numbers impossible.

There are other series expansions available that converge much quicker.

I ended up figuring it out by breaking down sin into a function of series rather than using Taylor expansions. It was much simpler. This was my code for sin:

def sine(x):
    sum = 0.0
    n = 0.0
    term = 1.0
    while (term > .0000000001):
        #loops until the iterations grow so large that 'term' becomes negligibly small
        term = ((x ** (2 * n + 1.0))) / (factorial(2 * n + 1.0))
        if n % 2 == 0:
            sum += term
        else:
            sum -= term
        n += 1
    return sum

I also had made a function to calculate factorials, fyi.

I ended up figuring it out by breaking down sin into a function of series rather than using Taylor expansions. It was much simpler. This was my code for sin:

def sine(x):
    sum = 0.0
    n = 0.0
    term = 1.0
    while (term > .0000000001):
        #loops until the iterations grow so large that 'term' becomes negligibly small
        term = ((x ** (2 * n + 1.0))) / (factorial(2 * n + 1.0))
        if n % 2 == 0:
            sum += term
        else:
            sum -= term
        n += 1
    return sum

I also had made a function to calculate factorials, fyi.

You can speed this up:

def sine(x):
    term = float(x)
    result = term # avoid 'sum' which is the name of a builtin function
    u = - term * term # this is minus (x squared)
    n = 0 # use an integer for integer data
    while abs(term) > 1.0e-10:
        n += 2
        term *= u / (n * (n+1))
        result += term
    return result

The remaining question is to give an estimate for the error on the result. With this series, I think I can prove that with an n having the same order of magnitude as x (say n > 2|x|) for example, then the error on the result is smaller than 2 |term| for example. This is bad when x is large, but you can speed up things because sine is 2 pi -periodic. So a good idea would be to replace x by y = x - 2 * k * pi where k is chosen so that y is between -pi and +pi. You could do this when you enter the function. This gives a series with a very fast convergence.

Good show! So the question is, can you use your program to compute all the trig functions you wanted (sin, cos, tan, arcsin, etc.).

I've already submitted my assignment and my professor's standards aren't as high as far as efficiency goes, theory matters more to him. But I do see what you did there, thanks for the help everyone! :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.