w3resource logo
Python Exercises

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

Python NumPy Math, Statistics, Trigonometry and Linear algebra: Exercise-21 with Solution

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

Sample Solution:-

Python Code:

import numpy as np
print("sine: array of angles given in degrees")
print(np.sin(np.array((0., 30., 45., 60., 90.)) * np.pi / 180.))
print("cosine: array of angles given in degrees")
print(np.cos(np.array((0., 30., 45., 60., 90.)) * np.pi / 180.))
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]

Python Code Editor:

import numpy as np
print("sine: array of angles given in degrees")
print(np.sin(np.array((0., 30., 45., 60., 90.)) * np.pi / 180.))
print("cosine: array of angles given in degrees")
print(np.cos(np.array((0., 30., 45., 60., 90.)) * np.pi / 180.))
print("tangent: array of angles given in degrees")
print(np.tan(np.array((0., 30., 45., 60., 90.)) * np.pi / 180.))

Improve this sample solution and post your code through Disqus