Trigonometric functions help us find relationships between angles and sides in triangles. They are useful for analyzing waves, rotations, and cycles in data.
0
0
Trigonometric functions (sin, cos, tan) in NumPy
Introduction
To calculate the height of a tree using its shadow and angle of elevation.
To analyze periodic data like sound waves or seasonal sales patterns.
To rotate points in a 2D plane for graphics or simulations.
To model circular motion or oscillations in physics or engineering.
Syntax
NumPy
import numpy as np np.sin(angle_in_radians) np.cos(angle_in_radians) np.tan(angle_in_radians)
Input angles must be in radians, not degrees.
Use np.radians(degrees) to convert degrees to radians.
Examples
Calculate sin, cos, tan for 90 degrees (π/2 radians).
NumPy
import numpy as np angle = np.pi / 2 # 90 degrees in radians sin_val = np.sin(angle) cos_val = np.cos(angle) tan_val = np.tan(angle) print(sin_val, cos_val, tan_val)
Convert 45 degrees to radians and find its sine.
NumPy
import numpy as np degrees = 45 radians = np.radians(degrees) sin_45 = np.sin(radians) print(sin_45)
Calculate sine for multiple angles at once using an array.
NumPy
import numpy as np angles = np.array([0, np.pi/4, np.pi/2]) sin_values = np.sin(angles) print(sin_values)
Sample Program
This program converts a list of angles from degrees to radians, then calculates and prints their sine, cosine, and tangent values in a clear table.
NumPy
import numpy as np # Angles in degrees angles_degrees = np.array([0, 30, 45, 60, 90]) # Convert degrees to radians angles_radians = np.radians(angles_degrees) # Calculate sine, cosine, and tangent sin_values = np.sin(angles_radians) cos_values = np.cos(angles_radians) tan_values = np.tan(angles_radians) # Print results in a table format print("Angle (deg) | sin | cos | tan") print("---------------------------------------") for deg, s, c, t in zip(angles_degrees, sin_values, cos_values, tan_values): print(f"{deg:10} | {s:7.3f} | {c:7.3f} | {t:7.3f}")
OutputSuccess
Important Notes
Tangent of 90 degrees (π/2 radians) is very large because it approaches infinity.
Always convert degrees to radians before using numpy trigonometric functions.
Use numpy arrays to calculate trig functions on many angles efficiently.
Summary
Trigonometric functions relate angles to ratios of sides in triangles.
Use numpy's sin, cos, and tan functions with angles in radians.
Convert degrees to radians with np.radians() before calculations.