如何绘制函数 f(x) = |sin(|x|)| 的图形?

如何绘制函数 f(x) = |sin(|x|)| 的图形?

我知道应该如何绘制。所有波浪都在 x 轴上方,但是,我在使用 TikZ 时语法有误。

\documentclass{minimal}

\usepackage{tikz,pgfplots}

\begin{document}

\begin{center}

\begin{tikzpicture}[domain=-4:7] [scale=0.8]

    \draw[ultra thick, ->] (-4,0) -- (7.5,0) node[right] {$\textbf{X}$};

    \draw[ultra thick,->] (0,-2) -- (0,4.2) node[above] {$\textbf{Y}$};

        \draw[dashed, thick,red] (1.5,0) -- (1.5,1);

        \draw[dashed, thick,red] (4.5,0) -- (4.5,-1);

        \draw[dashed, thick,red] (-1.5,0) -- (-1.5,-1);

    \draw[ultra thick,color=blue]   plot [samples =100](\x,{\abs(\sin(\abs(x)
 r))})   node[right] {$y = |sen~ |x||$};


\end{tikzpicture}

\end{center}

\end{document}

答案1

正如评论中所说,您不应该对sin和使用反斜杠,abs而应该对使用x。因此该函数应该是

  (\x,{abs(sin(abs(\x) r))})

以获得正确的曲线。

\documentclass{minimal}

\usepackage{tikz,pgfplots}

\begin{document}

\begin{center}

\begin{tikzpicture}[domain=-4:7] [scale=0.8]

    \draw[ultra thick, ->] (-4,0) -- (7.5,0) node[right] {$\textbf{X}$};

    \draw[ultra thick,->] (0,-2) -- (0,4.2) node[above] {$\textbf{Y}$};

        \draw[dashed, thick,red] (1.57,0) -- (1.57,1);

        \draw[dashed, thick,red] (4.71,0) -- (4.71,1);

        \draw[dashed, thick,red] (-1.57,0) -- (-1.57,1);

    \draw[ultra thick,color=blue]   plot [samples =100](\x,{abs(sin(abs(\x)
 r))})   node[right] {$y = |\sin|x||$};


\end{tikzpicture}

\end{center}

\end{document}

另请更正为sen~ |x||( \sin|x|| sen → \sin ) and don't useminimal` 类。

在此处输入图片描述

答案2

可以在 pgfplots 中轻松实现:

在此处输入图片描述

\documentclass[border=1pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.3}
\begin{document}

\begin{tikzpicture}    
\begin{axis}[axis lines = middle,
            xlabel = $x$,
            ylabel = $y$,
            ymax=2.2,]
\addplot[domain=-4:7,
        samples=300,
        color=blue,
        thick,] {abs(sin(deg(x)))};
\addplot [dashed,thick,red] coordinates {(1.57 ,0) (1.57 ,1)};
\addplot [dashed,thick,red] coordinates {(4.71 ,0) (4.71 ,1)};
\addplot [dashed,thick,red] coordinates {(-1.57,0) (-1.57,1)};
\node at (axis cs:6,1.2) {$y = |\sin|x||$};
\end{axis}    
\end{tikzpicture}

\end{document}

相关内容