0
0
NumPydata~20 mins

Trigonometric functions (sin, cos, tan) in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Trigonometry Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of numpy sine function on array
What is the output of this code snippet using numpy's sine function on an array of angles in radians?
NumPy
import numpy as np
angles = np.array([0, np.pi/2, np.pi])
result = np.sin(angles)
print(result)
A[1. 0. 1. ]
B[0. 0.70710678 1. ]
C[0. 1. 0. ]
D[0. 1. -1. ]
Attempts:
2 left
💡 Hint
Recall sine values at 0, 90, and 180 degrees (in radians: 0, π/2, π).
data_output
intermediate
1:30remaining
Number of elements in cosine output array
After running this code, how many elements does the resulting cosine array contain?
NumPy
import numpy as np
angles = np.linspace(0, 2*np.pi, 50)
cos_values = np.cos(angles)
print(len(cos_values))
A50
B49
C51
D52
Attempts:
2 left
💡 Hint
np.linspace(start, stop, num) creates 'num' evenly spaced points.
🔧 Debug
advanced
2:00remaining
Identify the error in tangent calculation
What error will this code raise when calculating tangent values for these angles?
NumPy
import numpy as np
angles = np.array([0, np.pi/2, np.pi])
tan_values = np.tan(angles)
print(tan_values)
AValueError: invalid value in tan
BNo error, outputs finite values
CTypeError: unsupported operand type(s) for tan
DRuntimeWarning: divide by zero encountered in tan
Attempts:
2 left
💡 Hint
Consider the tangent of π/2 (90 degrees).
🧠 Conceptual
advanced
1:30remaining
Understanding output range of cosine function
Which of the following statements about the output range of numpy's cosine function is correct?
AThe output values are always between 0 and 1 inclusive.
BThe output values are always between -1 and 1 inclusive.
CThe output values can be any real number depending on input.
DThe output values are always positive.
Attempts:
2 left
💡 Hint
Recall the range of the cosine function in trigonometry.
🚀 Application
expert
3:00remaining
Calculate and plot sine and cosine waves
Which option correctly calculates sine and cosine values for 100 points between 0 and 2π and plots both on the same graph?
A
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 2*np.pi, 100)
plt.plot(x, np.sin(x), label='sin')
plt.plot(x, np.cos(x), label='cos')
plt.legend()
plt.show()
B
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 2*np.pi, 100)
plt.plot(np.sin(x), x, label='sin')
plt.plot(np.cos(x), x, label='cos')
plt.legend()
plt.show()
C
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 2*np.pi, 100)
plt.plot(x, np.sin(x))
plt.plot(x, np.cos(x))
plt.show()
D
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 2*np.pi, 100)
plt.plot(x, np.tan(x), label='tan')
plt.plot(x, np.sin(x), label='sin')
plt.legend()
plt.show()
Attempts:
2 left
💡 Hint
Check if both sine and cosine are plotted against the same x values and if labels are included.