Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I am trying to plot an electric pottentional of a dielectric cylinder along with it's field. The pottentional 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(pottential), with it's 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 achive is a sophisticated plot,with the cylinder at the origin, where the equipottential 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

share|improve this question
4  
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
3  
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

1 Answer 1

up vote 15 down vote accepted

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.

share|improve this answer
    
+1 for very pretty plot! –  chris Jan 5 '13 at 19:53
    
@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
1  
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,Back‌​ground->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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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