Course 6.1Lesson 2 of 3
A table lists a few angles, and a graph shows all of them. np.linspace makes the angles, np.sin works on the whole array, and plt.plot draws the curve. The wave
Try the code and the simulations beside the lesson.
A table lists a few angles. A graph shows all of them. numpy makes the angles and the values in two lines:
np.linspace(0, 2 * np.pi, 200) makes evenly spaced numbers from to . It includes both ends.np.sin(x) works on every number of the array at once. There is no loop in your code.plt.plot(x, y) joins the points with short straight lines. With points in a turn, the lines look like a smooth curve.The sine and cosine are smooth waves. Both stay between and , and both repeat every . That length is the period. The cosine is the sine slid to the left by :
So the two graphs have the same shape. The sine starts at , and the cosine starts at .
A general wave has three numbers that you can change:
Read them one at a time.
In numpy, this is A * np.sin(B * (x - C)).
The tangent is . At , and every step of from them, the cosine is zero. The tangent is undefined there, and its graph has vertical asymptotes, lines it comes close to but never touches. The graph repeats every , not every .
A computer never gives infinity here. np.tan(np.pi / 2) is about , a huge number, because the stored is a tiny bit off. That causes two problems on a plot:
plt.plot joins the last point before an asymptote to the first point after it with a steep line.Fix both. Replace the huge values with np.nan, which means "not a number" and leaves a gap in the line, and fix the height of the picture with plt.ylim:
If your angles are in degrees, convert them before the function and label the axis in degrees:
The usual slip is np.sin(d) with in degrees. That draws a crowd of tiny waves, because every degree is read as one radian.
The program draws a wave with the numbers , and beside the cosine, and the tangent with its asymptotes hidden.
np.sin. Convert with np.radians first.np.linspace(start, stop, n) makes evenly spaced numbers, both ends included.np.nan, and set plt.ylim.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
np.tan(np.pi / 2) not give infinity?np.linspace(0, 2 * np.pi, 200) make, and how far apart are they?Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.