Welcome to the third tutorial of the series NumPy Trigonometric Functions. In this tutorial, we will understand the NumPy Tan function. Tan is the short name for the Tangent.
- Tan is not defined when the input given is an odd multiple of pi/2 i.e. pi/2, -pi/2, 3pi/2, -3pi/2, etc.
- There is an interesting relationship which is: tan(x) = sin(x)/cos(x).
We will practice different types of examples as well as plot the graph of NumPy Tan using Matplotlib Library for Python.
What is NumPy Tan?
- NumPy Tan is also one of the trigonometric functions provided by the NumPy Library which computes the trigonometric tangent of a single number and a NumPy Array of angles.
- NumPy Tan is equivalent to
- NumPy Tan function can be accessed as
Syntax Of NumPy Tan
numpy.tan(input) where input can be a single angle and a NumPy Array of Angles.
Working with Numpy Tan on Different Types of Values
Let’s try some examples of the NumPy Tan function to help us understand it better.
NumPy Tan on Pi Values
import numpy as np print("Tan of 0 is :",np.tan(0)) print("Tan of pi/6 is :",np.tan(np.pi/6)) print("Tan of pi/4 is :",np.tan(np.pi/4)) print("Tan of pi/3 is :",np.tan(np.pi/3)) print("Tan of pi is :",np.tan(np.pi))
Tan of 0 is : 0.0 Tan of pi/6 is : 0.5773502691896257 Tan of pi/4 is : 0.9999999999999999 Tan of pi/3 is : 1.7320508075688767 Tan of pi is : -1.2246467991473532e-16
- NumPy Tangent of pi provides a different output- the output is in scientific notation and it is equal to 0.
np.tan(3*np.pi/2) and observe the outputs.
Now, let’s see how we can pass angles in degrees as an argument to the numpy.tan function.
NumPy Tan with Deg2Rad Function
To compute the tangent of angles in which the argument for the tan function is in degrees function
deg2rad is used.
import numpy as np print("Tangent of 30 degrees is :",np.sin(np.deg2rad(30))) print("Tangent of 45 degrees is :",np.sin(np.deg2rad(45))) print("Tangent of 60 degrees is :",np.sin(np.deg2rad(60))) print("Tangent of 180 degrees is :",np.sin(np.deg2rad(180)))
Tangent of 30 degrees is : 0.49999999999999994 Tangent of 45 degrees is : 0.7071067811865476 Tangent of 60 degrees is : 0.8660254037844386 Tangent of 180 degrees is : 1.2246467991473532e-16
Note: One similar function is
rad2deg which takes an angle in radians and converts it to degrees. This function can be used with the Trigonometric Functions of the NumPy Library. Try to use this function with different input values and observe the outputs 🙂
Now, let’s see how can we compute the tangent of an array of angles.
NumPy Tan on Array of Angles
The tan function also takes a NumPy Array as an argument but we must make sure that the angles are converted to radians.
import numpy as np # A NumPy array with all the angles in degrees a = np.array((0 , 30 , 45 , 60 , 180)) print("Tangent Values :\n",np.tan(a*np.pi/180)) # A NumPy array with all the angles is radians b = np.array((0 , np.pi/2 , np.pi/3 , np.pi)) print("Tangent Values :\n",np.tan(b))
Tangent Values : [ 0.00000000e+00 5.77350269e-01 1.00000000e+00 1.73205081e+00 -1.22464680e-16] Tangent Values : [ 0.00000000e+00 1.63312394e+16 1.73205081e+00 -1.22464680e-16]
In the above snippet, the output is a NumPy Array and the values are in Scientific Notation.
NumPy Tan on an Evenly-Spaced NumPy Array
In this example, we will create a NumPy Array of 30 evenly spaced values using
import numpy as np a = np.linspace(-(np.pi/4) , np.pi/4 , 30) print("Tangent Values: ",np.tan(a))
Tangent Values: [-1. -0.89714006 -0.80382248 -0.71829915 -0.63918754 -0.5653756 -0.49595431 -0.43016871 -0.36738181 -0.30704735 -0.24868885 -0.19188316 -0.13624728 -0.08142734 -0.02708932 0.02708932 0.08142734 0.13624728 0.19188316 0.24868885 0.30704735 0.36738181 0.43016871 0.49595431 0.5653756 0.63918754 0.71829915 0.80382248 0.89714006 1. ]
- Here, we have created a NumPy Array using
numpy.linspacewhich has 30 evenly spaced angles in radians ranging from -pi/4 to pi/4.
- The output is also a NumPy Array which is the tangent of the elements of the array.
Now, let’s visualize how the Tan function actually looks using Matplotlib Library.
Visualizing The Numpy Tan Function
import numpy as np # Importing the Matplotlib Library import matplotlib.pyplot as plt # Creating a NumPy Array of 30 evenly-spaced elements a = np.linspace((-np.pi/4),(np.pi/4),30) # Storing the tangent values in a NumPy Array b = np.tan(a) plt.plot(a, b, color = "green", marker = "o") plt.title("numpy.tan()") plt.xlabel("X") plt.ylabel("Y") plt.show()
There you go you have successfully plotted the Tangent curve.
So, that was about the NumPy Tan function, and practicing the codes along with going through the tutorial would result in the best level of understanding of the NumPy Tan function. Don’t forget to do the Task given in the tutorial.
In the next tutorial, we will be starting with Inverse Trigonometric Functions. Till then stay tuned.