Get the Trigonometric cosine of an angle in Python

PythonNumpyServer Side ProgrammingProgramming

To find the Trigonometric cosine, use the numpy.cos() method in Python Numpy. The method returns the sine of each element of the 1st parameter x. The 1st parameter, x, is an Angle, in radians (2pi means 360 degrees). The 2nd and 3rd parameters are optional. The 2nd parameter is an ndarray, A location into which the result is stored.

If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshlyallocated array is returned. A tuple (possible only as a keyword argument) must have length equal to the number of outputs.

The 3rd parameter is the condition is broadcast over the input. At locations where the condition is True, the out array will be set to the ufunc result. Elsewhere, the out array will retain its original value.

Steps

At first, import the required library −

import numpy as np

Get the Trigonometric cosine. Finding cosine 90 degrees −

print("\nResult...",np.cos(np.pi/2.))

Finding cosine 60 degrees −

print("\nResult...",np.cos(np.pi/3.))

Finding cosine 45 degrees −

print("\nResult...",np.cos(np.pi/4.))

Finding cosine 30 degrees −

print("\nResult...",np.cos(np.pi/6.))

Finding cosine 0 degrees −

print("\nResult...",np.cos(0))

Example

import numpy as np

# To find the Trigonometric cosine, use the numpy.cos() method in Python Numpy
# The method returns the sine of each element of the 1st parameter x. This is a scalar if is a scalar.

print("Get the Trigonometric cosine...")

# finding cosine 90 degrees
print("\nResult...",np.cos(np.pi/2.))

# finding cosine 60 degrees
print("\nResult...",np.cos(np.pi/3.))

# finding cosine 45 degrees
print("\nResult...",np.cos(np.pi/4.))

# finding cosine 30 degrees
print("\nResult...",np.cos(np.pi/6.))

# finding cosine 0 degrees
print("\nResult...",np.cos(0))

Output

Get the Trigonometric cosine...

Result... 6.123233995736766e-17

Result... 0.5000000000000001

Result... 0.7071067811865476

Result... 0.8660254037844387

Result... 1.0
raja
Updated on 25-Feb-2022 05:21:11

Advertisements