%20%E7%9A%84%E5%9C%96%E5%BD%A2.png)
我正在嘗試在 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}