1
Good afternoon
Can someone explain to me why my code does not give the requested result and what I can do to obtain the requested solution?
Error appears as follows: total = round(sum,Ndec[k])Typeerror: 'list' Object cannot be Interpreted as an integer
Implement a function with three parameters, which, for a given angle in degrees (first parameter), a vector N
(second parameter), with increasing values of the number of terms of the series to be used, computes and returns a vector with the value of the approximation of the cosine function obtained with each of the Ni
Taylor series terms, for given angle.
The number of decimal places to use in the result for each number of terms is given by the vector NDec
(third parameter) with the index i
of the vector indicating the number of decimal places to be displayed for the number of terms defined in the index i
vector N
. The vector N
should not be modified by the function.
import math
def taylor(ang,N,NDec):
ang_rad = math.radians(ang)
res = []
soma = 0
for i in range(len(N)):
for k in range(len(NDec)):
for j in range(N[i]):
aprox_cos = (-1)**j/(math.factorial(2*j)) * ang_rad**(2*j)
soma = soma + aprox_cos
total = round(soma,NDec[k])
res.append(total)
return res
I used it on the console
ang = 180
N=[3, 4, 6, 10, 20]
NDec=[4, 6, 8, 10, 10]
The solution is:
[0.1239, -1.211353, -1.0018291, -1.0000000035, -1.0]
Thanks to those who can help!
Thank you for the reply, I have corrected that and edited the question, but it still does not give me the requested result
– user99237