×

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 value

print ("cosine value of pi / 6 is: ", end ="")

print (math.cos(a))

Output:

cosine value of pi/6 is: 0.8660254037844387

Example 2:

import math

print ("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.3342377271245026

cos(3) : -0.9899924966004454

cos(10) : -0.8390715290764524

cos(pi/3) : 0.5000000000000001

cos(math.pi) : -1.0

cos(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 math

import numpy as np

import matplotlib.pyplot as plt

input_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 += 1

print("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 Cos And Math Acos Function

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 value

print ("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 math

print ("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.8762980611683406

acos(0) : 1.5707963267948966

acos(-1) : 3.141592653589793

acos(1) : 0.0

acos(0.75) : 0.7227342478134157

acos(0.99) : 0.1415394733244273

acos(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 math

Tup = (0.21, 0.12, 0.39, -0.89 , 0.42) # Declaration of tuple

Lis = [-0.1, 0.92, 0.35, -0.46 , 0.85] # Declaration of list

print('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.00

Arc Cosine value of Negative Number x = 9.14

Arc Cosine value of Tuple Item =2*6

Arc Cosine value of List Item = 1.21

Arc 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 += 1

print("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:

Python Math Cos And Math Acos Function

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.


Related Topics

Linear Regression using Sklearn with Example

This article will examine Python's global, local and non-local variables and show you how to use them to write code without problems. Let's quickly review what a variable in Python is...

8 minutes read.

Difference between Yield and Return in Python

Python yield statement The generators are defined by using the yield statement in Python. Generally, it converts a normal Python function into a generator.  The yield statement hauls the function and returns...

3 minutes read.

How to Define a Function in Python?

What is a Function? In programming, a piece of code that executes a specific task or a group of related operations is known as a function. What does Python Functions Do? If you...

6 minutes read.

Android apps for coding in python

Nowadays, it is the generation of smartphones. The world can be seen and acknowledged with a single click on your mobile phone. Everyone uses mobile phones to do any work,...

9 minutes read.

Python object()

Python object() class The object class in Python is a base for all classes. It returns a new featureless object. Syntax class object Parameter NA Return It returns a new featureless object. Example 1 # Python program explaining # the object()...

1 minute read.

Python e-book free download

Python is a booming language these days. It has many applications for making code easier; it is also an open-source language. There are many sources to learn python. In this...

3 minutes read.

Comment starts with the symbol in Python

Python programming language: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a...

3 minutes read.

Python data science course

What is meant by Data Science? When processing raw, structured, and unstructured data utilizing various technologies, algorithms, and the scientific method, data science is a detailed study of the enormous quantity...

4 minutes read.

Python MySQL Update Operation

Python MySQL Update Operation: In this part of tutorial, we will learn that how can we update a table present in SQL database through our Python program. As like SQL,...

4 minutes read.

How to handle exceptions in python

Prerequisite: Exceptions and errors in Python What are Exceptions? Exceptions are run-time errors that unexpectedly occur and crash a program. When occurred, it alters the flow of the program. These errors are...

9 minutes read.

Kite Python

Kite in Python: The Kite is a package provided by the python programming language; it works with the help of artificial intelligence and helps us write code inside the visual studio....

3 minutes read.

Python Program to Find the Greatest Among Three Numbers

Python Program to find the greatest among three numbers We have many approaches to get the largest of three numbers, and here we will discuss a few of them. This python...

2 minutes read.

Python Set union() method

Python Set union() method The set.union() method in Python returns a set that contains all items from the original set and all items from the specified sets. Syntax set.union(set1, set2...) Parameter set1- This parameter represents the first set...

2 minutes read.

Standard Scaler in SKLearn

The sci kit learns in python is a library thatch is used in machine learning which is used to work on data modeling.It is only focused on the data modeling,...

4 minutes read.

Global variables in python

In Python, considering the scope, variables are categorized into global and local variables. In this article, we will discuss these two types along with examples. Any variable holds a value in...

4 minutes read.

Isodate Python

Python Programming Language Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

3 minutes read.

SKLearn Linear Module

The SK learn linear module is one such module that helps to study the relationship between the independent and dependent variables.The linear module can be implemented by using the best...

3 minutes read.

Python program to check if two strings are anagram or not

Python program to check if two strings are anagram or not Problem: This is a python program that takes two strings and checks if given strings are anagram or not. Examples: Input: string1...

1 minute read.

Difference between Module and Package in Python

The main difference between the module and the package in Python is that the module can be a simple file in Python that contains the different functions and collection of...

4 minutes read.

Python Program to Find the gcd of Two Numbers

Introduction Greatest Common Divisor is the full form of gcd. The greatest common divisor, or GCD, of two numbers, is a value that can exactly divide the two digits and is...

5 minutes read.