Skip to content
Tutorials Center
My learning

Topics

Lesson 4 of 38 ยท Getting Started

Using Python to Plot and Explore Waves

Use NumPy and Matplotlib in your browser to draw sine waves, change amplitude, frequency and phase, and add two waves together. No setup needed.

Equations are easier to understand when you can see them. In this course the code editors run real Python in your browser, with NumPy (fast arrays of numbers) and Matplotlib (plots) ready to use. This lesson shows the handful of tools you will use again and again to draw waves.

You do not need to be a programmer. Each program is short, and you can change the numbers and press Run to see what happens.

The sine function and radians

A smooth oscillation is described by the sine function. In physics, the angle inside sin is measured in radians. A full circle is 2ฯ€ radians (about 6.283), which equals 360ยฐ. One full cycle of a sine wave is a change of 2ฯ€ in its angle.

A wave with amplitude A, frequency f and time t has displacement:

Text
y = A sin(2ฯ€ f t)

When t increases by one period T = 1/f, the angle increases by exactly 2ฯ€ and the pattern repeats.

Your first wave plot

np.linspace(start, stop, n) creates n evenly spaced numbers. Feed them to np.sin to calculate a whole wave at once.

Python A sine wave

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Count the peaks: in 2 s at 2 Hz you should see 4 complete cycles. Now change f to 5 and A to 0.5 and run again.

Phase

Sometimes a wave does not start at zero. The phase constant ฯ† (phi) shifts the wave sideways in time:

Text
y = A sin(2ฯ€ f t + ฯ†)

A phase of ฯ€/2 (a quarter cycle) turns the sine into a cosine shape. Two waves with the same frequency but different ฯ† are "out of step" by that amount.

Comparing waves on one plot

Python Phase shift, amplitude and frequency

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Describe each curve in words: which one is taller, which one is squeezed, which one starts at the top?

Adding waves

When two waves meet in the same place, the displacements simply add. This is called superposition, and you will study it fully in Chapter 3. In code it is one plus sign.

Python Adding two waves

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The sum swells and shrinks once per second, the difference between the two frequencies. That swelling is called beats, and it is what you hear when two instruments are slightly out of tune.

Useful NumPy habits

Task Code
Make times t = np.linspace(0, 1, 1000)
Sine of an array np.sin(angle)
Value of ฯ€ np.pi
Largest value np.max(y)
Square each element y ** 2
Plot plt.plot(t, y) then plt.show()

Labelling is part of the physics

Always label axes with a quantity and its unit, such as "time (s)" or "displacement (m)". A graph without units cannot be checked. The same rule applies in exams.

Python Read values off a wave numerically

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Here the first upward crossing after the start is one period later, 0.25 s, because the wave starts at zero and rises.

Check your understanding

Quick check ยท 3 questions

of 3 answered You scored / 3 ยท perfect!

  1. 1. In y = A sin(2ฯ€ f t), what does a larger f do?

    f is the number of cycles per second, so a larger f gives more cycles in the same time and a shorter period.
  2. 2. How many radians are there in one full cycle?

    A full circle is 2ฯ€ radians (about 6.283), which is the same as 360 degrees.
  3. 3. What happens when you add two waves with nearby frequencies?

    Superposition adds the displacements. Close frequencies drift in and out of step, so the sum swells and shrinks at the difference frequency.

Summary

  • A sine wave is y = A sin(2ฯ€ f t + ฯ†); the angle is in radians.
  • np.linspace, np.sin and plt.plot are enough to draw any wave in this course.
  • Phase ฯ† shifts the wave in time; amplitude scales it; frequency compresses it.
  • Waves add by adding their displacements; close frequencies give beats.

Next: Restoring Forces and Oscillations