# Divergence and gradient operators in two dimensions

Divergence of a vector $$\mathbf{a}$$ can be numerically written as, $$\begin{eqnarray} \nabla \cdot \mathbf{a} \approx \sum_{i=0-9} w_i\mathbf{a(x+c_i)}\cdot \mathbf{c}_i \end{eqnarray}$$

The lattice directions $$\mathbf{c}_i$$ for two dimensions can be seen in the following figure and weights $$w_i$$ are $$4/9,1/9,1/36$$ respectively for directions that have $$0,1,\sqrt{2}$$ distance from center. I would like to check the error terms with above expression with Mathematica. I do not know how to expand a vector series so I started working on x component of $$\mathbf{a} =f$$ and proceeded as follows

c = {{0, 0}, {1, 0}, {0, 1}, {-1, 0}, {0, -1}, {1, 1}, {-1,
1}, {-1, -1}, {1, -1}}
w = {4/9, 1/9, 1/9, 1/9, 1/9, 1/36, 1/36, 1/36, 1/36}

Simplify[Normal[
Series[w[] c[[2, 1]] f[(x + c[[2, 1]]) t, (y + c[[2, 2]]) t] +
w[] c[[4, 1]] f[(x + c[[4, 1]]) t, (y + c[[4, 2]]) t] +
w[] c[[3, 1]] f[(x + c[[3, 1]]) t, (y + c[[3, 2]]) t] +
w[] c[[5, 1]] f[(x + c[[5, 1]]) t, (y + c[[5, 2]]) t] +
w[] c[[6, 1]] f[(x + c[[6, 1]]) t, (y + c[[6, 2]]) t] +
w[] c[[8, 1]] f[(x + c[[8, 1]]) t, (y + c[[8, 2]]) t] +
w[] c[[7, 1]] f[(x + c[[7, 1]]) t, (y + c[[7, 2]]) t] +
w[] c[[9, 1]] f[(x + c[[9, 1]]) t, (y + c[[9, 2]]) t], {t, 0,
4}]] /. t -> 1] /. {x -> 0, y -> 0}


Similar expression for y component of $$\mathbf{a}=g$$ can be derived and finally combined. Now my question is how to do it directly working with vectors, instead of components separately. Also I have used a brute force method for running over different lattices. How can I do it with more elegence?

Try this

c = {{0, 0}, {1, 0}, {0, 1}, {-1, 0}, {0, -1}, {1, 1}, {-1,  1}, {-1, -1}, {1, -1}}
w = {4/9, 1/9, 1/9, 1/9, 1/9, 1/36, 1/36, 1/36, 1/36}

Simplify[Normal[Series[Sum[w[[i]]*{f[x + c[[i,1]]*t, y + c[[i,2]]*t],
g[x + c[[i,1]]*t, y + c[[i,2]]*t]} . c[[i]], {i, 2, Length[c]}], {t, 0, 4}]]] /.
t -> 1 /. {x -> 0, y -> 0}

(*(1/18)*(6*(Derivative[1, 0][f][0, 0] + Derivative[0, 1][g][0, 0]) +
Derivative[1, 2][f][0, 0] + Derivative[3, 0][f][0, 0] + Derivative[0, 3][g][0, 0] +
Derivative[2, 1][g][0, 0])*)


Your hand series seems to start at i = 2, so that's where I started the sum. I'm not sure why, but you can change it if you need to.

• thanks a lot for the answer, it works as needed. Regarding starting the series from 2, the first term with $i=1$ is zero. But you are right, I should have mentioned it. Jan 3, 2019 at 13:39