w3resource

NumPy: Compute sine, cosine and tangent array of angles given in degrees


21. Trigonometric Functions in Degrees

Write a NumPy program to compute the trigonometric sine, cosine and tangent array of angles given in degrees.

Sample Solution:

Python Code:

 # Importing the NumPy library
import numpy as np

# Computing the sine values for an array of angles given in degrees
print("sine: array of angles given in degrees")
print(np.sin(np.array((0., 30., 45., 60., 90.)) * np.pi / 180.))

# Computing the cosine values for an array of angles given in degrees
print("cosine: array of angles given in degrees")
print(np.cos(np.array((0., 30., 45., 60., 90.)) * np.pi / 180.))

# Computing the tangent values for an array of angles given in degrees
print("tangent: array of angles given in degrees")
print(np.tan(np.array((0., 30., 45., 60., 90.)) * np.pi / 180.))

Sample Output:

sine: array of angles given in degrees                                 
[ 0.          0.5         0.70710678  0.8660254   1.        ]          
cosine: array of angles given in degrees                               
[  1.00000000e+00   8.66025404e-01   7.07106781e-01   5.00000000e-01   
   6.12323400e-17]                                                     
tangent: array of angles given in degrees                              
[  0.00000000e+00   5.77350269e-01   1.00000000e+00   1.73205081e+00   
   1.63312394e+16]

Explanation:

np.sin(np.array((0., 30., 45., 60., 90.)) * np.pi / 180.) – Here np.sin() takes an array of angles in degrees, converts them to radians using the formula (angle * np.pi) / 180., and returns an array of their corresponding sine values.

np.cos(np.array((0., 30., 45., 60., 90.)) * np.pi / 180.) – Here np.cos() takes an array of angles in degrees, converts them to radians using the formula (angle * np.pi) / 180., and returns an array of their corresponding cosine values.

np.tan(np.array((0., 30., 45., 60., 90.)) * np.pi / 180.) – np.tan() takes an array of angles in degrees, converts them to radians using the formula (angle * np.pi) / 180., and returns an array of their corresponding tangent values.

Pictorial Presentation:

NumPy Mathematics: Compute sine, cosine and tangent array of angles given in degrees.
NumPy Mathematics: Compute sine, cosine and tangent array of angles given in degrees.
NumPy Mathematics: Compute sine, cosine and tangent array of angles given in degrees.

For more Practice: Solve these Related Problems:

  • Implement a function that converts an array of angles from degrees to radians and then computes sine, cosine, and tangent using np.sin, np.cos, and np.tan.
  • Test the function on known angle values and verify the outputs against standard trigonometric values.
  • Create a solution that returns the trigonometric functions as separate arrays for clarity.
  • Apply the function on a multi-dimensional array of angles and check that the output shape matches the input.

Go to:


PREV : Statistics on a Random Array
NEXT : Inverse Trigonometric Functions

Python-Numpy Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.