Python

Python Math Cos

In this article, we’ll look at how to use Python’s trigonometric cosine (cos) function. We’ll discuss using modules to implement the cos function in a Python program. We’ll also learn how to use the program’s cos function to plot graphs.

Let’s move on to the modules we can employ to construct the cos function in our code now. One of the Python Math functions, the cos function, calculates the Trigonometry Cosine for the given equation. The cos function in Python produces a number between -1 and 1. We’ll go over how to use the arithmetic cos function in this section with examples. In Python, we may utilize the math module to import and implement the cos function and other basic mathematical operations. We can utilize the NumPy module of Python instead of using the math module to implement the cos function. To successfully create the cos() function in the program, we will first need to know how to utilize Python’s math and numpy module of Python. Python’s math module includes a number of useful mathematical values and operations, including the cos() function. The math.cos() function will return the trigonometric cosine value for the input. Also, the value we pass as the function’s input should be in radians. The syntax for utilizing the math.cos() method in a Python program is as follows.

In this case, a= radian value. You may find the Cosine value for an integer or a valid numerical expression. The cos function returns the Cosine value if the number supplied is positive or negative. If the argument is anything else but a numeric value, the cos() function throws the TypeError. When you use the math cos() function, it returns the cosine value for the input you have given. The following sample program will demonstrate utilising the math module’s cos() method in Python.

Example 1:

This script computes the cosine of a 30° angle. Sexagesimal degrees are converted to radians using math.radians() function. The cosine of a 30° angle is returned by the function cos().

import math
a = math.radians (30)
print (math.cos (a))

As shown in the graphic below:.

Example 2:

Here is a Python snippet that shows how cos() works. We first imported “math” for mathematical operations before returning the cosine of pi / 6 result.

import math
abc = math.pi / 6
print (math.cos(abc))

The result can be seen here.

Example 3:

This is our final Python program, which depicts the cos() function graphically. For graphing the cosine function, we plot the angle on the horizontal x-axis and then its cosine on the vertical y-axis specifically for each angle. The outcome is a smooth curve that fluctuates from +1 to -1, as shown below. The shape is quite similar to that of the cosine function but with a 90-degree shift to the left.

import math
import numpy as np
import matplotlib.pyplot as plt
arr1 = np.linspace(-(3 * np.pi), 3 * np.pi, 30)
arr2 = []
for i in range(len(arr1)):
    arr2.append(math.cos(arr1[i]))
    i += 1
print("in_array : ", arr1)
print("\nout_array : ", arr2)
plt.plot(arr1, arr2, color = 'blue', marker = "o")
plt.title("math.cos()")
plt.xlabel("X")
plt.ylabel("Y")
plt.show()

Here you can find the values in both arrays.

Below you can observe the graphical representation of the above values.

Example 4:

In this example, we’ll look at using the cos() function, NumPy, and module in a Python program and plot graphs using them in the output. We imported NumPy and matplotlib modules first, as you can see in the code. We set the cosine values for each array value after establishing an array with radian values. The values were then printed in the output. The graph is plotted in the output after using the plot() method with variables.

import numpy as jtp  
import matplotlib.pyplot as mlt  
arr1 = jtp.linspace(-(2*jtp.pi), 2*jtp.pi, 10)  
arr2 = jtp.cos(arr1)  
print("Array Containing Radian values: ", arr1)  
print("\nArray Containing Respective cos values: ", arr2)  
mlt.plot(arr1, arr2, color = 'Red', marker = "*")  
mlt.title("Here is the Graphical representation of the cos function")  
mlt.xlabel("X-axis")  
mlt.ylabel("Y-axis")  
mlt.show()

In the screenshot below, you can observe the radian values and the arrays’ cos values.

Here is the graphical representation of the cos() function of the above values.

Conclusion:

The math module in Python provides you with some of the most commonly used mathematical functions. This module’s commonly used cos() method in this article. The cos() function in the math module of Python is used to determine the cosine value of a radian-based argument. The math module is included in the standard library of Python. Remember that it is included in every Python installation. However, before using the functions it provides, you must first import them. Import math is its syntax; after importing, we utilize the static object to call this method. The cosine of the value sent in as an argument is returned by the math cos() function. The cos() method should be supplied with a value in radians. The Math cos() method returns the cosine of the angle specified in radians, which is a numeric number between -1 and 1. Because cos() is a static Math method, it is always utilized for Math. This topic was explained in length with examples in this article.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content