0
I’m having a little trouble with my code. I need to create a code that calculates the sine of an angle using the Taylor series, in the range of [-pi, +pi]. Thus, the sine function repeats. That is, the sine value of the angle of pi/6 is equal to the angle value of 5pi/6. So if you pass an angle outside the [-pi,pi] range, the program must find the equivalent of it range. The angle passed as a parameter to the function must be in radians. With the input, the angle in radians (x) and the increment (n).
My code is like this,
import math
def seno(x, grau):
assert type(grau) == int, "Seno: O grau do polinômio não é um inteiro"
assert grau >= 0, "Seno: O grau do polinômio não é positivo"
assert (grau % 2) == 1, "Seno: O grau do polinômio não é impar"
if (-math.pi >= x >= math.pi):
senx = 0
for n in range (0, grau, 1):
senx = senx + (-1)**n*((x**(2*n+1))/(math.factorial(2*n+1)))
else:
senx = 0
y = (math.pi + x) % (2*math.pi)
z = y - math.pi
for n in range (0, grau, 1):
senx = senx + (-1)**n*((z**(2*n+1))/(math.factorial(2*n+1)))
return (senx)
I was able to find the calculation that "converts" angles larger than the [-pi,pi] interval into the range. But my problem is, for angles that are already in range I don’t need to do that calculation. So I’m trying to create a condition, using the if for angles (x) in the range.
What would be "implement the angle"? Do you need to ensure that the past angle is in the [-π, π] range? Do you need to convert the angle passed to the range [-π, π]? Should the angle passed as parameter for the function be in degrees or radians? You can provide an example function call that doesn’t perform as you expected?
– enzo
Thus, the sine function is repeated. That is, the sine value of the angle of pi/6 is equal to the angle value of 5pi/6. So if you pass an angle outside the [-pi,pi] range, the program must find the equivalent of it within the range. The angle passed as a parameter to the function shall be in radians. But if you help me figure out why the code isn’t working, I’m already very grateful.
– Guilherme Mello
@Guilhermemello, I don’t know if you know but the statement
assert
is only checked in mode debugging. Do not useassert
to send error messages, it is only a test tool.– Augusto Vasques
@Augustovasques was asked in the activity to use.
– Guilherme Mello