Divergence of vector field

- example
`divergence(V,X)`

`divergence(`

returns
the divergence
of vector field `V`

,`X`

)`V`

with respect to the
vector `X`

in Cartesian coordinates. Vectors `V`

and `X`

must
have the same length.

Find the divergence of the vector field *V*(*x*,*y*,*z*) = (*x*, 2*y*^{2},
3*z*^{3}) with
respect to vector *X* = (*x*,*y*,*z*) in
Cartesian coordinates.

syms x y z divergence([x, 2*y^2, 3*z^3], [x, y, z])

ans = 9*z^2 + 4*y + 1

Find the divergence of the curl of this vector field. The divergence of the curl of any vector field is 0.

syms x y z divergence(curl([x, 2*y^2, 3*z^3], [x, y, z]), [x, y, z])

ans = 0

Find the divergence of the gradient of this scalar function. The result is the Laplacian of the scalar function.

syms x y z f = x^2 + y^2 + z^2; divergence(gradient(f, [x, y, z]), [x, y, z])

ans = 6

Gauss' Law in differential form states that the divergence of electric field is proportional to the electric charge density as

$$\overrightarrow{\nabla}.\overrightarrow{E}\left(\overrightarrow{r}\right)=\frac{\rho \left(\overrightarrow{r}\right)}{{\epsilon}_{0}}.$$

Find the electric charge density for the electric field $$\overrightarrow{E}={x}^{2}\overrightarrow{i}+{y}^{2}\overrightarrow{j}$$.

syms x y ep0 E = [x^2 y^2]; rho = divergence(E,[x y])*ep0

rho = ep0*(2*x + 2*y)

Visualize the electric field and electric charge density for ```
-2
< x < 2
```

and `-2 < y < 2`

with ```
ep0
= 1
```

. Create a grid of values of `x`

and `y`

using `meshgrid`

.
Find the values of electric field and charge density by substituting
grid values using `subs`

. To simultaneously substitute
the grid values `xPlot`

and `yPlot`

into
the charge density `rho`

, use cells arrays as inputs
to `subs`

.

rho = subs(rho,ep0,1); v = -2:0.1:2; [xPlot,yPlot] = meshgrid(v); Ex = subs(E(1),x,xPlot); Ey = subs(E(2),y,yPlot); rhoPlot = double(subs(rho,{x,y},{xPlot,yPlot}));

Plot the electric field using `quiver`

. Overlay
the charge density using `contour`

. The contour
lines indicate the values of the charge density.

quiver(xPlot,yPlot,Ex,Ey) hold on contour(xPlot,yPlot,rhoPlot,'ShowText','on') title('Contour Plot of Charge Density Over Electric Field') xlabel('x') ylabel('y')

Was this topic helpful?