How to simulate a wave equation

Viewed 139

I've been recently trying to simulate a wave equation in p5.js.

My aproach is to plot a bunch of points with (x, y) coordinates. I've used the wave equation for obtaining the movement of those particles.

This is my numerical integration method:

var pos     = this.masses[i].pos
var nextPos = this.masses[i + 1].pos
var lastPos = this.masses[i - 1].pos

var vel = this.masses[i].vel, acc = this.masses[i].acc // for simplicity

acc.x = 0
acc.y = c * c * ((nextPos.y - 2*pos.y + lastPos.y)) / d2x

vel.x += acc.x * dt
vel.y += acc.y * dt

pos.x += vel.x * dt
pos.y += vel.y * dt

My acceleration is defined as the second derivative with respect to x times the speed of the wave squared, as shown here.

So I convert my equation from this:

(d²u/dt²) = c² (d²u/dx²)

Into a discrete equation like this (where u, represents the position of one particle and c the propagation speed of the waves):

d2u = u(x + 2h, t) - 2*u(x + h, t) + u(x, t)
d2t = 1e-5 // small step
acc = c * c * (d2u / d2t)

But my result is quite strange: my wave

As you can see, the wave grows, when the expected result was something like this:

expected wave

What I'm doing wrong? Someone could help me? Thanks :)

0 Answers
Related