Computing Divergence of a vector field numerically

3 views (last 30 days)
I am trying to compute the divergence at individual points of a vector field numerically. I have had issues with Matlab returning nonzero arrays when it should return all zero's. For example:
[x y] = meshgrid(0:pi/2:2*pi,0:pi/2:2*pi); u = sin(x); v = -y*cos(x); divergence(x,y,u,v)
The divergence of this field evaluates symbolically to 0, so why am I not getting this result numerically?
Thank you

Answers (1)

John D'Errico
John D'Errico on 17 Oct 2017
Edited: John Kelly on 24 Aug 2018
First, divergence computes a NUMERICAL approximation to the gradients necessary. It has no clue as to the real derivatives, or the real functions that created these variables. With such a coarse grid, you would never expect a true zero result, even if 0 is the correct result in symbolic terms.
Having said that, you need to understand the * and .* operators in MATLAB.
  • is used for MATRIX multiplication.
.* is used for element-wise multiplication.
There IS a difference.
So, if we use a rather finer grid, and use the proper operator where necessary, you might see something more reasonable.
[x y] = meshgrid(linspace(0,2*pi,100)); u = sin(x); v = -y.*cos(x); divergence(x,y,u,v)
.
doc mtimes
doc times
.

Categories

Find more on Mathematics in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!