Stückweise definierte Funktion schlecht gefärbt

Stückweise definierte Funktion schlecht gefärbt

Ich möchte dies plotten

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{calc}
\pgfplotsset{compat=newest}% <- set a compat!! (current version is 1.14)
%%%%%%%
\pgfkeys{/pgf/declare function={argsinh(\x) = ln(\x + sqrt(\x^2+1));}}
\pgfkeys{/pgf/declare function={argcosh(\x) = ln(x + sqrt(-1 + x)*sqrt(1 + x));}}
%%%%%%
\begin{document}
\begin{tikzpicture}
\begin{axis}[   
    cycle list name= color list,    
    tick align=center,
    axis lines = left,
    xmin = 0,
    xmax = 2,
    ymin = 0,
    ymax = 1.1,   
    ylabel = {$\mathopen|H(j\Omega)\mathclose|$},    
    xtick = {1,1.2},
    xticklabels = {1, $\Omega_s$},
    ytick = {0,0.1, 0.5, 1},
    yticklabels = {0, $\frac{1}{1+\varepsilon^2/k_1^2}$,$\frac{1}{1+\varepsilon^2}$,1},  
    ]

    \foreach \n in {5,6} {
    \addplot+[domain=0:1,samples=201]{1/(1+1^2*(cos(\n*(acos(\x))))^(2))};

   \addplot+[domain=1:2,samples=201]{1/(1+1^2*(cosh(\n*(argcosh(\x))))^(2))};
    }

    \addplot[black,dotted] coordinates {
        (1.2,1)
        (1.2,.1)
        (2,.1)
    };
    \addplot[black,dotted] coordinates {
        (0,.5)
        (1,.5)
        (1,0)
    };
\end{axis}
\end{tikzpicture}
\end{document}

Aber ich muss diese vier Farben vermeiden und nur zwei für beide \n verwenden. Dann wurde mir klar, dass dies durch die Definition einer neuen einzelnen stückweisen Funktion möglich ist

%%%%%%
\begin{tikzpicture}[
\declare function={
chebyshev(\x)= (\x<=1) * (1/(1+1^2*(cos(\n*(acos(\x))))^(2)))+
     and(\x>1) * (1/(1+1^2*(cosh(\n*(argcosh(\x))))^(2)));
     }]
%%%%%% ...

\foreach \n in {5,6} {

    \addplot+[domain=0:2,samples=400]{chebyshev(\x)};

}

Aber es endet jedes Mal mit einem Fehler und ich kann einfach nicht erkennen, wo das Problem liegt. Können Sie mir helfen?

Antwort1

Das Problem besteht darin, dass acos(x)nur für definiert ist x \in [0,1]und Sie versuchen, für zu plotten x \in [0,2]. Am einfachsten wäre es, bei der Variante mit zwei Funktionen zu bleiben und den Farbindex um eins zu sichern, bevor Sie den zweiten mit plotten \pgfplotsset{cycle list shift=-1}.

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{calc}
\pgfplotsset{compat=newest}% <- set a compat!! (current version is 1.14)
%%%%%%% 
\pgfkeys{/pgf/declare function={argsinh(\x) = ln(\x + sqrt(\x^2+1));}}
\pgfkeys{/pgf/declare function={argcosh(\x) = ln(x + sqrt(-1 + x)*sqrt(1 + x));}}
%%%%%% 
\begin{document}
\begin{tikzpicture}
  \begin{axis}[   
    cycle list name= color list,    
    tick align=center,
    axis lines = left,
    xmin = 0,
    xmax = 2,
    ymin = 0,
    ymax = 1.1,   
    ylabel = {$\mathopen|H(j\Omega)\mathclose|$},    
    xtick = {1,1.2},
    xticklabels = {1, $\Omega_s$},
    ytick = {0,0.1, 0.5, 1},
    yticklabels = {0, $\frac{1}{1+\varepsilon^2/k_1^2}$,$\frac{1}{1+\varepsilon^2}$,1},  
    ]

    \foreach \n in {5,6} {
      \addplot+[domain=0:1,samples=201]{1/(1+1^2*(cos(\n*(acos(\x))))^(2))};
      \pgfplotsset{cycle list shift=-1}
      \addplot+[domain=1:2,samples=201]{1/(1+1^2*(cosh(\n*(argcosh(\x))))^(2))};
    }

    \addplot[black,dotted] coordinates {
      (1.2,1)
      (1.2,.1)
      (2,.1)
    };
    \addplot[black,dotted] coordinates {
      (0,.5)
      (1,.5)
      (1,0)
    };
  \end{axis}
\end{tikzpicture}
\end{document}

Bildbeschreibung hier eingeben

verwandte Informationen