3 % compute divergence of vector field
5 % f{1}, f{2}, f{3} vector components on staggered mesh (midpoints of sides)
7 % s{1},s{2} terrain gradient at mesh cell midpoints
9 % u = df{1}/x1 + df{2}/dx2 + df{3}/dx3
16 dudx = (u(2:end,:,:)-u(1:end-1,:,:))/h(1);
17 dvdy = (v(:,2:end,:)-v(:,1:end-1,:))/h(2);
18 dwdz = (w(:,:,2:end)-w(:,:,1:end-1))/h(3);
19 d = dudx + dvdy + dwdz;
21 error('slant not supported');
23 dudz = (u(1:end-1,:,3:end)+u(2:end,:,3:end)...
24 -u(1:end-1,:,1:end-2)-u(2:end,:,1:end-2))/(4*h(3));
25 dvdz = (v(:,1:end-1,3:end)+v(:,2:end,3:end)...
26 -v(:,1:end-1,1:end-2)-v(:,2:end,1:end-2))/(4*h(3));
30 d(:,:,k) = d(:,:,k) + dudz .* sx + dvdz .* sy;