1
$\begingroup$

I find numerically a scalar field that satisfy Poisson equation. From its gradient I derive a vector field.

scalarField = 
 NDSolveValue[{D[u[x, y], x, x] + D[u[x, y], y, y] == -Exp[-y - x], 
   u[x, 0] == u[x, 1] == u[-1, y] == u[1, y] == 0}, 
  u, {x, -1, 1}, {y, 0, 1}]
vectorField = {-D[scalarField[x, y], x], -D[scalarField[x, y], y]}

I want to plot the divergence of the vector field along a line. For example: the one in the figure generate by the following code (x == -0.3; 0 <= y <= 1).

Show[StreamPlot[vectorField, {x, -1, 1}, {y, 0, 1}, AspectRatio -> 1, 
  FrameLabel -> {"x", "y"}], 
 Graphics[{Thick, Line[{{-0.3, 0}, {-0.3, 1}}]}]]

output

fieldDivergence = Div[vectorField, {x, y}]

I can make a 3D plot of it with

Plot3D[fieldDivergence, {x, -1, 1}, {y, 0, 1} , Mesh -> All, 
 AxesLabel -> {"x", "y", "Div"}]

But I don't know how to proceed to do a 2D plot. I tried

Plot[fieldDivergence[-0.3, y], {y, 0, 1}]

Which gives an empty plot. From looking here I suspect it has to do with the fieldDivergence entity not returning a number.

I don't know what approach to try. How do I plot the divergence of a vector field along a parametric line?

$\endgroup$
  • $\begingroup$ Is this perhaps what you are looking for? Plot[fieldDivergence /. x -> -0.3, {y, 0, 1}] $\endgroup$ – MarcoB Jun 8 '17 at 20:52
1
$\begingroup$
Plot[fieldDivergence /. {x -> -.3, y -> yy}, {yy, 0, 1}]

enter image description here

$\endgroup$
1
$\begingroup$

For a vector to be shown you need to plot an area. Thus simply restrict your plot to the narrow area enclosing your chosen line:

Show[StreamPlot[vectorField, 
    {x, -.31, -.29}, 
    {y, 0, 1}, 
  AspectRatio -> 10,  
  FrameLabel -> {"x", "y"}], 
  Graphics[{Thick, Line[{{-0.3, 0}, {-0.3, 1}}]}]]
$\endgroup$
  • $\begingroup$ I am not trying to plot the vector, but its divergence in the points of a given line/curve. $\endgroup$ – Pedro H. N. Vieira Jun 9 '17 at 17:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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