Page 1 of 1

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

Posted: Sun Jul 02, 2017 1:23 pm
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!

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

Posted: Sun Jul 02, 2017 11:08 pm
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