0
$\begingroup$

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:
u(i,0)=sin(i)
du(i,0)/dt=0

Boundary Condition:
u(0,j)=0
u(95,j)=0

dt=1
dx=5

What is the result when t=5.

$\endgroup$
3
$\begingroup$

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]
     }
     else
     {
         unew[i] = u[i]
     }
}

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

$\endgroup$
  • $\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 '15 at 1:10
  • $\begingroup$ Thanks Tanmay, I added example values so that I can understand more on the example. $\endgroup$ – mich May 29 '15 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: tanmayagrawal7@gmail.com $\endgroup$ – Tanmay Agrawal May 29 '15 at 8:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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