7
$\begingroup$

I'm trying to render an equation in the cylindrical coordinates, and I used the following codes:

With[{λ = 1, f = 50, R = 25}, ContourPlot[ -Mod[(Pi/λ)*(Sqrt[ρ^2 + f^2] - f), Pi] /. {ρ -> Norm[{x, y}], ϕ -> ArcTan[x, y]}, {x, -R, R}, {y, -R, R}, Contours -> 20, ContourLines -> False, RegionFunction -> (Sqrt[#1^2 + #2^2] < R &), ColorFunction -> "Rainbow", Frame -> False]] 

However, I got a pattern which seems not smooth because points were sampled equally in the Cartesian coordinates. How can I get a smooth contour plot? Furthermore, can I plot such a pattern in the 3D coordinates? Thanks a lot.

Here is an example of the output that I would like:

enter image description here

$\endgroup$
3
  • $\begingroup$ I added your graphic as an example for you, as I know the site does not let you do that yet. $\endgroup$ Commented Mar 8, 2013 at 1:06
  • $\begingroup$ Tony, if my post fully answers your question please consider Accepting it. If not, please tell me in what way it is lacking. $\endgroup$ Commented Mar 9, 2013 at 0:34
  • $\begingroup$ @ Mr. Wizard, I'll do that. You were good but the 3d plot was not exactly what I want. Probably I didn't describe it clearly. I intend to plot the same pattern in 3D coordinates, say, the target-sheet-shaped preservers. It seems to shear and/or rotate 2d graphics with certain viewpoint. Thanks anyway. $\endgroup$ Commented Mar 9, 2013 at 2:29

1 Answer 1

7
$\begingroup$

First, your function can be simplified:

FullSimplify[ -Mod[(Pi/λ)*(Sqrt[ρ^2 + f^2] - f), Pi] /. {ρ -> Norm[{x, y}], ϕ -> ArcTan[x, y]}, Element[x | y, Reals] ] 
-Mod[(Pi*(-f + Sqrt[f^2 + x^2 + y^2]))/λ, Pi] 

Second, you appear to want a DensityPlot rather than a ContourPlot.

Third, you can get smoother graphics by increasing PlotPoints:

With[{λ = 1, f = 50, R = 25}, DensityPlot[ Mod[(Pi*(-f + Sqrt[f^2 + x^2 + y^2]))/λ, Pi], {x, -R, R}, {y, -R, R}, ColorFunction -> "Rainbow", Frame -> False, PlotPoints -> 100, ExclusionsStyle -> Black, RegionFunction -> (Sqrt[#1^2 + #2^2] < R &) ] ] 

Mathematica graphics

Or as a Plot3D:

With[{λ = 1, f = 50, R = 25}, Plot3D[Mod[(Pi*(-f + Sqrt[f^2 + x^2 + y^2]))/λ, Pi], {x, -R, R}, {y, -R, R}, ColorFunction -> "Rainbow", PlotPoints -> 25, RegionFunction -> (Sqrt[#1^2 + #2^2] < R & ) ] ] 

Mathematica graphics


Incorporating R.M's suggestions from the comments:

jet[u_?NumericQ] /; 0 <= u <= 1 := Blend[{{0, RGBColor[0, 0, 9/16]}, {1/9, Blue}, {23/63, Cyan}, {13/21, Yellow}, {47/63, Orange}, {55/63, Red}, {1, RGBColor[1/2, 0, 0]}}, u] With[{λ = 1, f = 50, R = 25}, ArrayPlot[Table[ If[Norm[{x, y}] < R, Mod[(Pi*(-f + Sqrt[f^2 + x^2 + y^2]))/λ, Pi], None], {x, -25, 25, 0.1}, {y, -25, 25, 0.1} ], ColorFunction -> jet, Frame -> False ] ] 

Mathematica graphics

$\endgroup$
9
  • $\begingroup$ Thanks Mr. Wizard, I can actually get the same figure as you shown. But I wanna cut out the corners, say, just show the circular patterns. $\endgroup$ Commented Mar 8, 2013 at 0:18
  • $\begingroup$ @Tony Sorry, I removed one too many options in my attempt to simplify things. Please look again and tell me if this helps. $\endgroup$ Commented Mar 8, 2013 at 0:20
  • $\begingroup$ That's exactly what I want. Thank you so much. $\endgroup$ Commented Mar 8, 2013 at 0:22
  • $\begingroup$ @Tony I just noticed that ϕ (phi) does not appear to be used in the function. What am I missing? What do you intend? $\endgroup$ Commented Mar 8, 2013 at 0:26
  • 1
    $\begingroup$ @TonyDong You don't see the exclusions in MATLAB only because you're doing a finite sampling without any consideration for the function's discontinuities. DensityPlot as Mr.Wizard used here is "aware" of the discontinuity, which is why we have to deal with Exclusions. If you instead sample the domain and plot it using ListDensityPlot, then you'll reproduce MATLAB's figure. Also, see this answer if you want to reproduce the jet colorscheme. $\endgroup$ Commented Mar 8, 2013 at 4:44

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.