2

I am plotting a function that needs to be evaluated on numerous samples on some parts to avoid visible mistakes, even with the smooth option. To avoid using too many where the samples are not needed, I had cut my plot in several domain, and each had its own \addplot with a chosen number of samples. It was messy programming and it has some minor graphical mishaps. So when I came across samples at, I wanted to try it, but quite unsuccessfully :

Here is the simplified code :

    \documentclass[border=2pt]{standalone}
    \usepackage[utf8]{inputenc}
    \usepackage{tikz,pgfplots}

    \newcommand*{\addZreModif}[2]{ 
                %\addplot[#2,thick=3pt,ultra thick,samples at={0,((2*pi/W)/500),...,((2*pi/W)/10),((2*pi/W)*1.1/10),...,(2*pi/W),(2*pi*1.02/W),...,(4*pi/W),...,Ttrace}] {  
                \addplot[#2,thick=3pt,ultra thick,samples at={0,((2*pi/W)/500),...,((2*pi/W)/10)}] {  
                        ( 1 - ( (exp(-W*#1*x)/sqrt(1-#1^2)) *cos((W*x*sqrt(1-#1^2) 
                        - (atan(#1/sqrt(1-#1^2))*pi/180) ) *180/pi) )  ) *\Kg }; 
            }

    \begin{document}
    \begin{tikzpicture}[
      declare function={
        W=pi*2;
        Ttrace=3;
      }]
    \def\Kg{2}
    \def\Ezero{1}
    \begin{axis}
       \addZreModif{0.2}{red,dotted}
    \end{axis}
    \end{tikzpicture}
    \end{document}

The interesting bit lies inside the \newcommand, in the options of the addplot:

this one:

\addplot[#2,thick=3pt,ultra thick,samples at={0,((2*pi/W)/500),...,((2*pi/W)/10)}] {

is the working one. It represents the first domain (the start of the curve, with an horizontal starting slop I don't want to miss) with 50 points

The longer one, commented in the presented code :

\addplot[#2,thick=3pt,ultra thick,samples at={0,((2*pi/W)/500),...,((2*pi/W)/10),((2*pi/W)*1.1/10),...,(2*pi/W),(2*pi*1.02/W),...,(4*pi/W),...,Ttrace}] {  

is the one I wished to use, based on examples I've seen of sample at but it doesn't compute and that's why I'm here. by the way I've seen two writings :

samples at={0,firstStep1,...,end1,firstStep2,...,end2}

and

samples at={0,firstStep1,...,end1,end1,firstStep2,...end2}

I don't which is right, I've tried both without success...

I tried :

\addplot[#2,thick=3pt,ultra thick,samples at={0,(1/500),...,(1/10),(1.1/10),...,1}] {  

and

\addplot[#2,thick=3pt,ultra thick,samples at={0,(1/500),...,(1/10),(1/10),(1.1/10),...,1}] {  

If you can tell me what's wrong, thanks in advance.

  • Your description " To avoid using too many where the samples are not needed, I had cut my plot in several domain, and each had its own \addplot with a chosen number of samples. It was messy programming and it has some minor graphical mishaps" reminds of a recent plotting problem here where I use the computer algebra system, SAGE, to do the work. It might be worth looking at if you don't get an answer. – DJP Jul 31 '19 at 13:23
  • Thanks @DJP, I'll have a look – LMT-PhD Jul 31 '19 at 13:27
2

Your best bet is to construct the list of samples as a macro, in this case \mylist. I had one heck of a time figuring out what sequences like {2,...,3} were supposed to accomplish. You should always include at least one extra point to establish the spacing.

BTW, never use #1^2 for #1*#1 when doing calculations. They are not the same!

\documentclass[border=2pt]{standalone}
\usepackage[utf8]{inputenc}
\usepackage{tikz,pgfplots}

\newcommand*{\addZreModif}[2]{ 
            %\addplot[#2,thick=3pt,ultra thick,samples at={0,((2*pi/W)/500),...,((2*pi/W)/10),((2*pi/W)*1.1/10),...,(2*pi/W),(2*pi*1.02/W),...,(4*pi/W),...,Ttrace}] {  
            \addplot[#2,thick=3pt,ultra thick,samples at={\mylist}] {  
                    ( 1 - ( (exp(-W*#1*x)/sqrt(1-#1*#1)) *cos((W*x*sqrt(1-#1*#1) 
                    - (atan(#1/sqrt(1-#1*#1))*pi/180) ) *180/pi) )  ) *\Kg }; 
        }

\begin{document}
\begin{tikzpicture}[
  declare function={
    W=pi*2;
    Ttrace=3;
  }]
\xdef\mylist{0}
\foreach \i in {1,2,...,50} {\pgfmathparse{\i/500}%
  \xdef\mylist{\mylist,\pgfmathresult}};
\foreach \i in {1.1,1.2,...,10} {\pgfmathparse{\i/10}%
  \xdef\mylist{\mylist,\pgfmathresult}};
\foreach \i in {1.02,1.04,...,2} {\xdef\mylist{\mylist,\i}};
\foreach \i in {2.1,2.2,...,3} {\xdef\mylist{\mylist,\i}};
\def\Kg{2}
\def\Ezero{1}
\begin{axis}
   \addZreModif{0.2}{red,dotted}
\end{axis}
\end{tikzpicture}
\end{document}

demo

| improve this answer | |
  • Thank you for your answer, I was wondering how to concatenate lists to be able to chose the incremental value like that – LMT-PhD Jul 31 '19 at 19:27
  • BTW, what is the difference between #1^2 and #1*#1 ? – LMT-PhD Jul 31 '19 at 19:49
  • 1
    #1^2=exp(2*log(#1)) which is much slower to calculate and doesn't work for negative numbers. – John Kormylo Aug 1 '19 at 15:16

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.