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()