1

I am trying to sum several fonctions in a plot with a loop (as I am trying to make the process automatic), and foreach doesn't seem to be able to help me.

Unluckily the question already asked pgfplots: use foreach inside addplot provides the opposite of what it says with an addplot inside a loop.

\foreachitem\i\in\alistlst[]{
    \addplot [domain=\i,samples=50]
    {   \GainPurGdB{\GainTot}
        +\IntegrGdB*\NbItegr
        +\PremOrdrLieuReelGdB{\WcArray[1]}
        +\PremOrdrLieuReelGdB{\WcArray[2]}
        %\foreach \k in {1,...,\WcArraylen} {
        %     \ifnum\OrdrArray[\k]=1
        %       +\PremOrdrLieuReelGdB{\WcArray[\k]}
        %   \fi
        %     \ifnum\OrdrArray[\k]=2
        %       +\DeuxOrdrLieuReelGdB{\WcArray[\k]}{\ZArray[\k]}
        %   \fi
        %}
    }[LieuReel,#4];

This code works, but if I try to use the commented part, It doesn't compile.

Do you have an idea ? Is this a lost cause ?

Thank you anyway for the time you spent reading this.

Edit : Adding a MWE

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots,listofitems}
\pgfplotsset{compat=1.16}

\newcommand{\GainPurGdB}[1]{ 20log10(#1) } \newcommand{\IntegrGdB}{ -20(log10(x)) } \newcommand{\PremOrdrLieuReelGdB}[1]{ -(10(log10(#1^2x^2+1))) } \newcommand{\DeuxOrdrLieuReelGdB}[2]{ -10(log10((1-(x/#1)^2)^2+4(#2^2)*(x/#1)^2)) }

\newcommand{\TracQUnGdB}{ \xdef\alist{(\Wmin):} \foreachitem\i\in\WcArray[]{ \xdef\alist{\alist(0.51/\i),(0.51/\i):(21/\i),(21/\i):} } \xdef\alist{\alist(\Wmax)} \foreach \i in \alist { \addplot [domain=\i,samples=50] { \GainPurGdB{\GainTot} +\IntegrGdB\NbItegr +\PremOrdrLieuReelGdB{\WcArray[1]} +\PremOrdrLieuReelGdB{\WcArray[2]} %\foreach \k in {1,...,\WcArraylen} { % \ifnum\OrdrArray[\k]=1 % +\PremOrdrLieuReelGdB{\WcArray[\k]} % \fi % \ifnum\OrdrArray[\k]=2 % +\DeuxOrdrLieuReelGdB{\WcArray[\k]}{} % \fi %} }; } }

\newcommand{\NbItegr}{1} \newcommand{\GainTot}{10} \readlist\WcArray{10,0.1} \readlist\OrdrArray{1,1} \readlist\ZArray{0,0}

\begin{document}

\begin{center} \begin{tikzpicture} % Inverses \def\Wmax{10^(2)} \def\Wmin{10^(-2)} \begin{semilogxaxis} [ymin=-90,ymax=80] \TracQUnGdB \end{semilogxaxis} \end{tikzpicture} \end{center}

\end{document}

LMT-PhD
  • 1,244

1 Answers1

2
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots,listofitems}
\pgfplotsset{compat=1.16}

\newcommand*{\GainPurGdB}[1]{  20*log10(#1)  }
\newcommand*{\IntegrGdB}{
    -20*(log10(x))  }
\newcommand*{\PremOrdrLieuReelGdB}[1]{
    -(10*(log10(#1^2*x^2+1)))  }
\newcommand*{\DeuxOrdrLieuReelGdB}[2]{
    -10*(log10((1-(x/#1)^2)^2+4*(#2^2)*(x/#1)^2))  }
\pgfplotsset{mysum/.code={%
  \ifnum\OrdrArray[#1]=1
      \edef\mysum{\mysum+\PremOrdrLieuReelGdB{\WcArray[#1]}}%
  \fi
    \ifnum\OrdrArray[#1]=2
      \edef\mysum{+\DeuxOrdrLieuReelGdB{\WcArray[#1]}}%
  \fi
}}
\newcommand*{\TracQUnGdB}{ 
    \xdef\alist{(\Wmin):}
    \foreachitem\i\in\WcArray[]{
     \xdef\alist{\alist(0.5*1/\i),(0.5*1/\i):(2*1/\i),(2*1/\i):}
    }
    \xdef\alist{\alist(\Wmax)}
    \foreach \i in \alist {
         \edef\mysum{}
         \pgfplotsset{mysum/.list={1,...,\WcArraylen}}
         %\typeout{\mysum}
        \expanded{\noexpand\addplot [domain=\i,samples=50]
        {   \GainPurGdB{\GainTot}
            +\IntegrGdB*\NbItegr
            +\PremOrdrLieuReelGdB{\WcArray[1]}
            +\PremOrdrLieuReelGdB{\WcArray[2]}
            \mysum
        };}
    }
}

\newcommand*{\NbItegr}{1}
\newcommand*{\GainTot}{10}
\readlist\WcArray{10,0.1}
\readlist\OrdrArray{1,1}
\readlist\ZArray{0,0}

\begin{document}

\begin{tikzpicture} % Inverses
    \def\Wmax{10^(2)}
    \def\Wmin{10^(-2)}
    \begin{semilogxaxis}
        [ymin=-90,ymax=80]
        \TracQUnGdB
    \end{semilogxaxis}
\end{tikzpicture}
\end{document}

enter image description here

  • Thank you for your help. Could you tell me a bit about how it works ? – LMT-PhD May 05 '21 at 20:20
  • 1
    The mysum code accumulates the summands by extending the \mysum macro (without creating groups). The problem is that, from my point of view, the code you presented is a rather wild mix of strategies involving various packages, so I am not sure how to explain things. In other words, all I did was to add a "minimal damage" solution rather than something that can be used very generally, simply because it gets embedded into something that has quite a few ingredients that get combined in something that I might want to call "wild mix" (you do not have to agree with this assessment). –  May 05 '21 at 20:25
  • I try to minimise the need for different \usepackage, but with a code I work on several times I can end up using different ways to do the same thing like \newcommand* or \def fo replace variables by their values. Do you have indications on which is best ? If you have advices I am listening. Thank you – LMT-PhD May 06 '21 at 07:38