Main Content

divergence

Compute divergence of vector field

Description

example

div= divergence(X,Y,Z,Fx,Fy,Fz)computes thenumerical divergenceof a 3-D vector field with vector componentsFx,Fy, andFz.

The arraysX,Y, andZ, which define the coordinates for the vector componentsFx,Fy, andFz, must be monotonic, but do not need to be uniformly spaced.X,Y, andZmust be 3-D arrays of the same size, which can be produced bymeshgrid.

div= divergence(Fx,Fy,Fz)assumes a default grid of sample points. The default grid pointsX,Y, andZare determined by the expression[X,Y,Z] = meshgrid(1:n,1:m,1:p), where[m,n,p] = size(Fx). Use this syntax when you want to conserve memory and are not concerned about the absolute distances between points.

example

div= divergence(X,Y,Fx,Fy)computes thenumerical divergenceof a 2-D vector field with vector componentsFxandFy.

The matricesXandY, which define the coordinates forFxandFy, must be monotonic, but do not need to be uniformly spaced.XandYmust be 2-D matrices of the same size, which can be produced bymeshgrid.

div= divergence(Fx,Fy)assumes a default grid of sample points. The default grid pointsXandYare determined by the expression[X,Y] = meshgrid(1:n,1:m), where[m,n] = size(Fx). Use this syntax when you want to conserve memory and are not concerned about the absolute distances between points.

Examples

collapse all

Load a 3-D vector field data set that represents a wind flow. The data set contains arrays of size 35-by-41-by-15.

loadwind

Compute the numerical divergence of the vector field.

div = divergence(x,y,z,u,v,w);

Display the divergence of vector volume data as slice planes. Show the divergence at the y z -planes with x = 9 0 and x = 1 3 4 , at the x z -plane with y = 5 9 , and at the x y -plane with z = 0 . Use color to indicate divergence.

h = slice(x,y,z,div,[90 134],59,0); shadinginterpcolorbar daspect([1 1 1]); axistightcamlight set([h(1),h(2)],'ambientstrength',0.6);

Figure contains an axes object. The axes object contains 4 objects of type surface.

Specify 2-D coordinates and a vector field.

[x,y] = meshgrid(-8:2:8,-8:2:8); Fx = 200 - (x.^2 + y.^2); Fy = 200 - (x.^2 + y.^2);

Plot the vector field componentsFxandFy.

quiver(x,y,Fx,Fy)

Figure contains an axes object. The axes object contains an object of type quiver.

Find the numerical divergence of the 2-D vector field. Plot the contour of the divergence.

D = divergence(x,y,Fx,Fy); holdoncontour(x,y,D,'ShowText','on')

Figure contains an axes object. The axes object contains 2 objects of type quiver, contour.

Input Arguments

collapse all

Input coordinates, specified as matrices or 3-D arrays.

  • For 2-D vector fields,XandYmust be 2-D matrices of the same size, and that size can be no smaller than2-by-2.

  • For 3-D vector fields,X,Y, andZmust be 3-D arrays of the same size, and that size can be no smaller than2-by-2-by-2.

Data Types:single|double
Complex Number Support:Yes

Vector field components at the input coordinates, specified as matrices or 3-D arrays.Fx,Fy, andFzmust be the same size asX,Y, andZ.

Data Types:single|double
Complex Number Support:Yes

More About

collapse all

Numerical Divergence

Thenumerical divergenceof a vector field is a way to estimate the values of the divergence using the known values of the vector field at certain points.

For a 3-D vector field of three variables F ( x , y , z ) = F x ( x , y , z ) e ^ x + F y ( x , y , z ) e ^ y + F z ( x , y , z ) e ^ z , the definition of the divergence ofFis

div F = · F = F x x + F y y + F z z .

For a 2-D vector field of two variables F ( x , y ) = F x ( x , y ) e ^ x + F y ( x , y ) e ^ y , the divergence is

div F = · F = F x x + F y y .

Algorithms

divergencecomputes the partial derivatives in its definition by using finite differences. For interior data points, the partial derivatives are calculated usingcentral difference. For data points along the edges, the partial derivatives are calculated usingsingle-sided (forward) difference.

For example, consider a 2-D vector fieldFthat is represented by the matricesFxandFyat locationsXandYwith sizem-by-n. The locations are 2-D grids created by[X,Y] = meshgrid(x,y), wherexis a vector of lengthnandyis a vector of lengthm.divergencethen computes the partial derivativesFx/ ∂xandFy/ ∂yas

  • dFx(:,i) = (Fx(:,i+1) - Fx(:,i-1))/(x(i+1) - x(i-1))and

    dFy(j,:) = (Fy(j+1,:) - Fy(j-1,:))/(y(j+1) - y(j-1))

    for interior data points.

  • dFx(:,1) = (Fx(:,2) - Fx(:,1))/(x(2) - x(1))and

    dFx(:,n) = (Fx(:,n) - Fx(:,n-1))/(x(n) - x(n-1))

    for data points at the left and right edges.

  • dFy(1,:) = (Fy(2,:) - Fy(1,:))/(y(2) - y(1))and

    dFy(m,:) = (Fy(m,:) - Fy(m-1,:))/(y(m) - y(m-1))

    for data points at the top and bottom edges.

The numerical divergence of the vector field is equal todiv = dFx + dFy.

Extended Capabilities

Version History

Introduced before R2006a