How to calculate and plot the inverse cosine in python ?

To find the inverse cosine, a solution is to use either math's acos() or numpy's arccos(). Examples:

Using Math acos()

To calculate inverse cosine in python, first use the "math" module to find the inverse cosine of a given angle. The syntax for this is

import math

math.acos(x)

This takes as its argument a number between -1 and 1 and returns the arc-cosine of that number in radians. For example, if you wanted to calculate the inverse cosine of root suqre of 3 divided by 2, your code would look like this:

Example 1:

import math

x = math.sqrt(3) / 2

y = math.acos(x)

print(y)

The above code will print out

0.5235987755982989

which is the arc-cosine expressed in radians. You can also convert this result into degrees by multiplying it by 180/math.pi. Alternatively, you can use the math module's "degrees" function, which directly converts radians into degrees:

math.degrees(y)

This will print out

30.000000000000004

degrees

Example 2:

x = math.sqrt(2) / 2

y = math.acos(x)

print( math.degrees(y) )

Output

45.0

Example 3:

x = 1.0 / 2

y = math.acos(x)

print( math.degrees(y) )

Output

59.99999999999999

It is important to note that if the input number is outside of the range -1 and 1, then an error will be raised since it cannot be calculated with this function.

Using Numpy arccos()

Numpy's arccos() function enables to calculate the inverse cosine of a given number within [-1, 1].

The syntax for Numpy’s arccos() function is as follows:

import numpy as np

np.arccos(x)

An example of using the arccos() function can be seen below:

x = 1.0 / 2

y = np.arccos(x)

print( math.degrees(y) )

The above code will print out

59.99999999999999

Plot inverse cosine using matplotlib

If you wish to plot the inverse cosine graphically, then you can use the matplotlib library within python.

import matplotlib.pyplot as plt
import numpy as np

X = np.linspace(-1,1,100)

Y = np.arccos(X)

plt.plot(X,Y)

plt.grid()

plt.title('How to calculate and plot the inverse cosine in python ?')

plt.savefig("inverse_cosine_01.png", bbox_inches='tight',dpi=200, facecolor='white')

plt.show()

How to calculate and plot the inverse cosine in python ?
How to calculate and plot the inverse cosine in python ?

Another example

from pylab import *
from scipy import misc

ax = subplot(111)

plt.plot(X,Y)

grid(True)

ax.spines['left'].set_position('zero')
ax.spines['right'].set_color('none')
ax.spines['bottom'].set_position('zero')
ax.spines['top'].set_color('none')

y_label_list = [r'$\pi/4$', r'$\pi/2$', r'$3\pi/4$', r'$\pi$']

ax.set_yticks([math.pi/4,math.pi/2,3*math.pi/4,math.pi])

ax.set_yticklabels(y_label_list)

title('How to calculate and plot the inverse cosine in python ?')

savefig("inverse_cosine_02.png", bbox_inches='tight',dpi=200, facecolor='white')

show()

How to calculate and plot the inverse cosine in python ?
How to calculate and plot the inverse cosine in python ?

References

Links Site
math docs.python.org
numpy.arccos numpy.org
numpy.radians() numpy.org
How to calculate the cosine of an angle in python ? moonbooks.org
Image

of