This site is version 0.0.1. Most of the code on this site is drafted by an agent and needs review. I apologize for the AI-speak, we are working on rewriting it!
viv-lang.org

PDE solvers · Waves and transport (hyperbolic)

The wave equation

Method
leapfrog: centered in time and space, second order, explicit
Checked
a standing wave against its exact solution; the measured order; and that the energy does not drift

A string fixed at both ends, plucked into its fundamental: . Centered differences in both variables give the leapfrog scheme, , with .

Leapfrog has no dissipation at all — its amplification factor has modulus one for — so the string's energy, , should neither grow nor decay over many periods. That is the test a diffusive scheme cannot pass.

The first step needs care. Leapfrog wants two earlier levels and the problem gives one, plus an initial velocity; taking (a Taylor step, with zero velocity) keeps the whole run second order. A plain copy would make it first.

What the run shows

A standing wave over half a period
t = 0t = 1/4t = 1/2t = 3/4t = 1
−1.0−0.500.51.00.20.40.60.8xu(x, t)
  • leapfrog matches the standing wave sin(pi x) cos(pi c t)
  • it is second order
  • the energy does not drift over twenty periods
e1
0.00013371
e2
3.34241e-05
order
2.00015
drift
9.18755e-15

Computed when this page was built, by the same interpreter that runs it in your browser: 3 of 3 tests, 0.17 s. The numbers above are the program's show lines.

The program

89 lines, 3 functions. It is editable: change it and press Run (or ⌘/Ctrl + Enter). The tests at the bottom run too, so if your change breaks the solver, a test fails.

second_difference
The second difference of u, with both ends held at zero.
standing_wave_error
Leapfrog to t_end on n interior points; the L2 error against sin(pi x) cos(pi c t).
energy_drift
The discrete energy after each of `periods` periods, relative to the first.
Viv [1]

Open in the workbench The atlas Download wave_1d.viv