1
$\begingroup$

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.

$\endgroup$
1
  • 1
    $\begingroup$ Is this to solve a Partial Differential Equation? Can you not just use a standard stencil and convolve it with the original grid? $\endgroup$ Commented Apr 27, 2020 at 21:17

1 Answer 1

2
$\begingroup$

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 ). $$

$\endgroup$
1
  • $\begingroup$ Thanks a lot! This is what I need. $\endgroup$ Commented Apr 28, 2020 at 10:10

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.