我目前正在尝试将多个图形放在一个图形中\tikzpicture
,但当我尝试放入正切函数时,由于其在 pi/2 处的未定义性质以及我的域被定义为-pi:pi
我的 LaTeX 编译器不断出现故障。这是代码
\begin{tikzpicture}[domain=0:pi]
\draw[very thin, color=gray] (-pi,-1) grid (pi,1);
\draw[->] (-7.0,0) -- (7.0,0) node[right] {$x$};
\draw[->] (0,-1.2) -- (0,1.2) node[above] {$f(x)$};
\draw[color=red] plot(\x,{sin(\x r)}) node[right] {$f(x) = \sin(x)$};
\draw[color=blue] plot(\x,{cos(\x r)}) node[right] {$f(x) = \cos(x)$};
\draw[color=orange] plot(\x,{tan(\x r)}) node[right] {$f(x) = \tan(x)$};
\end{tikzpicture}
我最大的问题是,我怎样才能在这个图表上设置范围限制,以便绘制正切函数,是否还有其他库可以用来绘制正切函数?
答案1
如果没有pgfplots
它,会稍微麻烦一些。网格的上边界和下边界分别为 1 和 -1,因此需要计算角度。然后可以显示 tan 函数的可见、允许部分:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[domain=-pi:pi]
\draw[very thin, color=gray] (-pi,-1) grid (pi,1);
\draw[->] (-4,0) -- (6,0) node[right] {$x$};
\draw[->] (0,-1.2) -- (0,1.2) node[above] {$f(x)$};
\draw[color=red] plot(\x,{sin(\x r)})
node[above right] {$f(x) = \sin(x)$};
\draw[color=blue] plot(\x,{cos(\x r)})
node[right] {$f(x) = \cos(x)$};
\draw[color=orange]
plot[domain=-pi:-.75*pi] (\x, {tan(\x r)})
plot[domain=-.25*pi:.25*pi] (\x, {tan(\x r)})
plot[domain=.75*pi:pi](\x,{tan(\x r)})
node[below right] {$f(x) = \tan(x)$};
\end{tikzpicture}
\end{document}
答案2
因为您还没有让我们知道您是否已经找到了解决方案,所以这里有一种可能性......
\documentclass[border=2pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.3}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
% set the labels at the axes
xlabel=$x$,
ylabel=$f(x)$,
% set the domain where the values of the functions should be calculated for
domain=0:pi,
% draw the lines "smooth"
smooth,
% set limits of the axis
xmin=0,
xmax=pi,
ymin=-2,
ymax=2,
% to avoid PGF Math Errors (while calculating the tangent values)
% restrict the y domain
% for that the limits should be a bit larger than the axis limits
% (compare with the result if the limits are equal)
restrict y to domain=-5:5,
]
\addplot [red] function {sin(x)};
\addplot [blue] function {cos(x)};
\addplot [orange] function {tan(x)};
\end{axis}
\end{tikzpicture}
\end{document}
答案3
您可以以某种方式重新定义tan
函数(mytan
在示例中命名),以使其在任何地方定义。
\documentclass[tikz,border=7pt]{standalone}
\usetikzlibrary{math}
\tikzmath{
function mytan(\x) {
if abs(cos(\x)) < .001 then {
return 100;
} else {
return tan(\x);
};
};
}
\begin{document}
\begin{tikzpicture}[domain=-pi:pi, samples=100]
\draw[very thin, color=gray] (-pi,-1) grid (pi,1);
\clip (-pi,-2) rectangle (2*pi,2);
\draw[->] (-7.0,0) -- (7.0,0) node[right] {$x$};
\draw[->] (0,-1.2) -- (0,1.2) node[above] {$f(x)$};
\draw[color=red] plot(\x,{sin(\x r)}) node[above right] {$f(x) = \sin(x)$};
\draw[color=blue] plot(\x,{cos(\x r)}) node[right] {$f(x) = \cos(x)$};
\draw[color=orange] plot(\x,{mytan(\x r)}) node[below right] {$f(x) = \tan(x)$};
\end{tikzpicture}
\end{document}