# Plot electric potential and field

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,
ExclusionsStyle}]]], {PlotRangePadding -> None, Frame -> None,
Axes -> None, ContourShading -> False}];
gradField = ComplexExpand[{D[f, rx[]], D[f, ry[]]}];
field =
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[], {0, 0},
rangeCoords[] - rangeCoords[]]}], cont,
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 • Yes, you got that from my web site. I'll be back in a few minutes... – Jens Jan 5 '13 at 19:05
• 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.stack.imgur.com/Lp1Zo.png) – chris Jan 5 '13 at 19:06
• @Jens: Yes I did!!! Great work!!! – Thanos Jan 5 '13 at 19:12
• @chris: Actually I am trying to do something like this(please check my edited question). – Thanos Jan 5 '13 at 19:21
• 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. – Oleksandr R. Jan 5 '13 at 21:03

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;

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] 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] 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.

• @Jens: You are awsome proffessor! That was exactly what I was trying to do! Thank you very much for this very pretty plot! WOW!!! – Thanos Jan 5 '13 at 20:12
• @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"]). – Thanos Jan 5 '13 at 20:36
• 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]]` – Jens Jan 5 '13 at 22:13
• @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? – Thanos Jan 6 '13 at 7:51