Expansion in Taylor using functions

Asked

Viewed 58 times

1

I wrote a function that calculates the sine of an angle using the Taylor expansion given by the expression sen(x) = x - x 3/3! + x 5/5! - x 7/7! + ... AD INFINITUM. I created a function that calculates the factorial of a number and another function that calculates the sine using this expansion. The calculation must be stopped when the next expansion term is less than 10 -10 (in module) and this is expressed in while. I don’t see any error in the code, but when I run the program the value it returns is incorrect when I compare with the value of the calculator.

PI = 3.14159265358979323846

def fatorial(n):
    '''
    Esta função recebe um número inteiro n e devolve
    o seu fatorial.
    '''
    fatorial = 1
    for i in range(1,n+1,1):
        fatorial = fatorial * i
    return fatorial 
        

def seno(theta):
    '''
    Esta função aproxima o valor da função seno para o ângulo theta
    usando a série de Taylor até que o módulo do próximo termo da
    série calculada seja menor 1e-10.
    Entrada: O ângulo theta que deve ser informado em graus.
    Saída: A aproximação do seno do ângulo theta.
    '''
    n = 1
    k = 3
    eps = 10**-10
    theta = (theta*PI)/180
    x = ((-1)**n)*((theta)**k)
    y = fatorial(k)
    while x/y < -eps or x/y > eps:
        theta = theta + (x/y)
        n = n + 1
        k = k + 2
        x = ((-1)**n) * ((theta)**k)
        y = fatorial(k)
    return theta
  • Your while x/y < -eps or x/y > eps: shouldn’t be while x/y > -eps and x/y < eps:?

No answers

Browser other questions tagged

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