分段定義的函數顏色很差

分段定義的函數顏色很差

我想繪製這個

\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}

但我必須避免這四種顏色,而只使用兩種顏色\n。然後我意識到,可以透過定義新的單一分段函數來完成

%%%%%%
\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)};

}

但每次都會出錯,我根本看不出問題出在哪裡。你能幫助我嗎?

答案1

問題是acos(x)只為 定義x \in [0,1],而您正在嘗試為 繪製x \in [0,2]。最簡單的方法是堅持使用兩個函數變體,並在使用 繪製第二個函數之前將顏色索引備份一\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}

在此輸入影像描述

相關內容