x*cos(1/x) 的图形

x*cos(1/x) 的图形

我正在尝试在 tikz 中绘制图形f(x)=x*cos(1/x)并使用以下代码:

\documentclass[10pt]{article}
\usepackage{pgf,tikz}
\usetikzlibrary{arrows}
\pagestyle{empty}
\begin{document}
\begin{tikzpicture}[line cap=round,line join=round,>=triangle 45,x=1.0cm,y=1.0cm]
\draw[->,color=black] (-0.45754230805133705,0.0) -- (0.6210968701809529,0.0);
\foreach \x in {-0.4,-0.30000000000000004,-0.20000000000000004,-0.10000000000000003,0.19999999999999998,0.3,0.4,0.5,0.6}
\draw[shift={(\x,0)},color=black] (0pt,2pt) -- (0pt,-2pt) node[below] {\footnotesize $\x$};
\draw[->,color=black] (0.0,-0.340872843559345) -- (0.0,0.38634286875659907);
\foreach \y in {-0.30000000000000004,-0.20000000000000004,-0.10000000000000003,0.19999999999999998,0.3}
\draw[shift={(0,\y)},color=black] (2pt,0pt) -- (-2pt,0pt) node[left] {\footnotesize $\y$};
\draw[color=black] (0pt,-10pt) node[right] {\footnotesize $0$};
\clip(-0.45754230805133705,-0.340872843559345) rectangle (0.6210968701809529,0.38634286875659907);
\draw[smooth,samples=100,domain=-0.45754230805133705:0.6210968701809529] plot(\x,{(\x)*\sigma\upsilon\nu((1.0/(\x))*180/pi)});
\end{tikzpicture}
\end{document}

但是它无法编译。我猜问题在于函数未在零处定义。上面的代码是 Geogebra 给我的。有什么想法吗?我想要完整的代码!

答案1

我只需使用该包进行绘图pgfplots,该包建立在其基础上pgf,但是是一个更“自然”的绘图界面:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\begin{document}
\begin{tikzpicture}
  \begin{axis}[samples = 1000]
    \addplot[mark = none] {x * cos ( 180 / ( x / pi ) ) };
  \end{axis}
\end{tikzpicture}
\end{document}

我需要在这里增加样本,否则就没有足够的细节:我不太确定你实际上想要了解情节的哪一部分!

(请注意,pgf数学系统需要以度为单位的角度,因此,就像问题中的自动生成代码一样,我必须将其从x弧度值转换为度数。)

将轴从“框”(常见的科学绘图样式)移动到中心(更常用于绘制公式),并且可以相对轻松地实现“放大”

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\begin{document}
\begin{tikzpicture}
  \begin{axis}[axis lines = middle, domain = -1:1, samples = 1000]
    \addplot[mark = none] {x * cos ( 180 / ( x / pi ) ) };
  \end{axis}
\end{tikzpicture}
\end{document}

答案2

使用 PSTricks 的纯乐趣解决方案。

\documentclass[pstricks,border=20pt,12pt,dvipsnames]{standalone}
\usepackage{pst-plot}

\def\f{x*cos(1/x)}

\begin{document}

\begin{psgraph}[algebraic,Dx=0.025,Dy=0.01,plotpoints=1000]{->}(0,0)(-.1,-.1)(.1,.1){15cm}{!}
    \psplot[linecolor=Red]{-.1}{-0.005}{\f}
    \psplot[linecolor=Red]{0.005}{.1}{\f}
\end{psgraph}
\end{document}

在此处输入图片描述

放大动画

\documentclass[pstricks,border=20pt,12pt,dvipsnames]{standalone}
\usepackage{pst-plot}
\usepackage[nomessages]{fp}

\def\f{x*cos(1/x)}

\begin{document}
\foreach \i in {1.5,1.4,...,0.1}{%
\FPeval\D{round(\i/5:2)}%
\begin{psgraph}[algebraic,Dx=\D,Dy=\D,plotpoints=1000]{->}(0,0)(-\i,-\i)(\i,\i){20cm}{!}
    \psplot[linecolor=Red]{-\i}{\D\space 10 div neg}{\f}
    \psplot[linecolor=Red]{\D\space 10 div}{\i}{\f}
\end{psgraph}}
\end{document}

在此处输入图片描述

相关内容