I'm following the article at http://www.paykin.info/irina/project_2.jsp Finite Difference method.

How to interpret this one? How to convert this to pseudo code? $$u(i,j+1) = 2u(i,j) + \left(\frac{k}{h}\right)^2[u(i+1,j) - 2u(i,j) + u(i-1,j)] - u(i,j-1)$$

Assuming the ff given values (Im not sure if values are possible).

Initial Condition:

Boundary Condition:


What is the result when t=5.


1 Answer 1


The term on LHS is the variable's value at next time step i.e. (j+1) which depends on the values of variable u at previous time steps j and (j-1).

Pseudo code could be like this:

Initialize u(i,j) and let it equal to u(i,j-1).

For (all grid points except those on boundary), use the discretized equation to advance in time.

For boundary nodes, use the boundary conditions.

If I have to write in C with i varying from 0 to N, I can write:

for (i = 0; i <= N; i++)
     if (i > 0 && i < N)
          unew[i] = 2*u[i] + (dt/dx)*(dt/dx)*(u[i+1] - 2*u[i] + u[i-1]) - uold[i]
         unew[i] = u[i]

Hope this helps. If still got some query, feel free to ask.

  • $\begingroup$ let say given initial condition u(i,0)=some function. where to initialize that one? Regarding the unew and uold, are that additional variables? With given values of dt and dx, how to get the result when t=5? $\endgroup$
    – mich
    May 29, 2015 at 1:10
  • $\begingroup$ Thanks Tanmay, I added example values so that I can understand more on the example. $\endgroup$
    – mich
    May 29, 2015 at 1:31
  • $\begingroup$ All the variables are defined and declared at the beginning of simulation. Suppose if you want to have a sinusoidal initial condition, you can write in C as: for (i = 0; i <= N; i++) { u[i] = sin[i]; unew[i] = sin(i); uold[i] = sin(i); } If you still have a problem regarding coding or the physics, feel free to contact me: [email protected] $\endgroup$ May 29, 2015 at 8:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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