Loop to match five point differentiation formula

I have a uniform grid (x,y) and I need help to generate a loop to compute (x-2h,y), (x-h,y),(x+h,y),(x+2h,y) for every point of x, and the same thing for every point of y , (x,y-2h),(x,y-h), (x,y+h), (x,y+2h).

x = {-0.5, -0.5, -0.5, -0.5, -0.28700000000000003,
-0.28700000000000003, -0.28700000000000003, -0.28700000000000003,
-0.07400000000000001, -0.07400000000000001, -0.07400000000000001,
-0.07400000000000001, 0.139, 0.139, 0.139, 0.139, 0.352, 0.352,
0.352, 0.352, 0.565, 0.565, 0.565, 0.565};

y = {0.001, 0.214, 0.427, 0.64, 0.001, 0.214, 0.427, 0.64,
0.001, 0.214, 0.427, 0.64, 0.001, 0.214, 0.427, 0.64,
0.001, 0.214, 0.427, 0.64, 0.001, 0.214, 0.427, 0.64};


where h = 0.213;

Thanks.

• Is this to solve a Partial Differential Equation? Can you not just use a standard stencil and convolve it with the original grid? – SuperCiocia Apr 27 at 21:17

What you want can be easily written as a single Table:

dx = Table[{{ix - 2*h, iy}, {ix - h, iy}, {ix + h, iy}, {ix + 2*h, iy}}, {ix, x}, {iy, y}]

dy = Table[{{ix, iy - 2*h}, {ix, iy - h}, {ix, iy + h}, {ix, iy + 2*h}}, {ix, x}, {iy, y}]


such that the first line reads dx[[1]][[1]] = {{-0.926, 0.001}, {-0.713, 0.001}, {-0.287, 0.001}, {-0.074, 0.001}}, which is all the variations for the first x (-0.5) at a fixed y (0.001).

However, if you are trying to solve a Partial Differential Equation, you can try to use a ready-made stencil which has probably already been derived from the above finite-difference method formulae.

For instance, if you are dealing with a Laplacian in 2D, you can convolve your grid of $$(x,y)$$ points with the following kernel: $$\nabla^2 = \left ( \begin{array}{ccc} 0 & 1 & 0 \\ 1 & -4 & 1 \\ 0 & 1 & 0 \end{array} \right ).$$

• Thanks a lot! This is what I need. – Ghady Apr 28 at 10:10