taylor series in python without angle and margin of error

1

Hello! I need to mount a program that calculates sine, cosine, and exponential of taylor series in Python, but the program can not accept an angle value, just the x, the number of terms in the series, and an acceptable error. I tried to do this but it goes wrong because the values do not match what I should give. the statement is this:

Consider the following functions:

  

sin (x) = x / 1! - x3 / 3! + x5 / 5! -. . . + (-1) k. x2k + 1 / (2k + 1)! +. . .

     

cos (x) = 1 - x2 / 2! + x4 / 4! - x6 / 6! +. . . + (-1) k. x2k / (2k)! +. . .

     

ex = 1 + x + x2 / 2! + x3 / 3! + x4 / 4! + ... + xn / n! + ...

There are 2 ways to approach: 1. Calculate the sum of a number of terms. The higher the number of terms the better the approximation. 2. Calculate the sum to find a term whose absolute value is very small, because from a certain term, the following will always be smaller in absolute value.

Your program should calculate the value of the above functions using the two approximation methods for various values of n (number of terms) and eps (very small value). You should also compare the calculated values with the one obtained using the function in the Python math module.

Thank you !! :)

def serial ():

For the sine function

def sen():
    import math

    x = float(input("Entre com o valor de x: "))
    n = int(input("Entre com o valor de n, sendo ele positivo: "))
    eps = float(input("Entre com um valor bem pequeno: "))

    while n <= 0:
        n = int(input("Entre com um valor positivo de n: "))

    while eps <= 0 or eps >1:
        eps = float(input("Entre com um valor bem pequeno: "))

    seno = math.sin(x)
    print("O seno vale", seno)


    #Para calcular o seno com base na soma de termos
    sen_n = 0
    sinal_n = 0
    fat_n = 1

    for k in range (1, n + 1 ,2):
        fat_n = 1

        for i in range (2, k + 1):
            fat_n = fat_n * i

        sen_n += (pow(-1, sinal_n) * pow(x, k)) / fat_n
        sinal_n += 1
        sen1 = sen_n

    print("sen1 vale", sen1)

    print("A diferença pelo método 1 é", abs(sen1 - seno))

    sen()
serie()
    
asked by anonymous 03.06.2017 / 17:45

2 answers

1
  

I would like to understand what k means in the statement, and also about the error (I'm terrible in math). If you have not solved this question yet, I will await your comments here.

That said, you can make the sums of the terms of the Taylor series, like this:

# Primeiro fazemos a conversão para radianos
def para_radianos(x):
    return x/180*math.pi

# aqui nós fazemos a divisão e o fatorial
def taylor(x, i):
return math.pow(x, i)/math.factorial(i)

And now we need to do the sequence of sums and subtractions, for this we create a loop :

# para o cálculo do seno
def calcula_seno(x, termos): # recebe o ângulo e no. de termos
    y = 3
    z = 0
    while True: # só para quando alcançarmos o número de termos
        z = z - taylor(x, y)
        y = y + 2
        z = z + taylor(x, y)
        y = y + 2
        if y >= termos: # alcançamos?
            break
    return x+z

And we call the function this way:

angulo = 32
radianos = para_radianos(angulo)
calcula_seno(radianos, 1)
  

For the calculation of the cosine, the function is not very different from this. Here is a complete example:

import math

# rad
def para_radianos(x):
    return x/180*math.pi

# taylor
def taylor(x, i):
    return math.pow(x, i)/math.factorial(i)

# sen
def calcula_seno(x, termos):
    y = 3
    z = 0
    while True:
        z = z - taylor(x, y)
        y = y + 2
        z = z + taylor(x, y)
        y = y + 2
        if y >= termos:
            break
    return x+z

# cos
def calcula_cosseno(x, termos):
    y = 2
    z = 0
    while True:
        z = z - taylor(x, y)
        y = y + 2
        z = z + taylor(x, y)
        y = y + 2
        if y >= termos:
            break
    return 1+z

# testa seno
print('testa seno:')
seno_math = math.sin(para_radianos(32))
seno_taylor = calcula_seno(para_radianos(32), 5)
print('math.sin: ' + str(seno_math))
print('taylor:   ' + str(seno_taylor))
print('')
# testa cosseno
print('testa cosseno:')
cosseno_math = math.cos(para_radianos(45))
cosseno_taylor = calcula_cosseno(para_radianos(45), 5)
print('math.sin: ' + str(cosseno_math))
print('taylor:   ' + str(cosseno_taylor))
  

Result:   

    
04.06.2017 / 04:30
0

Your duties

sen(x) = x/1! – x3/3! + x5/5! - ... + (-1)k . x2k+1/(2k+1)! + ...
cos(x) = 1 – x2/2! + x4/4! – x6/6! + ... + (-1)k . x2k/(2k)! + ...
ex(x) = 1 + x + x2/2! + x3/3! + x4/4! + ... + xn/n! + ...

can be generalized to

f(y) = ∑ fn(n) / fd(n)! × y^(fe(n)) 

ie (taking the first 5 terms):

import math
def taylor(fn,fd,fe,x,iter=5):
  s=0
  for i in range(0, iter):
    s +=  fn(i)/math.factorial(fd(i))*math.pow(x, fe(i))
  return s

def seno(x):
  return taylor(lambda n: (-1)**n,
                lambda n: 2*n +1,
                lambda n: 2*n +1, x)

def cosseno(x):
  return taylor(lambda n: (-1)**n, 
                lambda n: 2*n,
                lambda n: 2*n, x)

def exp(x): return taylor(lambda n:1, lambda n:n, lambda n:n, x)

To test ...

def rad(x): return x/180*math.pi

r=rad(31)
print(math.sin(r), seno(r), math.sin(r)-seno(r))
## 0.5150380749100542 0.5150380749391384 -2.90842905315003e-11

Join now the various termination or error strategies

    
22.05.2018 / 11:46