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'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

share|improve this question
    
I added your graphic as an example for you, as I know the site does not let you do that yet. –  Mr.Wizard Mar 8 '13 at 1:06
    
Thank you so much, Mr. Wizard. –  Tony Dong Mar 8 '13 at 1:09
    
Tony, if my post fully answers your question please consider Accepting it. If not, please tell me in what way it is lacking. –  Mr.Wizard Mar 9 '13 at 0:34
    
@ 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. –  Tony Dong Mar 9 '13 at 2:29
add comment

1 Answer

up vote 5 down vote accepted

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

share|improve this answer
    
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. –  Tony Dong Mar 8 '13 at 0:18
    
@Tony Sorry, I removed one too many options in my attempt to simplify things. Please look again and tell me if this helps. –  Mr.Wizard Mar 8 '13 at 0:20
    
That's exactly what I want. Thank you so much. –  Tony Dong Mar 8 '13 at 0:22
    
@Tony I just noticed that ϕ (phi) does not appear to be used in the function. What am I missing? What do you intend? –  Mr.Wizard Mar 8 '13 at 0:26
1  
@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. –  rm -rf Mar 8 '13 at 4:44
show 4 more comments

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.