Divergence of a vector field in MATLAB

Click For Summary

Discussion Overview

The discussion revolves around calculating the divergence of a vector field in MATLAB, particularly focusing on scenarios where the velocity field is known at discrete points within a volume. Participants explore methods for numerical approximation of divergence using finite differences and interpolation techniques, as well as the challenges associated with limited data points.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Debate/contested

Main Points Raised

  • One participant asks how to compute the divergence of a velocity field given at only 10 points within a volume, questioning the sufficiency of this data.
  • Several participants emphasize the need for a more comprehensive velocity field, suggesting that knowing values at only 10 points may not be adequate for calculating divergence.
  • Another participant proposes a method for calculating divergence using finite differences, detailing how to approximate derivatives at grid points.
  • There is a question about the reasoning behind using 2Δ in the denominator for the finite difference approximation of derivatives.
  • One participant expresses confusion about the implementation of divergence calculation in MATLAB, seeking clarification on specific coding issues.
  • Another participant mentions the difficulty of calculating numerical divergence when the vector components are random, suggesting that a functional form of the vector field would be more suitable for analysis.
  • Some participants discuss the possibility of using interpolation methods to estimate divergence from known data points.

Areas of Agreement / Disagreement

Participants generally agree that knowing the velocity field at a limited number of points poses challenges for accurately calculating divergence. However, there is no consensus on the best approach to take, with multiple methods and viewpoints being presented.

Contextual Notes

Limitations include the assumption that the velocity field can be adequately modeled or interpolated from a limited number of data points, and the unresolved nature of how to handle random vector components in divergence calculations.

Apashanka
Messages
427
Reaction score
15
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:
Physics news on Phys.org
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.
 
phyzguy said:
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...
 
Apashanka said:
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?
 
  • Like
Likes   Reactions: Apashanka
phyzguy said:
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??
 
Apashanka said:
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?
 
  • Like
Likes   Reactions: Apashanka
phyzguy said:
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
 
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##
 
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
IMG_20190620_115410_HDR.jpg

The correct ans. will be
IMG_20190620_120042_HDR.jpg
 
Last edited by a moderator:
  • #11
Can anyone please help...
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
IMG_20190620_193644_HDR.jpg

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...
 
  • #12
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 ??
 
  • #13
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:
https://in.mathworks.com/matlabcent...e-of-a-vector-field-numerically#answer_286287

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.
 
  • Like
Likes   Reactions: Apashanka and scottdave
  • #14
Either you calculate the derivatives using finite differences, or you interpolate between the known points by, for example, <br /> \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.
 
  • #15
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??
 
  • #16
Apashanka said:
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??
Will anyone please help... really I am in trouble
 
  • #17
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.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K