Python - Sine cycle in degrees

Asked

Viewed 174 times

-1

Good in order to calculate the sine cycle (sin) in degrees between 0 and 390 with an interval of 10.

print('Ciclo seno em graus entre 0 e 390 com intervalo de 10')
a=0
while True:
    if 0<=a<=390:
        math.sin(math.degrees(a))
        a=a+10
    else:
        break
    print(a)

I’m not getting the right result...

  • 1

    thanks for the comment, however are appearing values above 390 and did not use the sine... I think the most correct result complementing your reply would be: for a in range(0,391,10): print(math.degrees(math.sin(a)))

  • 2

    I’m sorry, my fault: for a in range(0,391,10): print(math.sin(math.degrees(a)))

3 answers

5


For starters, the circular functions - sine, cosine and tangent - are obtained through the trigonometric circle. This, in turn, goes from 0 to 360°. So it makes no sense to table the sines in the interval 0 to 390°, since the values of 360° to 390° are equal to those of the interval of 0 to 30°.

Another thing, the values of the sines included in the interval [1, 179], have signal POSITIVE. The sines corresponding to 0 and 180° have value NULL and the sines included in the interval [181, 359], have signal NEGATIVE.

In addition, sine calculation is always based on radians.

Taking all these observations into consideration, we can implement the following code:

from math import sin, radians

print('Ciclo do seno de "0" à "360°" com intervalo de "10°"')
for i in range(0, 361, 10):
    if 0 <= i < 360:
        print(f'{f"{i:<4} = "}{sin(radians(i)):.4f}')
    else:
        print(f'{f"{i:<4} = 0.0000"}')

Note that the block for traverses the range(0, 361, 10), I mean, go through the 0 to 361° - 1, that is, the interval 0 to 360°, jumping from 10 to 10°.

Note that the temporary variable of the block for, that is, the variable i, corresponds to the value in degrees. And therefore I don’t need to convert tau value again into degrees - applying the function degrees().

Finally we can calculate the sine and display the result with 4 decimal places.

  • But what if he wants to take a full ride and the 30 degrees left?

  • @Luiz Felipe, good evening. A complete tour is 0 à 360. Two complete laps is 0 à 720. Of 0 à 390 would be a complete turn + 30°.

  • 2

    Yes, I know! And that’s what I said in my comment above. The problem is, by limiting it to 360 degrees, you’re "ignoring" the 30 degrees left for the 390s the AP ordered, right? Anyway, I left my +1 because I found your solution very elegant.

  • 2

    @Luiz Felipe, I completely understood his position, but as I explained in the first paragraph of my post there is no reason to RECALCULATE a value that we had calculated previously. Strong arm!

  • Good morning, thank you very much for the comment I understood very well the explanation only left with a question: what is the sep='\t'? is that I’ve tried removing it from the code, but it hasn’t changed anything.

  • To complete your comment since in my problem I asked in degrees you must change this part of the code print(f'{i}° = {math.degrees(sin(radians(i))):.4f}', sep='\t') to convert the radian to degrees.

  • I was just checking the calculations in the calculator and I ended up getting a little confused... For example sen(90) in degrees gives 1 and sen -1(1) is 90. But here in python it seems the function sen is in radians or sen(90) in radians gives 0.894 and sen -1(0.894) gives 1.106. Therefore in the program sen(90) radians is 0.894 and then converting to degrees using math.degrees is the same thing as sin -1(0.894) degrees which is 63.38?

  • 1

    @Diogo, the \t serves only to separate outputs with one tab - three spaces. How outputs were formatted with f-string in fact, it makes no difference. So I’m going to remove it from the code. That was an inattention of mine. Thank you for the remark.

Show 3 more comments

4

I’m not sure what you want with this code, since it doesn’t show the calculated value of the sine. Anyway, just change the print position so that the range of a is shown correctly:

    print('Ciclo seno em graus entre 0 e 390 com intervalo de 10')
    a=0
    while True:    
           if 0<=a<=390:
               print(a)
               print(math.sin(math.degrees(a)))
               a=a+10
           else:
               break

2

I believe that the while and the if should be:

while a <= 390:
    if 0 >= a <= 390:
        math.sin(math.degrees(a))
        a = a + 10
    else:
        break
    print(a)    

Why the while True should be unable to leave the loop.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.