7

I want to draw Hermite-Gaussian modes using tikz, all that is really needed is a blurred circle and blurred lines that overlay splitting it into different sections as shown in this picture: enter image description here

I am not even able to find a proper way of doing the first one, since all blurring options i have found so far are not smooth enough.

  • The first one is relatively simple. You can draw it with: \node[circle, inner color=white, outer color=black, inner sep= 0.5 cm, label={[color=white]below:00}] (00) {};, on a black background. The others however are complicated. I've tried with a blurred line in between, but the results are not very good. – Roald Oct 9 '16 at 10:21
  • I am using \fill with a fading option. – Josh Oct 9 '16 at 12:34
13

With functional shading one has full control of the output --- as long as one knows the correct math formula.

\documentclass[tikz]{standalone}    
\pgfdeclarefunctionalshading{Hermite-Gaussian modes}{\pgfpoint{-25bp}{-25bp}}{\pgfpoint{25bp}{25bp}}{}{
    10 atan sin 1000 mul cos 1 add
    exch
    10 atan sin 1000 mul cos 1 add
    mul 4 div
    dup dup
}
\begin{document}
    \tikz\path[shading=Hermite-Gaussian modes](-10,-10)rectangle(10,10);
\end{document}

Explanation

One may also check TikZ's manual 109.2.3 General (Functional) Shadings and PDF's standard 8.7.4.5.2 Type 1 (Function-Based) Shadings and Annex B Operators in Type 4 Functions.

Some technical comment:

The canvas is 50bp by 50bp. The range of coordinate is from (-25bp, -25bp) to (25bp, 25bp), that is, the input is two real numbers falling between -25 to 25. The output of your function should be a triple of numbers between 0 and 1, black is (0,0,0), red is (1,0,0), white is (1,1,1).

I suggest that one should test the PDF file by the official renderer. By official I mean Adobe Acrobat Reader DC, which used to be called Adobe Reader. It happens that unofficial PDF readers tends to disable some insecure features of PDF, and sometimes functional shading is one of them.

Go back to my code, it is simply a function:

where

is a function looks like z when z is small and is constant when z is large.

I added two dup in my code so that when it finishes calculating the gray scale, it will then duplicate the result so R, G, B channels will get the same result. (In some rare case, if your function output just one real number, the PDF renderer will treat it like it is the gray scale. For stability and portability, however, I should have dup the result.)

  • Wow! im amazed by how compact your code is. I have no idea how you definition actually works, in other words, i am not able to draw the other modes. Would you be so kind to quickly add some comments on how you approached this and what some of the terms mean? – Josh Oct 10 '16 at 13:49
  • Also my output does not match yours. It is blue and the resolution is very low. – Josh Oct 10 '16 at 14:12
  • @Josh I added some references and fixed my code. Now Adobe Acrobat Reader DC will give you the correct result. – Symbol 1 Oct 10 '16 at 18:11
  • @ Symbol1 alright, it works! One thanks you. The PDF reader was indeed the issue here. However i do not understand how the picture is produced with such a huge frequency on the cos. If i plot your function from [-25,25] (for x and y) and use 17 instead of 1000 i get the function i would expect, but then pgf does not do what i expect. – Josh Oct 10 '16 at 19:16
  • @Josh The input of trigonometric functions in given in degrees! So 1000° ≈ 17.44 rad, everything is good! – Symbol 1 Oct 10 '16 at 23:52
3

After reading the tikz and pgf manual i have produced this: enter image description here

I don't quite like it, its okey but they could look better. I would really appreciate some tips. Maybe the only way is really plotting the 2 dimentional Hermite-Gauss intensity distributions. Here is the code:

\documentclass{standalone}
\usepackage{ifthen}
\usepackage{tikz}
\usetikzlibrary{fadings}


\tikzfading[name=fade out,
inner color=transparent!0,
outer color=transparent!100]

\tikzfading[name=middle,
top color=transparent!100,
bottom color=transparent!100,
middle color=transparent!0]

\tikzfading[name=middle rot,
right color=transparent!100,
left color=transparent!100,
middle color=transparent!0]

\begin{document}

\begin{tikzpicture}
\fill[black] (0,0) rectangle (6,6);

\fill[white!99!black,path fading=fade out] (2,4) circle (1cm);

\fill[white!99!black,path fading=fade out] (2,2) circle (1cm);

\fill[white!0!black,path fading=middle] (0,1.8) rectangle +(4,0.4);


\fill[white!99!black,path fading=fade out] (4,4) circle (1cm);

\fill[white!0!black,path fading=middle] (3,3.8) rectangle +(2,0.4);
\fill[white!0!black,path fading=middle rot] (3.8,3) rectangle +(0.4,2);

\end{tikzpicture}
\end{document}
3

After playing around with pgfplots I have found a "simple" solution. It takes quite a while to compile, however the results and the easy input make up for it: enter image description here

Here is the MWE along with the actual 3D plots: enter image description here

\documentclass[]{standalone}
\usepackage{tikz}
\usepackage{pgfplots}

\pgfplotsset{myaxis/.style={%
        view={0}{90}, 
        % view={60}{60}, % enable for 3D view
        samples=50,
        hide axis,
        colormap={bw}{gray(0cm)=(0); gray(0.1cm)=(0.15) ; gray(1cm)=(1)},
        width=8cm,
        height=8cm}}
\pgfplotsset{compat=1.5}


\begin{document}


\begin{tikzpicture}
\begin{axis}[myaxis]
\addplot3[surf,shader=interp,domain=-20:20] {(exp(-x^2/100))^2*(exp(-y^2/100))^2};
\end{axis}

\begin{axis}[xshift=6cm,myaxis]
\addplot3[surf,shader=interp,domain=-20:20] {(0.28*x*exp(-x^2/100))^2*(exp(-y^2/100))^2};
\end{axis}

\begin{axis}[yshift=-6cm,myaxis]
\addplot3[surf,shader=interp,domain=-20:20] {(exp(-x^2/100))^2*(0.28*y*exp(-y^2/100))^2};
\end{axis}


\begin{axis}[myaxis,xshift=6cm,yshift=-6cm]
\addplot3[surf,shader=interp,domain=-20:20] {(0.28*x*exp(-x^2/100))^2*(0.28*y*exp(-y^2/100))^2};
\end{axis}


\begin{axis}[myaxis,xshift=12cm]
\addplot3[surf,shader=interp,domain=-22:22] {(-2 + 0.0784*x^2)*exp(-x^2/100))^2*(exp(-y^2/100))^2};
\end{axis}

\begin{axis}[myaxis,yshift=-12cm]
\addplot3[surf,shader=interp,domain=-22:22] {exp(-x^2/100))^2*((-2 + 0.0784*y^2)*exp(-y^2/100))^2};
\end{axis}

\begin{axis}[myaxis,xshift=12cm,yshift=-6cm]
\addplot3[surf,shader=interp,domain=-22:22] {((-2 + 0.0784*x^2)*exp(-x^2/100))^2*(0.28*y*exp(-y^2/100))^2};
\end{axis}

\begin{axis}[myaxis,xshift=6cm,yshift=-12cm]
\addplot3[surf,shader=interp,domain=-22:22] {((0.28*x)*exp(-x^2/100))^2*((-2 + 0.0784*y^2)*exp(-y^2/100))^2};
\end{axis}

\begin{axis}[myaxis,xshift=12cm,yshift=-12cm]
\addplot3[surf,shader=interp,domain=-22:22] {((-2 + 0.0784*x^2)*exp(-x^2/100))^2*((-2 + 0.0784*y^2)*exp(-y^2/100))^2};
\end{axis}

\end{tikzpicture}

\end{document}

If anyone is interested in plotting higher modes than the ones provided here you just need higher order Hermite polynomials, which can be found here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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