4
$\begingroup$

I am solving Laplace equation on circular domain from which i get solution for potential.
The code I used:

w = ImplicitRegion[x^2 + y^2 <= 4, {{x, -2, 2}, {y, -2, 2}}]
op = -Laplacian[u[x, y], {x, y}]
h = {DirichletCondition[u[x, y] == 50, x^2 + y^2 == 4 && x > 1.99], 
   DirichletCondition[u[x, y] == -50, x^2 + y^2 == 4 && x < -1.99]};
uif = NDSolveValue[{op == 
    NeumannValue[0, x^2 + y^2 == 4 && -1.9 < x < 1.9], h}, 
  u, {x, y} \[Element] w]
ContourPlot[uif[x, y], {x, y} \[Element] w, 
 ColorFunction -> "Temperature", AspectRatio -> Automatic, 
 Contours -> 30]

Solution

Now i would like to calculate current density (gradient of potential) from solution for potential and plot it as vector field on the same graph. Can this be done with some built in function or how would i go about this?

$\endgroup$
  • $\begingroup$ Your graphic doesn't correspond to the code. $\endgroup$ – andre314 Jul 30 '18 at 11:36
  • $\begingroup$ @andre Thanks. I fixed it now. $\endgroup$ – Polihistor Jul 30 '18 at 11:41
5
$\begingroup$

You can use the function StreamPlot and combine plots with Show.

contour = ContourPlot[uif[x, y], {x, y} ∈ w, ColorFunction -> "Temperature", Contours -> 20];
stream = StreamPlot[ Evaluate@Grad[uif[x, y], {x, y}], {x, y} ∈ w];

Show[contour, stream]

streamPlot

$\endgroup$
  • $\begingroup$ That' s what I wanted. Thanks. $\endgroup$ – Polihistor Jul 30 '18 at 11:38
2
$\begingroup$

How about:

VectorPlot[Evaluate[Grad[uif[x, y], {x, y}]], 
 Element[{x, y}, uif["ElementMesh"]]]

enter image description here

$\endgroup$
2
$\begingroup$

StreamDensityPlot does all what you need :

field=Grad[uif[x, y],{x,y}];
StreamDensityPlot[field, {x, y} \[Element] w,
  Mesh->{{{20,Directive[Thick,Dashed]},18,16,14,12,{10,Thick}}},
  MeshStyle->Black,
  StreamStyle-> Black,
  ColorFunctionScaling->False,
  ColorFunction-> (Hue[#5/100.]&)
  ]  

enter image description here

I have interpreted the "current density" in your question as the module of the gradient of the potential (= the module of the field, that is to say a scalar, not a vector). That's the reason why I have added the iso-current-density curves (the black curves) in the graphic.

EDIT

A version with a legend :

field=Grad[uif[x, y],{x,y}];
StreamDensityPlot[field, {x, y} \[Element] w,
  Mesh->{{{20,Directive[Thick,Dashed]},18,16,14,12,{10,Thick}}},
  MeshStyle->Black,
  PlotLegends->Automatic,
  StreamStyle-> Black,
  RegionFunction->Function[{x,y,vx,vy,n},n<70],
  ColorFunction-> Hue
  ]   

enter image description here

$\endgroup$

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.