NumPy: Using np.cos() and np.arccos() functions (5 examples)

Updated: February 26, 2024 By: Guest Contributor Post a comment

Introduction

NumPy is a fundamental package for scientific computing in Python. It offers an array object, various derived objects, and numerous routines for fast operations on arrays. Two functions that often come in handy, especially when dealing with trigonometric operations, are np.cos() and np.arccos(). This tutorial will guide you through using these functions with five comprehensive examples, expanding from basic usage to more complex applications.

Understanding np.cos()

The np.cos() function computes the cosine of the given angle (in radians). This function plays a crucial role in trigonometry, physics, and engineering tasks where cosine values of angles are needed.

import numpy as np

# Example 1: Basic usage
angle_rad = np.pi / 4 # 45 degrees in radians
print('Cosine of 45 degrees:', np.cos(angle_rad))

Output:

Cosine of 45 degrees: 0.7071067811865476

Understanding np.arccos()

The inverse of the cosine function is np.arccos(), which returns the angle in radians for a given cosine value. It’s particularly useful in solving problems that require finding an angle when the cosine value is known.

import numpy as np

# Example 2: Basic usage of np.arccos()
cos_value = 0.5
print('Angle corresponding to cos value 0.5:', np.arccos(cos_value))

Output:

Angle corresponding to cos value 0.5: 1.0471975511965979

Working with Arrays

NumPy excels in handling arrays. Both np.cos() and np.arccos() can operate on arrays, making these functions extremely useful for vectorized computations.

import numpy as np

# Example 3: Applying np.cos() and np.arccos() on arrays
angles_rad = np.array([0, np.pi/2, np.pi])
cos_values = np.cos(angles_rad)
print('Cosine values:', cos_values)
angles_from_cos = np.arccos(cos_values)
print('Angles from cosine values:', angles_from_cos)

Output:

Cosine values: [ 1.000000e+00  6.123234e-17 -1.000000e+00]
Angles from cosine values: [0.         1.57079633 3.14159265]

Advanced Applications

Moving onto more sophisticated examples, you can integrate these functions into complex mathematical and physics calculations.

Finding Projectile Motion Angle

Let’s calculate the angle required for a projectile to achieve a specific range under gravity, assuming the initial velocity is known.

import numpy as np

# Example 4: Finding the projectile motion angle
velocity = 20 # m/s
distance = 45 # m
gravitational_acceleration = 9.81 # m/s^2
cos_value = distance * gravitational_acceleration / (velocity ** 2)
angle_1 = np.arccos(cos_value / 2)
angle_2 = np.pi - angle_1
print('Required angles for the range:', angle_1, ',', angle_2)

Output:

Required angles for the range: 0.9862603034044354 , 2.1553323501853576

Application in Wave Mechanics

Next, we explore the use of the cosine function in calculating the displacement of a wave at different points in time.

import numpy as np

# Example 5: Wave displacement calculation
frequency = 2 # Hz
amplitude = 1.5 # meters
times = np.linspace(0, 2*np.pi, 100)
displacements = amplitude * np.cos(2 * np.pi * frequency * times)

# Plotting the result (assuming matplotlib is imported)
import matplotlib.pyplot as plt
plt.plot(times, displacements)
plt.title('Wave Displacement Over Time')
plt.xlabel('Time (seconds)')
plt.ylabel('Displacement (meters)')
plt.show()

Conclusion

The np.cos() and np.arccos() functions in NumPy offer flexibility and precision for trigonometric calculations. Whether you’re working with simple angles, diving into array-based vectorized operations, or tackling advanced mathematical models, these functions provide a solid foundation. This guide showcased their potential through progressive examples, paving the way for their application in further complex scenarios.