Python math.cos and math.acos function

Math.cos() function

In Python, the Math module is used for performing the mathematical operations. It includes the math.cos() function that is used for obtaining the cosine value of an angle in radians.

Syntax:

`math.cos(x)`

Parameter:

x : Numeric value

Returns: Returns the cosine value of an angle.

Example:

`import math a = math.pi / 6# returns the cosine valueprint ("cosine value of pi / 6 is: ", end ="") print (math.cos(a)) `

Output:

`cosine value of pi/6 is: 0.8660254037844387`

Example 2:

`import mathprint ("cos(-1.23) : ", math.cos(-1.23))print ("cos(3) : ", math.cos(3))print ("cos(10) : ", math.cos(10))print ("cos(pi/3) : ", math.cos(math.pi/3))print ("cos(math.pi) : ", math.cos(math.pi))print ("cos(2*math.pi) : ", math.cos(2*math.pi))`

Output:

`cos(-1.23) : 0.3342377271245026cos(3) : -0.9899924966004454cos(10) : -0.8390715290764524cos(pi/3) : 0.5000000000000001cos(math.pi) : -1.0cos(2*math.pi) : 1.0`

We can also obtain the graphical representation of the cos function by using the matplotlib and Numpy libraries.

Example:

`import mathimport numpy as npimport matplotlib.pyplot as pltinput_arr = np.linspace(-(2 * np.pi), 2 * np.pi, 20)out_arr = []for i in range(len(input_arr)):    out_arr.append(math.cos(input_arr[i]))    i += 1print("input_arr : ", input_arr)print("\nout_arr : ", out_arr)plt.plot(input_arr, out_arr, color='red', marker="o")plt.title("math.cos()")plt.xlabel("X")plt.ylabel("Y")plt.show()`

Output:

Python math.acos()

If we want to find the inverse of cosine then the math.acos() function is used for finding the inverse of cos.

It returns the arc cosine for the particularized expression. The value between -1 to 1 should be passed in this function.

Syntax

`math.acos(x)`

Parameter: Only a single parameter is accepted.

x : Numeric valuee to be passed to math.acos()

Returns: It returns arc cosine value.

Example:

`import math  a = math.pi / 4# returns the valueprint ("The value of arc cosine of pi / 4 is: ", end ="")  print (math.acos(a))`

Output:

`The value of arc cosine of pi / 4 is: 0.6674572160283838`

Example 2:

`import mathprint ("acos(0.64) : ", math.acos(0.64))print ("acos(0) : ", math.acos(0))print ("acos(-1) : ", math.acos(-1))print ("acos(1) : ", math.acos(1))print ("acos(0.75) : ", math.acos(0.75))print ("acos(0.99) : ", math.acos(0.99))print ("acos(pi/4) : ", math.acos(math.pi/4))`

Output:

`acos(0.64) : 0.8762980611683406acos(0) : 1.5707963267948966acos(-1) : 3.141592653589793acos(1) : 0.0acos(0.75) : 0.7227342478134157acos(0.99) : 0.1415394733244273acos(pi/4) : 0.6674572160283838`

Example 3:

The arc cosine values of different data types can be found with the math.acos() function.

Now, we can understand by taking an example.

`import mathTup = (0.21, 0.12, 0.39, -0.89 , 0.42) # Declaration of tupleLis = [-0.1, 0.92, 0.35, -0.46 , 0.85] # Declaration of listprint('Arc Cosine value of Positive Number = %.2f' %math.acos(1))print('Arc Cosine value of Negative Number = %.2f' %math.acos(-1))print('Arc Cosine value of Tuple Item = %.2f' %math.acos(Tup[3]))print('Arc Cosine value of List Item = %.2f' %math.acos(Lis[2]))print('Arc Cosine value of Multiple Number = %.2f' %math.acos(0.10 + 0.20 - 0.40))`

Output:

`Arc Cosine value of Positive number = 0.00Arc Cosine value of Negative Number x = 9.14 Arc Cosine value of Tuple Item =2*6Arc Cosine value of List Item = 1.21Arc Cosine value of Multiple number = 1.67`

Example:

We can also obtain the graphical representation of the acos function by using the matplotlib and Numpy libraries.

`import math import numpy as np import matplotlib.pyplot as plt  in_array = np.linspace(-(1 / 3.5 * np.pi), 1 / 3.5 * np.pi, 20) out_array = [] for i in range(len(in_array)):     out_array.append(math.acos(in_array[i]))     i += 1print("Input_Array : \n", in_array)  print("\nOutput_Array : \n", out_array)  plt.plot(in_array, out_array, "go-")  plt.title("math.acos()")  plt.xlabel("X")  plt.ylabel("Y")  plt.show() `

Output:

Conclusion

In the above article, We studied Python math.cos() function and also math.acos() function and we learned how to use these two  functions in Python.