Kodeclik Blog
About
Kodeclik is an online coding academy for kids and teens to learn real world programming. Kids are introduced to coding in a fun and exciting way and are challeged to higher levels with engaging, high quality content.
Popular Classes
Scratch Coding
Minecraft Coding
TinkerCAD
Roblox Studio
Python for Kids
Javascript for Kids
Pre-Algebra
Geometry for Kids
Copyright @ Kodeclik 2024. All rights reserved.
Recall that the sine of an angle is defined as the opposite side (opposite to the angle) divided by the hypotenuse in a right-angled triangle. It is well known that the sine of zero is zero and the sine of 90 degrees (which is pi/2 radians) is 1. The Math.sin() is a handy function in Python to compute these trigonometric ratios. Consider the following program:
The output is:
Note that in the second line we are careful to convert degrees to radians (by scaling by pi/180) before passing the result to the sin() function. (In the first line, we know that 0 degrees is the same as 0 radians, so we do not need to be so careful.)
If you did not have access to the sin() function you can approximate the sin function by using a Taylor series expansion (expanded around 0) as shown in the below function:
The above function takes two inputs, viz. Theta the angle for which we wish to compute the sine() and n, the number of terms. Inside the function, we first convert the angle to radians and then use a for loop to add up terms according to the Taylor expansion for sin(theta). Even though the Taylor’s series is infinite, we are approximating it here with a finite number of terms.
Now we can use this function like so:
The output will be:
As you can see we are using five terms in the sine function expansion to approximate the value of sin(). And you can see that the result is not exactly 1.0. (as produced by the Math.sin() function). If we increase the number of terms we will get increasingly more accurate results. Here is some code to explore this:
The output will be:
As you can see, after a few terms we see the series converging to the desired value.
In conclusion, you can use the math.sin() function which is a built-in function in the math module of Python or you can construct your own approximation using Taylor series.
If you liked learning about the math.sin() function, you should checkout our blogposts on the math.cos() and math.tan() functions!
Now that you have mastered the math.sin() function, checkout the math.ceil() and math.floor() functions! Also
learn about the math domain error in Python and how to fix it!
Interested in more things Python? Checkout our post on Python queues. Also see our blogpost on Python's enumerate() capability. Also if you like Python+math content, see our blogpost on Magic Squares. Finally, master the Python print function!
Want to learn Python with us? Sign up for 1:1 or small group classes.
import math
print("Sine of " + str(0) + " degrees is "
+ str(math.sin(0)))
print("Sine of " + str(90) + " degrees is "
+ str(math.sin(90*math.pi/180)))
Sine of 0 degrees is 0.0
Sine of 90 degrees is 1.0
import math
def sine_approx(theta,n):
# first convert theta degrees to radians
t = theta*math.pi/180
sum = 0
# now use a
for i in range(0,n):
sum = sum + math.pow(-1,i)*math.pow(t,2*i+1)/math.factorial(2*i+1)
return(sum)
print("Sine of " + str(0) + " degrees is "
+ str(math.sin(0)))
print("Sine of " + str(90) + " degrees is "
+ str(math.sin(90*math.pi/180)))
print("Sine of " + str(90) + " degrees is "
+ str(sine_approx(90,5)))
Sine of 0 degrees is 0.0
Sine of 90 degrees is 1.0
Sine of 90 degrees is 1.0000035425842861
import math
def sine_approx(theta,n):
# first convert theta degrees to radians
t = theta*math.pi/180
sum = 0
# now use a
for i in range(0,n):
sum = sum + math.pow(-1,i)*math.pow(t,2*i+1)/math.factorial(2*i+1)
return(sum)
print("Taylor series computation of sin(x)")
for k in range(1,20):
print("Sine of " + str(90) + " degrees, with "
+ str(k) + " terms is " + str(sine_approx(90,k)))
Taylor series computation of sin(x)
Sine of 90 degrees, with 1 terms is 1.5707963267948966
Sine of 90 degrees, with 2 terms is 0.9248322292886504
Sine of 90 degrees, with 3 terms is 1.0045248555348174
Sine of 90 degrees, with 4 terms is 0.9998431013994987
Sine of 90 degrees, with 5 terms is 1.0000035425842861
Sine of 90 degrees, with 6 terms is 0.999999943741051
Sine of 90 degrees, with 7 terms is 1.0000000006627803
Sine of 90 degrees, with 8 terms is 0.9999999999939768
Sine of 90 degrees, with 9 terms is 1.0000000000000437
Sine of 90 degrees, with 10 terms is 1.0
Sine of 90 degrees, with 11 terms is 1.0000000000000002
Sine of 90 degrees, with 12 terms is 1.0000000000000002
Sine of 90 degrees, with 13 terms is 1.0000000000000002
Sine of 90 degrees, with 14 terms is 1.0000000000000002
Sine of 90 degrees, with 15 terms is 1.0000000000000002
Sine of 90 degrees, with 16 terms is 1.0000000000000002
Sine of 90 degrees, with 17 terms is 1.0000000000000002
Sine of 90 degrees, with 18 terms is 1.0000000000000002
Sine of 90 degrees, with 19 terms is 1.0000000000000002