Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

506
Views
How to simulate a wave equation

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 :)

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!