我想绘制这个
\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}