22
$\begingroup$

I am trying to plot the electric potential of a dielectric cylinder along with its field. The potential is a piecewise function:

F[r_, f_] := 
 Piecewise[{{-2 e r Cos[f]/(1 + er), 
0 <= r < a}, {-e r Cos[f] + (er - 1)*a^2 *e*r^(-1)*Cos[f], 
r >= a}}](*electorstatic pottential*)
e = 500;(*outer electric field*)
a = 0.02;(*cylinder's radius*)
er = 2;(*relevant acceptance*)

What's the best way to plot this function (potential), with its derivative (electric field)?

I tried to plot it using ContourPlot, but it doesn't look nice (I am not a Mathematica expert apparently)

ContourPlot[-2 e r Cos[f]/(1 + er), {r, 0, a}, {f, 0, 2 Pi}]
ContourPlot[-e r Cos[f] + (er - 1)*a^2 *e*r^(-1)*Cos[f], {r, a, 
2 a}, {f, 0, 2 Pi}]
Show[%, %%]

What I am trying to achieve is a sophisticated plot, with the cylinder at the origin, where the equipotential lines and the electric field lines will be plotted. I found something like that on the net (specifically the page "Gradient field plots in Mathematica"), but I don't know how to modify it...

gradientFieldPlot[f_, rx_, ry_, opts : OptionsPattern[]] := 
 Module[{img, cont, densityOptions, contourOptions, frameOptions, 
   gradField, field, plotRangeRule, rangeCoords}, 
  densityOptions = 
   Join[FilterRules[{opts}, 
     FilterRules[Options[DensityPlot], 
      Except[{Prolog, Epilog, FrameTicks, PlotLabel, ImagePadding, 
        GridLines, Mesh, AspectRatio, PlotRangePadding, Frame, 
        Axes}]]], {PlotRangePadding -> None, Frame -> None, 
     Axes -> None, AspectRatio -> Automatic}];
  contourOptions = 
   Join[FilterRules[{opts}, 
     FilterRules[Options[ContourPlot], 
      Except[{Prolog, Epilog, FrameTicks, PlotLabel, Background, 
        ContourShading, PlotRangePadding, Frame, Axes, 
        ExclusionsStyle}]]], {PlotRangePadding -> None, Frame -> None,
      Axes -> None, ContourShading -> False}];
  gradField = ComplexExpand[{D[f, rx[[1]]], D[f, ry[[1]]]}];
  field = 
   DensityPlot[Norm[gradField], rx, ry, 
    Evaluate@Apply[Sequence, densityOptions]];
  img = Rasterize[field, "Image"];
  plotRangeRule = FilterRules[Quiet@AbsoluteOptions[field], PlotRange];
  cont = If[
    MemberQ[{0, 
      None}, (Contours /. FilterRules[{opts}, Contours])], {}, 
    ContourPlot[f, rx, ry, Evaluate@Apply[Sequence, contourOptions]]];
  frameOptions = 
   Join[FilterRules[{opts}, 
     FilterRules[Options[Graphics], 
      Except[{PlotRangeClipping, PlotRange}]]], {plotRangeRule, 
     Frame -> True, PlotRangeClipping -> True}];
  rangeCoords = Transpose[PlotRange /. plotRangeRule];
  Apply[Show[
     Graphics[{Inset[
        Show[SetAlphaChannel[img, 
          "ShadingOpacity" /. {opts} /. {"ShadingOpacity" -> 1}], 
         AspectRatio -> Full], rangeCoords[[1]], {0, 0}, 
        rangeCoords[[2]] - rangeCoords[[1]]]}], cont, 
     StreamPlot[gradField, rx, ry, 
      Evaluate@FilterRules[{opts}, StreamStyle], 
      Evaluate@FilterRules[{opts}, StreamColorFunction], 
      Evaluate@FilterRules[{opts}, StreamColorFunctionScaling], 
      Evaluate@FilterRules[{opts}, StreamPoints], 
      Evaluate@FilterRules[{opts}, StreamScale]], ##] &, 
   frameOptions]]

This can be run like that

gradientFieldPlot[(y^2 + (x - 2)^2)^(-1/
    2) - (y^2 + (x - 1/2)^2)^(-1/2)/2, {x, -1.5, 2.5}, {y, -1.5, 
  1.5}, PlotPoints -> 50, ColorFunction -> "BlueGreenYellow", 
 Contours -> 10, ContourStyle -> White, Frame -> True, 
 FrameLabel -> {"x", "y"}, ClippingStyle -> Automatic, Axes -> True, 
 StreamStyle -> Orange]

And the amazing output is

What I would like to do is

$\endgroup$
6
  • 8
    $\begingroup$ Yes, you got that from my web site. I'll be back in a few minutes... $\endgroup$
    – Jens
    Commented Jan 5, 2013 at 19:05
  • $\begingroup$ you could start from ContourPlot[-2 e r Cos[f]/(1 + er), {r, 0, a}, {f, 0, 2 Pi}, ContourShading -> False, ContourStyle -> Red]; ContourPlot[-e r Cos[f] + (er - 1)*a^2*e*r^(-1)*Cos[f], {r, 0, a}, {f, 0, 2 Pi}, ContourShading -> False, ContourStyle -> Blue]; Show[{%, %%}] and get (i.sstatic.net/Lp1Zo.png) $\endgroup$
    – chris
    Commented Jan 5, 2013 at 19:06
  • $\begingroup$ @Jens: Yes I did!!! Great work!!! $\endgroup$
    – Thanos
    Commented Jan 5, 2013 at 19:12
  • $\begingroup$ @chris: Actually I am trying to do something like this(please check my edited question). $\endgroup$
    – Thanos
    Commented Jan 5, 2013 at 19:21
  • 4
    $\begingroup$ If you quote third-party code in your posts (especially when it does not originate on a StackExchange site), please remember to provide a full citation. In addition to being required under the StackExchange terms and conditions, this is because you grant readers a copyright licence to anything you post. If you lack the right to offer such a licence but don't clearly indicate this fact, it could lead to a copyright dispute. $\endgroup$ Commented Jan 5, 2013 at 21:03

1 Answer 1

26
$\begingroup$

I'll just address the technical plotting aspect of the question.

The problem is that your cylinder potential is intended to be a function of polar coordinates r and f, but to plot it in Cartesian space you have to convert to x and y first. This is done as follows:

pot[r_, f_] := 
 Piecewise[{{-2 e r Cos[f]/(1 + er), 
    0 <= r < a}, {-e r Cos[f] + (er - 1)*a^2*e*r^(-1)*Cos[f], 
    r >= a}}](*electorstatic pottential*)
e = 500;(*outer electric \
field*)a = 0.02;(*cylinder's radius*)er = 2;

gradientFieldPlot[
 pot[Sqrt[x^2 + y^2], ArcTan[x, y]], {x, -.2, .2}, {y, -.2, .2}, 
 PlotPoints -> 50, ColorFunction -> "BlueGreenYellow", Contours -> 10,
  ContourStyle -> White, Frame -> True, FrameLabel -> {"x", "y"}, 
 ClippingStyle -> Automatic, Axes -> True, StreamStyle -> Orange]

plot

The colors represent the field strength, and the orange lines are the field lines.

I called the potential pot instead of F.

Since your cylinder is so small, we should zoom in even more:

gradientFieldPlot[
 pot[Sqrt[x^2 + y^2], ArcTan[x, y]], {x, -.05, .05}, {y, -.05, .05}, 
 PlotPoints -> 50, ColorFunction -> "BlueGreenYellow", Contours -> 10,
  ContourStyle -> White, Frame -> True, FrameLabel -> {"x", "y"}, 
 ClippingStyle -> Automatic, Axes -> True, StreamStyle -> Orange]

plot2

So everything looks as expected for a dielectric cylinder in the homogeneous external field of a capacitor. The equipotential lines (white) are completely flat inside the cylinder, corresponding to a uniform field. The highest field strength is at the "poles" of the dielectric cylinder facing the (far-away) capacitor plates.

$\endgroup$
7
  • $\begingroup$ @Jens: You are awsome proffessor! That was exactly what I was trying to do! Thank you very much for this very pretty plot! WOW!!! $\endgroup$
    – Thanos
    Commented Jan 5, 2013 at 20:12
  • $\begingroup$ @Jens: I was trying to add a legend using PlotLegend -> {"pottential", "field"} inside gradientfieldplot's argument, but nothing showed up(I have already included Needs["PlotLegends"]`). $\endgroup$
    – Thanos
    Commented Jan 5, 2013 at 20:36
  • 1
    $\begingroup$ If you don't have version 9 yet, then Mathematica's PlotLegends package should be avoided like the plague. Instead, in this case perhaps the easiest is to add the PlotLabel option to gradientFieldPlot. E.g., like this: PlotLabel->Framed[Style["Potential (white) and field (orange and background)",Larger,GrayLevel[.3],FontFamily->"Helvetica"],FrameStyle->None,Background->Lighter[Orange],RoundingRadius->6]] $\endgroup$
    – Jens
    Commented Jan 5, 2013 at 22:13
  • $\begingroup$ @Jens: I ended there, before I see your comment :(... You are right about Legend-it has issues on v8.0.0. Is there a way to plot a legend illustrating the color scale? Or perhaphs is there a way to plot the whole solution in 3D? $\endgroup$
    – Thanos
    Commented Jan 6, 2013 at 7:51
  • 1
    $\begingroup$ That was indeed the problem, and I eventually figured it out. I very much appreciate you coming back and taking the time to help, and thank you very much for your amazing implementation. I made some really neat plots, and to be honest it was my favorite thing to procrastinate my work with for the better part of the time since then. Thank you! $\endgroup$
    – Mike H
    Commented Nov 1, 2021 at 14:20

Your Answer

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

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