Trigonometric functions help us find relationships between angles and sides in triangles. They are useful for analyzing waves, rotations, and cycles in data.
Trigonometric functions (sin, cos, tan) in NumPy
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
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
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)
NumPy
import numpy as np degrees = 45 radians = np.radians(degrees) sin_45 = np.sin(radians) print(sin_45)
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}")
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.
Practice
1. What does the numpy function
np.sin() calculate when given an angle in radians?easy
Solution
Step 1: Understand the function purpose
The functionnp.sin()calculates the sine value of an angle given in radians.Step 2: Recall sine definition in triangles
Sine of an angle is the ratio of the length of the opposite side to the hypotenuse in a right triangle.Final Answer:
The sine of the angle, which is the ratio of the opposite side to the hypotenuse in a right triangle. -> Option DQuick Check:
np.sin() gives sine ratio [OK]
Hint: Remember sin = opposite/hypotenuse in triangles [OK]
Common Mistakes:
- Confusing sine with cosine or tangent
- Thinking np.sin() converts radians to degrees
- Using degrees directly without conversion
2. Which of the following is the correct way to calculate the cosine of 60 degrees using numpy?
easy
Solution
Step 1: Recognize angle units for numpy trig functions
numpy trigonometric functions expect angles in radians, not degrees.Step 2: Convert degrees to radians before using np.cos()
Usenp.radians(60)to convert 60 degrees to radians, then applynp.cos().Final Answer:
np.cos(np.radians(60)) -> Option AQuick Check:
Convert degrees to radians before trig functions [OK]
Hint: Always convert degrees to radians with np.radians() [OK]
Common Mistakes:
- Passing degrees directly to np.cos()
- Using np.degrees() instead of np.radians()
- Passing a trig function inside np.cos()
3. What is the output of the following code?
import numpy as np angles = np.array([0, 90, 180]) radians = np.radians(angles) sin_values = np.sin(radians) print(np.round(sin_values, 2))
medium
Solution
Step 1: Convert angles to radians
Angles 0, 90, 180 degrees are converted to radians: 0, π/2, π.Step 2: Calculate sine values and round
sin(0) = 0, sin(π/2) = 1, sin(π) = 0. Rounded to 2 decimals: [0.00, 1.00, 0.00].Final Answer:
[0.00 1.00 0.00] -> Option CQuick Check:
sin(0, 90, 180) = [0, 1, 0] [OK]
Hint: Recall sin(0)=0, sin(90°)=1, sin(180°)=0 [OK]
Common Mistakes:
- Not converting degrees to radians
- Confusing sine values at 90 and 180 degrees
- Forgetting to round output
4. The following code is intended to calculate the tangent of 45 degrees but gives an incorrect result. What is the error?
import numpy as np tan_45 = np.tan(45) print(tan_45)
medium
Solution
Step 1: Identify input units for np.tan()
numpy trigonometric functions require angles in radians, not degrees.Step 2: Correct the input by converting degrees to radians
Usenp.radians(45)to convert 45 degrees before passing tonp.tan().Final Answer:
The angle 45 should be converted to radians before using np.tan(). -> Option BQuick Check:
Convert degrees to radians before np.tan() [OK]
Hint: Always convert degrees to radians before trig functions [OK]
Common Mistakes:
- Passing degrees directly to np.tan()
- Using np.degrees() instead of np.radians()
- Assuming np.tan() works with degrees
5. You have an array of angles in degrees:
angles = np.array([30, 45, 60]). You want to create a new array that contains the tangent values of these angles but only include values where the tangent is less than 2. Which code correctly does this?hard
Solution
Step 1: Convert degrees to radians for tangent calculation
Usenp.radians(angles)to convert the array of degrees to radians before applyingnp.tan().Step 2: Filter tangent values less than 2
Use boolean indexingtan_vals[tan_vals < 2]to select values less than 2.Final Answer:
tan_vals = np.tan(np.radians(angles)); filtered = tan_vals[tan_vals < 2] -> Option AQuick Check:
Convert degrees, then filter tan values less than 2 [OK]
Hint: Convert degrees first, then filter with boolean indexing [OK]
Common Mistakes:
- Not converting degrees to radians
- Filtering with wrong comparison operator
- Using np.degrees() instead of np.radians()
