Main Content

divergence

Divergence of symbolic vector field

Description

d = divergence(V,X) returns the divergence of symbolic vector field V with respect to vector X in Cartesian coordinates. Vectors V and X must have the same length.

example

d = divergence(V) returns the divergence of the vector field V with respect to a default vector constructed from the symbolic variables in V.

Examples

collapse all

Find the divergence of the vector field V(x,y,z)=(x,2y2,3z3) with respect to vector X=(x,y,z).

syms x y z
V = [x 2*y^2 3*z^3];
X = [x y z];
div = divergence(V,X)
div = 9z2+4y+1

Show that the divergence of the curl of the vector field is 0.

divCurl = divergence(curl(V,X),X)
divCurl = 0

Find the divergence of the gradient of the scalar field f(x,y,z)=x2+y2+z2. The result is the Laplacian of the scalar field.

syms x y z
f = x^2 + y^2 + z^2;
divGrad = divergence(gradient(f,X),X)
divGrad = 6

Gauss’s Law in differential form states that the divergence of an electric field is proportional to the electric charge density.

E(r)=ρ(r)ϵ0

Find the electric charge density for the electric field E=x2iˆ+y2jˆ.

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

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 the electric field and charge density by substituting grid values using subs. Simultaneously substitute the grid values xPlot and yPlot into the charge density rho by using cell 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")

Figure contains an axes object. The axes object with title Contour Plot of Charge Density Over Electric Field, xlabel x, ylabel y contains 2 objects of type quiver, contour.

Since R2023a

Derive the electromagnetic wave equation in free space without charge and without current sources from Maxwell's equations.

First, create symbolic scalar variables to represent the vacuum permeability and permittivity. Create a symbolic matrix variable to represent the Cartesian coordinates. Create two symbolic matrix functions to represent the electric and magnetic fields as functions of space and time.

syms mu_0 epsilon_0
syms X [3 1] matrix
syms E(X,t) B(X,t) [3 1] matrix keepargs

Next, create four equations to represent Maxwell's equations.

Maxwell1 = divergence(E,X) == 0
Maxwell1(X, t) = X·E(X,t)=01,1
Maxwell2 = curl(E,X) == -diff(B,t)
Maxwell2(X, t) = 

X×E(X,t)=-t B(X,t)

Maxwell3 = divergence(B,X) == 0
Maxwell3(X, t) = X·B(X,t)=01,1
Maxwell4 = curl(B,X) == mu_0*epsilon_0*diff(E,t)
Maxwell4(X, t) = 

X×B(X,t)=ε0μ0t E(X,t)

Then, find the wave equation for the electric field. Compute the curl of the second Maxwell equation.

wave_E = curl(Maxwell2,X)
wave_E(X, t) = 

X X·E(X,t)-ΔX E(X,t)=-X×t B(X,t)

Substitute the first Maxwell equation in the electric field wave equation. Use lhs and rhs to obtain the left and right sides of the first Maxwell equation.

wave_E = subs(wave_E,lhs(Maxwell1),rhs(Maxwell1))
wave_E(X, t) = 

-ΔX E(X,t)=-X×t B(X,t)

Compute the time derivative of the fourth Maxwell equation.

dMaxwell4 = diff(Maxwell4,t)
dMaxwell4(X, t) = 

X×t B(X,t)=ε0μ0t t E(X,t)

Substitute the term that involves the magnetic field X×tB(X,t) in wave_E with the right side of dMaxwell4. Use lhs and rhs to obtain these terms from dMaxwell4.

wave_E = subs(wave_E,lhs(dMaxwell4),rhs(dMaxwell4))
wave_E(X, t) = 

-ΔX E(X,t)=-ε0μ0t t E(X,t)

Using similar steps, you can also find the wave equation for the magnetic field.

wave_B = curl(Maxwell4,X)
wave_B(X, t) = 

X X·B(X,t)-ΔX B(X,t)=ε0μ0X×t E(X,t)

wave_B = subs(wave_B,lhs(Maxwell3),rhs(Maxwell3))
wave_B(X, t) = 

-ΔX B(X,t)=ε0μ0X×t E(X,t)

dMaxwell2 = diff(Maxwell2,t)
dMaxwell2(X, t) = 

X×t E(X,t)=-t t B(X,t)

wave_B = subs(wave_B,lhs(dMaxwell2),rhs(dMaxwell2))
wave_B(X, t) = 

-ΔX B(X,t)=-ε0μ0t t B(X,t)

Input Arguments

collapse all

Symbolic vector field, specified as a vector of symbolic scalar variables, symbolic function, symbolic matrix variable, or symbolic matrix function. V must have the same length as X.

  • If V is a function of symbolic scalar variables, where V is of type sym or symfun, then the vector X must be of type sym or symfun.

  • If V is a function of symbolic matrix variables, where V is of type symmatrix or symfunmatrix, then the vector X must be of type symmatrix or symfunmatrix.

Data Types: sym | symfun | symmatrix | symfunmatrix

Vector with respect to which you find the divergence, specified as a vector of symbolic scalar variables, symbolic function, symbolic matrix variable, or symbolic matrix function. X must have the same length as V.

  • If you do not specify X and V is a function of symbolic scalar variables, then, by default, divergence constructs vector X from the symbolic scalar variables in V with the order of variables as defined by symvar(V).

  • If X is a symbolic matrix variable of type symmatrix, then X must have a size of 1-by-N or N-by-1.

  • If V and X are scalars, then divergence(V,X) = diff(V,X).

Data Types: sym | symfun | symmatrix | symfunmatrix

Limitations

  • The divergence function does not support tensor derivatives. If the input V is a tensor field or a matrix rather than a vector, then the divergence function returns an error.

  • Symbolic Math Toolbox™ currently does not support the dot or cross functions for symbolic matrix variables and functions of type symmatrix and symfunmatrix. If vector calculus identities involve dot or cross products, then the toolbox displays those identities in terms of other supported functions instead. To see a list of all the functions that support symbolic matrix variables and functions, use the commands methods symmatrix and methods symfunmatrix.

More About

collapse all

Divergence of Symbolic Vector Field

The divergence of the symbolic vector field V = (V1,...,Vn) with respect to the vector X = (X1,...,Xn) in Cartesian coordinates is the sum of partial derivatives of V with respect to X1,...,Xn.

div(V)=XV=i=1nViXi

Version History

Introduced in R2012a

expand all