# MATLAB Divergence of a vector field in MATLAB

#### Apashanka

If within a volume v ,there exists 10 velocity fields at different points then can anyone please suggest how to compute $\int_v(\nabla•v)$ within the volume?? using matlab
For exm if the velocity vector field be $v=x\hat x+y\hat y+z\hat z$ and for x=1 to 10,y=1 to 10 and z= 1 to 10 the 10 values of v is given ,is it possible to find $\nabla•v$?

Last edited:
Related MATLAB, Maple, Mathematica, LaTeX, Etc News on Phys.org

#### phyzguy

It's not clear from your question exactly what you are trying to do. You need to know the velocity field everywhere in the volume, at least to some level of sampling. If the data is gridded, for example with steps of 1, then there are 1000 grid points in the volume, not 10. Are you saying that you only know the velocity vector at 10 points within the volume? If so, this is probably not enough information to calculate the divergence. Alternatively, you could have a model of the velocity field in the volume, meaning a function v(x,y,z) where you can calculate the velocity at any point. Perhaps the 10 points are enough to build a model that you can interpolate to the other locations. Please give more detail on what information you have.

#### Apashanka

It's not clear from your question exactly what you are trying to do. You need to know the velocity field everywhere in the volume, at least to some level of sampling. If the data is gridded, for example with steps of 1, then there are 1000 grid points in the volume, not 10. Are you saying that you only know the velocity vector at 10 points within the volume? If so, this is probably not enough information to calculate the divergence. Alternatively, you could have a model of the velocity field in the volume, meaning a function v(x,y,z) where you can calculate the velocity at any point. Perhaps the 10 points are enough to build a model that you can interpolate to the other locations. Please give more detail on what information you have.
Actually I am just trying ,
Okay suppose if the velocity vector has components $v_x,v_y,v_z$ and their values are known at 100 points say then how to calculate the divergence of $\vec v$??could you please help....

#### phyzguy

Actually I am just trying ,
Okay suppose if the velocity vector has components $v_x,v_y,v_z$ and their values are known at 100 points say then how to calculate the divergence of $\vec v$??could you please help....
Suppose I know vx, vy, and vz at a series of points on a regular grid. Call the grid points xi, yi, and zi, where i runs from 1 to n. Call the spacing between the grid points Δ .Then I can calculate a derivative at a point (xi,yi,zi) by just discretizing the formula for the derivative, as follows:
$$\frac{\partial vx}{\partial x} = \frac{vx_{i+1} - vx_{i-1}}{2 \Delta}$$
So the divergence at the point (xi,yi,zi) is just
$$\frac{\partial vx}{\partial x} + \frac{\partial vy}{\partial y} + \frac{\partial vz}{\partial z} = \frac{vx_{i+1} - vx_{i-1}}{2 \Delta} + \frac{vy_{i+1} - vy_{i-1}}{2 \Delta} + \frac{vz_{i+1} - vz_{i-1}}{2 \Delta}$$

Does this help?

#### Apashanka

Suppose I know vx, vy, and vz at a series of points on a regular grid. Call the grid points xi, yi, and zi, where i runs from 1 to n. Call the spacing between the grid points Δ .Then I can calculate a derivative at a point (xi,yi,zi) by just discretizing the formula for the derivative, as follows:
$$\frac{\partial vx}{\partial x} = \frac{vx_{i+1} - vx_{i-1}}{2 \Delta}$$
So the divergence at the point (xi,yi,zi) is just
$$\frac{\partial vx}{\partial x} + \frac{\partial vy}{\partial y} + \frac{\partial vz}{\partial z} = \frac{vx_{i+1} - vx_{i-1}}{2 \Delta} + \frac{vy_{i+1} - vy_{i-1}}{2 \Delta} + \frac{vz_{i+1} - vz_{i-1}}{2 \Delta}$$

Does this help?
Why $2\Delta$ in the denominator??

#### phyzguy

Why $2\Delta$ in the denominator??
The derivative $\frac {\partial vx}{\partial x}$ is approximated by $\frac{\Delta vx}{\Delta x}$. If you calculate $\frac{vx_{i+1} - vx_i}{\Delta}$, this gives you the derivative $\frac{\partial v_x}{\partial x}$ at the point i+1/2. You want the derivative at the point i, so you take the difference between i+1 and i-1, but then you need to divide by Δx. Since the vx values are two grid points apart, you divide by 2Δ. Does this make sense?

#### Apashanka

The derivative $\frac {\partial vx}{\partial x}$ is approximated by $\frac{\Delta vx}{\Delta x}$. If you calculate $\frac{vx_{i+1} - vx_i}{\Delta}$, this gives you the derivative $\frac{\partial v_x}{\partial x}$ at the point i+1/2. You want the derivative at the point i, so you take the difference between i+1 and i-1, but then you need to divide by Δx. Since the vx values are two grid points apart, you divide by 2Δ. Does this make sense?
Yes got it

#### Apashanka

Okay for 3-D if the grid points are (111),(121),(211),(221),(112),(122),(212),(222) and the values of $v_x,v_y,v_z$ are known at these points ,then can anyone please help me in how to write program in MATLAB to compute divergence of $\vec v$ without using $meshgrid$

#### Apashanka

In 2-D if the grid coordinates are (1,1),(1,2),(2,1),(2,2) and $v_x,v_y$ at these points are (3,4),(8,9),(7,10),(1,5) then how to write correctly in MATLAB to calculate it's divergence??(without using $meshgrid$)
Mine is showing something like

The correct ans. will be

Last edited by a moderator:

#### jedishrfu

Mentor
Here's a matlab reference to check:

see if that helps on performing the call. Other than that try evaluating it manually to see if in fact matlab is computing it correctly.

In any event, if its matlab's fault then hey you found a bug.

#### Apashanka

For $(v_x,v_y)$ =(3,4),(7,9),(1,6),(8,5) at (1,1),(1,2),(2,1),(2,2) respectively the program I have is

Here the 3 and 0 are correct but 6 should be in place of -3 and vice versa ,where is the fault can anyone please help me.....

#### Apashanka

I am again posting ,seriously in trouble.....
I have taken a cube of size (1×1×1) and generated 10 random points(coordinates) within it and have defined a vector $\vec v=u\hat x+v\hat y+w\hat z$ for which at the 10 points $u,v,w$ are known the program is as follows.
Now my question is will it be possible to find $\nabla•v$ at these 10 points using MATLAB.....as a newcomer in MATLAB can anyone please help me ??

#### Wrichik Basu

Gold Member
2018 Award
I think this is a continuation of some previous threads that you have created regarding divergence.

If you look at the Matlab help page for divergence, you will find that all examples have been done with symbolic math.

In your case, you require numerical divergence, as you know the coordinates of your points. This is something difficult. I found an answer in Matlab Central:

But I don't think this is possible at all. Let's forget Matlab for a moment. What is the del (or nabla) operator? $$\nabla \ = \ \frac{\partial}{\partial x} \hat{i} \ + \ \frac{\partial}{\partial y} \hat{j} \ + \ \frac{\partial}{\partial z} \hat{k}.$$ When you dot this with a vector, you get the divergence.

In your case, all the components of the vectors are constants (as you know all the components). So doing a partial derivative on any of them will result in 0.

This problem is occurring because your vectors are random. Had they followed an equation, you could have taken the divergence of that equation and then evaluated the result at each point you wanted.

#### pasmith

Homework Helper
Either you calculate the derivatives using finite differences, or you interpolate between the known points by, for example, $$\mathbf{v}(x,y,z) = \sum_{k=0}^{N_x} \sum_{l=0}^{N_y} \sum_{m=0}^{N_z} \mathbf{a}_{klm} T_k(2x-1) T_l(2y-1) T_m(2z-1)$$ where $T_k$ is the Chebyshev polynomial of order $k$, and calculate the divergence of that analytically.

#### Apashanka

If in a 1×1×1 cube box the field $x^2$ is known at 10 points within the box....
Now if I divide the original cube into sub-volumes of size (.1×.1×.1) using those 10 values is it possible to know the value of the field at the corner of those subvolume.....
Or using these 10 values is it possible to know the divergence of the ($\nabla•x^2 \hat i$) at these corners of subvolume using MATLAB??

#### Apashanka

If in a 1×1×1 cube box the field $x^2$ is known at 10 points within the box....
Now if I divide the original cube into sub-volumes of size (.1×.1×.1) using those 10 values is it possible to know the value of the field at the corner of those subvolume.....
Or using these 10 values is it possible to know the divergence of the ($\nabla•x^2 \hat i$) at these corners of subvolume using MATLAB??

#### phyzguy

Is the field you see trying to calculate the divergence of $x^2 \hat i$? If so, you don't need Matlab to calculate the divergence. It is simple to do analytically.

"Divergence of a vector field in MATLAB"

### Physics Forums Values

We Value Quality
• Topics based on mainstream science
• Proper English grammar and spelling
We Value Civility
• Positive and compassionate attitudes
• Patience while debating
We Value Productivity
• Disciplined to remain on-topic
• Recognition of own weaknesses
• Solo and co-op problem solving