How to implement the sine function using Taylor series in Python?

Asked

Viewed 71 times

0

I’m having a little trouble with my code. I need to create a code that calculates the sine of an angle using the Taylor series, in the range of [-pi, +pi]. Thus, the sine function repeats. That is, the sine value of the angle of pi/6 is equal to the angle value of 5pi/6. So if you pass an angle outside the [-pi,pi] range, the program must find the equivalent of it range. The angle passed as a parameter to the function must be in radians. With the input, the angle in radians (x) and the increment (n).

inserir a descrição da imagem aqui

My code is like this,

import math
 
def seno(x, grau):
    
    assert type(grau) == int, "Seno: O grau do polinômio não é um inteiro"
    assert grau >= 0, "Seno: O grau do polinômio não é positivo"
    assert (grau % 2) == 1, "Seno: O grau do polinômio não é impar"
    
    
    if (-math.pi >= x >= math.pi):    
        senx = 0        
        for n in range (0, grau, 1):               
            senx = senx + (-1)**n*((x**(2*n+1))/(math.factorial(2*n+1)))
                
    else:    
        senx = 0        
        y = (math.pi + x) % (2*math.pi)     
        z = y - math.pi    

        for n in range (0, grau, 1):    
            senx = senx + (-1)**n*((z**(2*n+1))/(math.factorial(2*n+1)))    
    
    return (senx)

I was able to find the calculation that "converts" angles larger than the [-pi,pi] interval into the range. But my problem is, for angles that are already in range I don’t need to do that calculation. So I’m trying to create a condition, using the if for angles (x) in the range.

  • 2

    What would be "implement the angle"? Do you need to ensure that the past angle is in the [-π, π] range? Do you need to convert the angle passed to the range [-π, π]? Should the angle passed as parameter for the function be in degrees or radians? You can provide an example function call that doesn’t perform as you expected?

  • Thus, the sine function is repeated. That is, the sine value of the angle of pi/6 is equal to the angle value of 5pi/6. So if you pass an angle outside the [-pi,pi] range, the program must find the equivalent of it within the range. The angle passed as a parameter to the function shall be in radians. But if you help me figure out why the code isn’t working, I’m already very grateful.

  • 1

    @Guilhermemello, I don’t know if you know but the statement assert is only checked in mode debugging. Do not use assert to send error messages, it is only a test tool.

  • @Augustovasques was asked in the activity to use.

1 answer

0

Taylor series for sine function is valid for every value of x, so there is no need to worry if the value is not in the range [-π, π]. Let’s use your example of π/6 and 5π/6 compared to the definition of the sine function by the library math:

import math

def seno(x, n):
    senx = 0        
    for i in range(0, n):               
        senx += (-1)**i*((x**(2*i+1))/(math.factorial(2*i+1)))
    return senx

print(seno(math.pi/6, 21))    # Utilizando sua função:   0.49999999999999994
print(math.sin(math.pi/6))    # Utilizando a biblioteca: 0.49999999999999994

print(seno(5*math.pi/6, 21))  # Utilizando sua função:   0.4999999999999997
print(math.sin(5*math.pi/6))  # Utilizando a biblioteca: 0.49999999999999994

Browser other questions tagged

You are not signed in. Login or sign up in order to post.