MATLAB Divergence of a vector field in MATLAB

  • Thread starter Apashanka
  • Start date
387
13
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:

phyzguy

Science Advisor
4,207
1,192
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.
 
387
13
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

Science Advisor
4,207
1,192
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:
[tex] \frac{\partial vx}{\partial x} = \frac{vx_{i+1} - vx_{i-1}}{2 \Delta}[/tex]
So the divergence at the point (xi,yi,zi) is just
[tex] \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}[/tex]

Does this help?
 
387
13
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:
[tex] \frac{\partial vx}{\partial x} = \frac{vx_{i+1} - vx_{i-1}}{2 \Delta}[/tex]
So the divergence at the point (xi,yi,zi) is just
[tex] \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}[/tex]

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

phyzguy

Science Advisor
4,207
1,192
Why ##2\Delta## in the denominator??
The derivative [itex]\frac {\partial vx}{\partial x}[/itex] is approximated by [itex]\frac{\Delta vx}{\Delta x}[/itex]. If you calculate [itex] \frac{vx_{i+1} - vx_i}{\Delta}[/itex], this gives you the derivative [itex]\frac{\partial v_x}{\partial x}[/itex] 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?
 
387
13
The derivative [itex]\frac {\partial vx}{\partial x}[/itex] is approximated by [itex]\frac{\Delta vx}{\Delta x}[/itex]. If you calculate [itex] \frac{vx_{i+1} - vx_i}{\Delta}[/itex], this gives you the derivative [itex]\frac{\partial v_x}{\partial x}[/itex] 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
 
387
13
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##
 
387
13
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:
10,689
4,248
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.
 
387
13
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.....
 
387
13
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
1,063
935
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/matlabcentral/answers/361831-computing-divergence-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.
 

pasmith

Homework Helper
1,733
408
Either you calculate the derivatives using finite differences, or you interpolate between the known points by, for example, [tex]
\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)[/tex] where [itex]T_k[/itex] is the Chebyshev polynomial of order [itex]k[/itex], and calculate the divergence of that analytically.
 
387
13
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??
 
387
13
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
 

phyzguy

Science Advisor
4,207
1,192
Is the field you see trying to calculate the divergence of [itex]x^2 \hat i[/itex]? If so, you don't need Matlab to calculate the divergence. It is simple to do analytically.
 

Want to reply to this thread?

"Divergence of a vector field in MATLAB" You must log in or register to reply here.

Related Threads for: Divergence of a vector field in MATLAB

Replies
2
Views
2K
Replies
1
Views
3K

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
Top