NumPy Trigonometric Functions – np.sin(), np.cos(), np.tan()

NumPy Trigonometric Functions

NumPy supports trigonometric functions like sin, cos, and tan, etc. The NumPy trigonometric functions help to solve mathematical trigonometric calculation in an efficient manner. 

np.sin() Trigonometric Function

The np.sin() NumPy function help to find sine value of the angle in degree and radian.

Syntax: sin(x, /, out=None, *, where=True, casting=’same_kind’, order=’K’, dtype=None, subok=True[, signature, extobj])

Sine value of angle in degrees

import numpy as np # import numpy package
sin_90 = np.sin(90) # sine value of degree 90 in degree
print("Sine value of angle 90 in degree = ",sin_90) # print sine value
Output >>>
Sine value of angle 90 in degree =  0.8939966636005579

Sine value of the angle in radians

To get sine value of the angle in radians, need to multiply angle with np.pi/180.

np.pi = 3.14

sin_90 = np.sin(90 * np.pi/180) # sine value of angle 90 in radinas
print("Sine value of angle 90 in radians = ", sin_90)
Output >>>
Sine value of angle 90 in radians =  1.0

Learn more about python NumPy click here.

np.cos() Trigonometric Function

The np.cos() NumPy function help to find cosine value of the angle in degree and radian.

Syntax: cos(x, /, out=None, *, where=True, casting=’same_kind’, order=’K’, dtype=None, subok=True[, signature, extobj])

Cosine value of angle in degrees

import numpy as np # import numpy package
cos_180 = np.cos(180) # cosine value of degree 180 in degree
print("Cosine value of angle 180 in degree = ", cos_180) # print cosine value
Output >>>
Cosine value of angle 180 in degree =  -0.5984600690578581

Cosine value of the angle in radians

To get cosine value of the angle in radians, need to multiply angle with np.pi/180.

np.pi = 3.14

cos_180 = np.cos(180 * np.pi/180)
print("Cosine value of angle 180 in radians = ", cos_180)
Output >>>
Cosine value of angle 180 in radians =  -1.0

np.tan() Trigonometric Function

The np.tan() NumPy function help to find tangent value of the angle in degree and radian.

Syntax: tan(x, /, out=None, *, where=True, casting=’same_kind’, order=’K’, dtype=None, subok=True[, signature, extobj])

Tangent value of angle in degrees

import numpy as np # import numpy package
tan_60 = np.tan(60) # tangent value of degree 60 in degree
print("Tangent value of angle 60 in degree = ", tan_60) # print tangent value
Output >>>
Tangent value of angle 60 in degree =  0.320040389379563

Cosine value of the angle in radians

To get the tangent value of the angle in radians, need to multiply angle with np.pi/180.

np.pi = 3.14

tan_60 = np.tan(60 * np.pi/180) # tangent value of degree 60 in degree
print("Tangent value of angle 60 in radians = ", tan_60) # print tangent value
Output >>>
Tangent value of angle 60 in radians =  1.7320508075688767

Graphical Representation of Trigonometric sine Function

import numpy as np # import numpy package
import matplotlib.pyplot as plt # import matplotlib.pyplot package
x = np.arange(0, 3 * np.pi, 0.1) # create x array of angels from range 0 to 3*3.14
y = np.sin(x) # create y array of sine values of angles from x array
plt.plot(x, y) # plot grah 
plt.title(" Graphical Representation of sine function")
plt.xlabel("x axis ")
plt.ylabel("y axis ")
plt.show() # show plotted graph

Output >>>

numpy trigonometric functions Graph
Graph of np.sin() function

Graphical Representation of Trigonometric Cosine Function

import numpy as np # import numpy package
import matplotlib.pyplot as plt # import matplotlib.pyplot package
x = np.arange(0, 3 * np.pi, 0.1) # create x array of angels from range 0 to 3*3.14
y = np.cos(x) # create y array of sine values of angles from x array
plt.plot(x, y) # plot grah 
plt.title(" Graphical Representation of cosine function")
plt.xlabel("x axis ")
plt.ylabel("y axis ")
plt.show() # show plotted graph

Output >>>

NumPy trigonometric functions graph
Graph of np.cos() function

Graphical Representation Of Trigonometric Tangent Function

import numpy as np # import numpy package
import matplotlib.pyplot as plt # import matplotlib.pyplot package
x = np.arange(0, 3 * np.pi, 0.1) # create x array of angels from range 0 to 3*3.14
y = np.tan(x) # create y array of sine values of angles from x array
plt.plot(x, y) # plot grah 
plt.title(" Graphical Representation of tangent function")
plt.xlabel("x axis ")
plt.ylabel("y axis ")
plt.show() # show plotted graph

Output >>>

NumPy trigonometric functions graph
Graph of np.tan() function

Note: To get the value of Numpy trigonometric functions, need to multiply angle value with np.pi/180 because all trigonometric functions accept  by default value as degrees.

Conclusions 

In NumPy trigonometric function blog, We learn how to use np.sin(), np.cos() and np.tan() mathematical trigonometric functon. According to your requirement give argument as degrees or radians value to respective function.

To learn more about python NumPy library click on the bellow button.

Leave a Reply

Top