0
$\begingroup$

Having information of a vector field in some discrete points in 2D space(points and vectors are both in 2D), how can one plots (approximate) divergence of the vector-field in the total space(not in discrete points)? Is there any function in Mathematica which take information of vector field in some discrete points and do that? Any example provided answer is highly appreciated.

I have found this useful link through searching which is relevant: Discrete vector_field

Trying first answer:

field[{x_, y_}] := {Sin[x y], Cos[x + y]};
        randomPoints = RandomReal[{-1, 1}, {100, 2}];
        discreteField = randomPoints /. p_List?(Length[#] == 2 &) :> {p, field[p]};

        Graphics[discreteField /. {pt_List, v_List} :> Arrow[{pt, pt + 0.2 v}]]
VxInt = Interpolation[
   discreteField /. {pt_List, {vx_, vy_}} :> {pt, vx}, 
   InterpolationOrder -> All];
VyInt = Interpolation[
   discreteField /. {pt_List, {vx_, vy_}} :> {pt, vy}, 
   InterpolationOrder -> All];

VectorPlot[{VxInt[x, y], VyInt[x, y]}, {x, -1, 2}, {y, -1, 2}]

curl[x_, y_] = -Derivative[0, 1][VxInt][x, y] + 
  Derivative[1, 0][VyInt][x, y]

DensityPlot[
 curl[x, y] - Curl[field[{x, y}], {x, y}] // Evaluate, {x, -1, 
  2}, {y, -1, 2}, PlotLegends -> Automatic, PlotRange -> All]

As it is clear from the first and the second picture, there is a rotation in point (1.5 ,0). But the last output doesn't show a high value of the curl at this point.

$\endgroup$
1
  • $\begingroup$ It's unwise to calculate curl at point (1.5, 0) when interpolation was done on points in region [−1,1]×[−1,1]. InterpolationFunction is not ExtrapolationFunction. $\endgroup$ Dec 4, 2017 at 10:20

1 Answer 1

3
$\begingroup$

You probably want an interpolation on the (un)structured grid.

Suppose you have a vector field in discrete points:

field[{x_, y_}] := {Sin[x y], Cos[x + y]};
        randomPoints = RandomReal[{-1, 1}, {100, 2}];
        discreteField = randomPoints /. p_List?(Length[#] == 2 &) :> {p, field[p]};

        Graphics[discreteField /. {pt_List, v_List} :> Arrow[{pt, pt + 0.2 v}]]

enter image description here

You interpolate each component separately:

VxInt = Interpolation[discreteField /. {pt_List, {vx_, vy_}} :> {pt, vx}, 
            InterpolationOrder -> All];
        VyInt = Interpolation[discreteField /. {pt_List, {vx_, vy_}} :> {pt, vy}, 
            InterpolationOrder -> All];

        VectorPlot[{VxInt[x, y], VyInt[x, y]}, {x, -1, 1}, {y, -1, 1}]

enter image description here

Then you can treat it like a continuous function. For example, you can calculate the divergence and compare to the divergence of the initial field.

DensityPlot[
            Div[{VxInt[x, y], VyInt[x, y]}, {x, y}] - Div[field[{x, y}], {x, y}]
            // Evaluate, {x, -1, 1}, {y, -1, 1}, PlotLegends -> Automatic]

enter image description here

$\endgroup$
4
  • $\begingroup$ What are white wholes in final graph? $\endgroup$
    – sara nj
    Dec 3, 2017 at 20:03
  • $\begingroup$ When I change your final code to the below, It plots nothing!: DensityPlot[ Curl[{VxInt[x, y], VyInt[x, y]}, {x, y}] - Curl[field[{x, y}], {x, y}] // Evaluate, {x, -1, 1}, {y, -1, 1}, PlotLegends -> Automatic] $\endgroup$
    – sara nj
    Dec 3, 2017 at 20:10
  • 1
    $\begingroup$ White regions show where values are out of range, try PlotRange->All to see all values. Unfortunately, Curl works strangely with InterpolatingFunction. Try curl[x_, y_] = -Derivative[0,1][VxInt][x,y]+Derivative[1,0][VyInt][x,y] and then DensityPlot[curl[x,y]-Curl[field[{x,y}], {x, y}]//Evaluate,{x,-1,1},{y,-1,1}] $\endgroup$ Dec 4, 2017 at 1:31
  • $\begingroup$ I think there is a problem in the curl. Please see my edit to the question $\endgroup$
    – sara nj
    Dec 4, 2017 at 6:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.