Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to figure out how to draw a wave equation progress in a 2D graph with Matlab.

I found this piece of code which effectively draw a 2D wave placing a droplet in the middle of the graph (I almost fully commented it to simplify things) and then letting it expanding till the border, then bouncing back (how can this code do that? I don't see any code taking the graph dimensions into account) and forth

The code should be pretty simple to someone who knows how a wave equation work, but to me.. it's just dark.

What is the C1 constant? And how does the "un" line succeed into evolving the wave with the convolution? I can't find theory about that nor anything that explains this to me

c=1; % speed of wave;
dx=1; % space step;
dt=0.05; % time step;

szy=200; % size of the drawing area

tm=3000; % time
k=0.002; % decay factor
dsz=3; % droplet size
da=0.07; % droplet amplitude

x=0:dx:(szx-dx); % Generate a x vector from 0 to 200 with step 1
y=0:dx:(szy-dx); % Generate a y vector from 0 to 200 with step 1
t=0:dt:tm; % time, generate a time vector from 0 to 3000 with step 0.05

[X,Y] = meshgrid(x,y); % Create base planes for the graph


u=zeros(Ly,Lx); % initial value (a null matrix for u)
uo=u; % previous = curent => velocties =0

% Initialize the drawing and the axis
close all;
set(ha,'clim',[-1 1]); % set colors

D=[0 1 0; 1 -4 1; 0 1 0]; % 2d laplace operator, this will be used in the convolution (probably a laplace 2d operator substitute?)

 % Kdt = decay factor * dt (timestep), that's how much the wave decays per time step
 % !!!! this c1 is constant, but I can't figure out what it is

 % droplet as gaussian, the initial droplet is nothing else but a gaussian in a 2D matrix
 [Xd,Yd] = meshgrid(xd,yd);

 % Used to count droplets
 One_single_droplet = 0;

 for tt=t
     % !!!!!! Calculate wave equation evolution -> this is exactly where I can't figure out what does this mean

    uo=u; % current become old
    u=un; % new become current

       % Draw the wave updating the graph matrix values (u)

    % droplets, just one
    if One_single_droplet == 0
        x0d= 100;
        y0d= 100; % droplet center

        % Place the droplet centered on x0d and y0d: adds to the u matrix (the wave function) the gaussian 2d droplet

        One_single_droplet = 1;

share|cite|improve this question
This question is more suited to or maybe even SO. – Inquest Feb 11 '12 at 6:50
up vote 3 down vote accepted

This is a discretized version of the wave equation that is used by iterating. So, let us consider

$$\frac{1}{c^2}\frac{\partial^2u}{\partial t^2}-\frac{\partial^2u}{\partial x^2}-\frac{\partial^2u}{\partial y^2}=0.$$

Now, you can discretize the derivatives in the following way

$$\frac{\partial u}{\partial t}=\frac{u(i,k,l)-u(i-1,k,l)}{\Delta t}$$

$$\frac{\partial u}{\partial x}=\frac{u(i,k,l)-u(i,k-1,l)}{\Delta x}$$

$$\frac{\partial u}{\partial y}=\frac{u(i,k,l)-u(i,k,l-1)}{\Delta x}$$

Then just repeat them to get second derivatives and you will get

$$\frac{\partial^2 u}{\partial t^2}=\frac{u(i+1,k,l)-2u(i,k,l)+u(i-1,k,l)}{(\Delta t)^2}$$

$$\frac{\partial^2 u}{\partial x^2}=\frac{u(i,k+1,l)-2u(i,k,l)+u(i,k-1,l)}{(\Delta x)^2}$$

$$\frac{\partial^2 u}{\partial y^2}=\frac{u(i,k,l+1)-2u(i,k,l)+u(i,k,l-1)}{(\Delta x)^2}.$$

Now, you can write an iteration scheme to solve your equation by substitution of these discrete derivatives as

$$\frac{u(i+1,k,l)-2u(i,k,l)+u(i-1,k,l)}{c^2(\Delta t)^2}=\frac{u(i,k+1,l)-2u(i,k,l)+u(i,k-1,l)}{(\Delta x)^2}+\frac{u(i,k+1,l)-2u(i,k,l)+u(i,k-1,l)}{(\Delta x)^2}$$

and so


As you see from this scheme, the discretized Laplace operator has the coefficients of the matrix D in the matlab code and $c_1$ is exactly the one used in the code. Laplace operator is computed through conv2 that multiplies two matrices as it should. But note that a decay term has been added (those multiplied by k) and you must use for $u(0,k,l)$ the initial Gaussian wave-packet to start iteration.

This matlab code gives a fine example of application of an iteration scheme to solve a pde.

share|cite|improve this answer
You're right! The 2-kdt and kdt+1 terms seems to balance the compromise between gain/decay, the rest is finally clear. Thank you very much – Paul Feb 11 '12 at 22:12

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.