Divergence of a vector field in MATLAB

In summary, if within a volume there exists 10 velocity fields at different points, then it is possible to find ##\nabla•v## using matlab.
  • #1
Apashanka
429
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
  • #2
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.
 
  • #3
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...
 
  • #4
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:
[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?
 
  • Like
Likes Apashanka
  • #5
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:
[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??
 
  • #6
Apashanka said:
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?
 
  • Like
Likes Apashanka
  • #7
phyzguy said:
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
 
  • #8
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##
 
  • #9
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 Apashanka and scottdave
  • #14
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.
 
  • #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 [itex]x^2 \hat i[/itex]? If so, you don't need Matlab to calculate the divergence. It is simple to do analytically.
 

1. What is the divergence of a vector field in MATLAB?

The divergence of a vector field in MATLAB is a measure of the extent to which the vector field flows outward from a given point. It is a scalar quantity that represents the amount of "flow" or "flux" emanating from a point in the field.

2. How is the divergence of a vector field calculated in MATLAB?

In MATLAB, the divergence of a vector field can be calculated using the "divergence" function. This function takes in the x, y, and z components of the vector field as inputs and returns a scalar value representing the divergence at each point in the field.

3. What is the significance of the divergence of a vector field in MATLAB?

The divergence of a vector field in MATLAB is important because it can help us understand the behavior of a vector field at a given point. A positive divergence indicates that the field is expanding outward, while a negative divergence indicates that the field is contracting inward. A divergence of zero indicates that the field is neither expanding nor contracting.

4. Can the divergence of a vector field in MATLAB be visualized?

Yes, the divergence of a vector field in MATLAB can be visualized using the "quiver" function. This function creates a plot of the vector field with arrows representing the direction and magnitude of the vectors, and the color of the arrows representing the magnitude of the divergence at each point.

5. How can the divergence of a vector field in MATLAB be used in practical applications?

The divergence of a vector field in MATLAB can be used in a variety of practical applications, such as fluid dynamics, electromagnetics, and image processing. It can help in analyzing the flow of fluids, calculating electric and magnetic fields, and identifying features in images. It is also used in simulations and modeling to study the behavior of complicated systems.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
802
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
Replies
8
Views
531
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
Replies
4
Views
241
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • Calculus and Beyond Homework Help
Replies
9
Views
752
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
Back
Top