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 from , what amplitude do you expect it to settle at?
About 2. The tiny start grows, because the origin is an unstable spiral, until it reaches the limit cycle. For small the amplitude is very close to 2; at it's slightly above, which your measurement should show.
Name it
RK4 for systems
Write the system as with a NumPy array. One RK4 step of size :
It's exactly the scalar method from lesson 45, with arrays in place of numbers.
The oscillator
With and :
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 ;
- period: the average spacing of upward zero crossings, each located between two samples by linear interpolation: if , the crossing is at .
Interpolating matters: without it, the period is only known to the nearest step.
Worked example
What step size is reasonable for ?
Show the solution
- Steps per cycle. The period is about 6.7 time units, so gives over 300 steps per cycle.
- Accuracy. RK4's error goes as , so with that many steps per cycle the waveform is accurate to far better than the 0.01 the measurements need.
- Cost. Simulating 100 time units takes 5000 steps, about 15 cycles.
Answer 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
- Simulate carefully. Careful simulation gives:
| Amplitude | Period | |
|---|---|---|
| 0.1 | 2.000 | 6.287 (close to ) |
| 1 | 2.009 | 6.663 |
| 3 | 2.023 | 8.86 |
- The amplitude. The amplitude barely changes.
- 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 to 8.86 at .
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 , 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 ?
Explain why
Why does the measurement use only the second half of the simulation?
The simulation starts from a tiny kick far inside the limit cycle, and for the first few cycles the oscillation is still growing toward it. Measuring the amplitude or period during that start-up would mix in the transient. By the second half of a long run, the trajectory has converged onto the limit cycle, so the waveform repeats and the measurements describe the settled oscillation. A real bench measurement does the same thing by waiting for the circuit to settle before reading the scope.
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.