1
Hello! I need to assemble a program that calculates sine, cosine and exponential Taylor series in Python, but the program cannot accept an angle value, only x, number of series terms and an acceptable error. I tried to do this but it goes wrong because the values do not match what should give. the statement is this:
Consider the functions below:
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 = 1 + x + x2/2! + X3/3! + X4/4! + ... + Xn/n! + ...
There are two ways to approach: 1. Calculate the sum of a number of terms. The greater the quantity of terms, the better the approximation. 2. Calculate the sum until you find a term whose absolute value is very small, because from a certain term, the following will always be lower 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 to the one obtained using the existing function in the Python Math module.
thank you!! :)
def serie():
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()
Please take a look at my answer, and see what is missing - I would like to understand the question of the margin of error and then edit it if possible.
– Daniel