Numeric Solution to the wave equation [plus I need some help]

Show off your games, demos and other (playable) creations.
Post Reply
User avatar
Lightcycler
Prole
Posts: 18
Joined: Sun Dec 16, 2012 4:31 pm

Numeric Solution to the wave equation [plus I need some help]

Post by Lightcycler »

Hey guys!
I wanted to try solving differential equations numerically, and what's better for that task than the wave equation?
Click and drag to create a gauß bell with variable width.

However, I have a problem: As soon as the wave hits the boundary, or as soon as I change a single pixel's value(as opposed to changing a pair's value), the simulation does weird things: It only affects every second bin. Say, we have the array 50,50,50,50,50 and I change a value to 50,50,60,50,50, it becomes 55, 50, 55, 50, 55. And. I. Don't. Know. Why.

All derivatives are just the slope in that particular point, using central differences, and the integration just goes by adding the derivative times dt.... Which is fine, since I wanted a numeric approach. However, I can't get behind the weird bin thing. Anyone got ideas on how to fix it?

On a second approach, I tried to build it from scratch using real bins this time instead of relying on pixels, and here you can see the problem of every second bin not being affected more clearly.

If anyone's curious about my motivation, I want to do two things: write a program that can simulate fluids with different methods (including more than just surface waves) and simulating simple quantum mechanical problems by solving the schrödinger equation numerically.

So, yeah. Any ideas on why it won't work would be appreciated. Have a nice day everyone!
Attachments
bins.love
(1.86 KiB) Downloaded 139 times
wave.love
Works until the wave hits the boundary - help?
(2.27 KiB) Downloaded 158 times
JoshGrams
Prole
Posts: 33
Joined: Sat May 27, 2017 10:58 am

Re: Numeric Solution to the wave equation [plus I need some help]

Post by JoshGrams »

Lightcycler wrote: Sun Jul 02, 2017 1:23 pm All derivatives are just the slope in that particular point, using central differences
Er...it's sort of weird to calculate a central difference as an average of a forward and a backward difference. Mathematically the x[i] cancels out, but numerically it doesn't necessarily cancel perfectly.

But that's minor. The real problem is that you're trying to sample a signal above the Nyquist frequency. With discrete signals (digital rather than analog), if you process a signal with a higher frequency than half your sample rate, you'll run into these kind of aliasing problems.

Making a peak that's, say, 75, 100, 75 instead of just a single sample at 100 should solve your problem (works for me with your bins.love).

If you think about it...if all your samples are 50, then when you hit that single sample at 100, you have three slopes that are affected.

The one before is reading 50, 50, 100, so it computes (50-50)/bin and (100-50)/bin. So you get a zero and a positive value, average them out and you still get a positive slope.

The one on the peak is reading 50, 100, 50, so it computes (100-50)/bin and (50-100)/bin. You have a positive and a negative that exactly cancel each other out, so that slope is zero.

Then on the other side you have a negative and a zero, so you get a negative slope.

Now you have two single-sample spikes with a normal sample in between, so the pattern continues with the next iteration: you're just spreading these farther and farther.

HTH,

--Josh
Post Reply

Who is online

Users browsing this forum: No registered users and 99 guests