A853 of 55
0 XP
  1. Hook
  2. Predict
  3. Name it
  4. Check
  5. Explain

A8 · Lesson 53 · Project

Simulate an oscillator and measure what it does

Lesson 42 claimed that the van der Pol oscillator settles at an amplitude of about 2, whatever it starts from, and that its period stretches as the nonlinearity grows. In this project you'll check those claims the way an engineer checks a circuit design: simulate it, then measure the result. You'll write an RK4 integrator for a system of equations, run the oscillator from a tiny starting kick, and write code that measures the settled amplitude and period from the simulated waveform, the same measurements a scope would make.

Predict

Starting the van der Pol oscillator with μ=1 from x=0.1, what amplitude do you expect it to settle at?

Name it

RK4 for systems

Write the system as 𝐱˙=𝐟(t,𝐱) with 𝐱 a NumPy array. One RK4 step of size h:

𝐤1=𝐟(t,𝐱)𝐤2=𝐟(t+h2,𝐱+h2𝐤1)𝐤3=𝐟(t+h2,𝐱+h2𝐤2)𝐤4=𝐟(t+h,𝐱+h𝐤3)
𝐱new=𝐱+h6(𝐤1+2𝐤2+2𝐤3+𝐤4)

It's exactly the scalar method from lesson 45, with arrays in place of numbers.

The oscillator

With x and y=x˙:

x˙=yy˙=μ(1−x2)y−x

Measuring from a waveform

To measure a settled oscillation, ignore the start-up (use only the second half of the run), then:

  • amplitude: the largest |x|;
  • period: the average spacing of upward zero crossings, each located between two samples by linear interpolation: if xi<0≤xi+1, the crossing is at ti+h−xixi+1−xi.

Interpolating matters: without it, the period is only known to the nearest step.

Worked example

What step size is reasonable for μ=1?

Show the solution
  1. Steps per cycle. The period is about 6.7 time units, so h=0.02 gives over 300 steps per cycle.
  2. Accuracy. RK4's error goes as h4, so with that many steps per cycle the waveform is accurate to far better than the 0.01 the measurements need.
  3. Cost. Simulating 100 time units takes 5000 steps, about 15 cycles.

Answer h=0.02 is reasonable: over 300 steps per cycle, and 5000 steps for 100 time units.

Worked example

What should the measurements show as μ grows?

Show the solution
  1. Simulate carefully. Careful simulation gives:
μAmplitudePeriod
0.12.0006.287 (close to 2π)
12.0096.663
32.0238.86
  1. The amplitude. The amplitude barely changes.
  2. The period. The period grows as the waveform turns into slow climbs and fast jumps.

Answer the amplitude stays near 2 while the period grows, from 6.287 at μ=0.1 to 8.86 at μ=3.

Check

Step 1. Write rk4_step(f, t, x, h) for a NumPy state x, returning the new state.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Step 2. Write simulate(mu, x0, h, n) returning an (n + 1) × 2 array of states for the van der Pol oscillator, starting at x0. rk4_step is included.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Step 3. Write measure(xs, h) returning (amplitude, period) from the second half of a run: the largest |x|, and the mean spacing of upward zero crossings found by linear interpolation. simulate is included.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Step 4. Use your code (or the lesson's table) to find the period for small μ = 0.02. What value does it approach as μ→0?

Explain why

Why does the measurement use only the second half of the simulation?

Practice

Keep practicing

4 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.