我正在尝试绘制此函数:f(x)=(x^2+3x+9)/(3x)
。出现错误,提示无法将 9 除以 0,请问有什么解决办法吗?
这是我的代码:
\begin{tikzpicture}
\draw[step=0.5cm,cyan,very thin] (-1,-1) grid (5,6);
\draw[black, ->] (-1,0) -- (5,0);
\draw[black, ->] (0,-1) -- (0,6);
\draw[thick, black, ->] (0,0) -- (0.5,0);
\draw[thick, black, ->] (0,0) -- (0,0.5);
\draw [scale=0.5, domain=0:10,samples=25, teal] plot (\x,{(9+(\x)^2+3*\x)/(3*\x)}) ;
\draw [scale=0.5, domain=-2:10, smooth, variable=\x, blue] plot ({\x}, {-2/3*\x+5});
\filldraw[blue] (3,0.5) node[anchor=south] {$d$};
\filldraw[teal] (4,2) node[anchor=south] {$\mathcal{C}$};
\filldraw[black] (3,1.75) node[anchor=south west] {A};
\filldraw[black] (3,1.75) node[] {$\times$};
\filldraw[black] (0,0.25) node[anchor=east] {$\overrightarrow{i}$};
\filldraw[black] (0.25,0) node[ anchor=north] {$\overrightarrow{j}$};
\filldraw[black] (5,0) node[ anchor=south east] {$x$};
\filldraw[black] (0,6) node[ anchor=north west] {$y$};
\foreach \x in {-1,2,3,4,5,6,7,8,9}
\draw ({\x cm/2},2pt) -- ({\x cm/2},-2pt) node[anchor=north] {$\x$};
\foreach \y in {2,3,4,5,6,7,8,9,10,11}
\draw (2pt, {\y cm/2}) -- (-2pt, {\y cm/2}) node[anchor=east]{$\y$};
\end{tikzpicture}
这是错误:
软件包 PGF 数学错误:您要求我用“
9.0' by
0.0”除以“0.0”,但我无法用“{(9+(0)^2+30)/(30)}')。... 青色] 图 (\x,{(9+(\x)^2+3*\x)/(3*\x)})
答案1
绿色多项式函数的域错误。如果将 x 值设置为 0,y 值将为无穷大。对于 x=0.2,y 值为 16.067。这大于 y 轴上的 11。因此,将域设置为 0.2:10。然后绿线的绘图将超出 y 轴的限制,并且看起来与添加的图片相同。
答案2
为了解决这个问题,人们使用pgfplots
,用它可以轻松生成所示的图片(针对域 0 到 9)。
但是,当绘制定义间隙时(以丑陋的连接线的形式),问题也会再次出现。因此,通常您必须分段绘制函数(就像您必须使用“TikZ-only”一样)。
\documentclass[border=5pt, varwidth]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}[>=latex]
\begin{axis}[%axis equal,
axis lines=middle,
xmin=-0.0, xmax=9.5,
ymin=-0.5, ymax=11.75,
xtick={1,...,9},
ytick={1,...,11},
grid=major,
samples=222,
xlabel={$x$},
xlabel style = {anchor=south east},
ylabel={$y$},
ylabel style = {anchor=east},
enlarge x limits={abs=0.5},
]
\addplot[draw=green!55!black, thick, mark=none,
domain=0:9]{(x^2+3*x+9)/(3*x)};
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}[>=latex]
\begin{axis}[x=5mm, y=5mm,
axis lines=middle,
xmin=-9.5, xmax=9.5,
ymin=-11.75, ymax=11.75,
xtick={-9,...,9},
ytick={-11,...,11},
grid=major,
samples=222,
xlabel={$x$},
xlabel style = {anchor=south east},
ylabel={$y$},
ylabel style = {anchor=east},
]
\addplot[draw=green!55!black, thick, mark=none,
domain=0.0:9]{(x^2+3*x+9)/(3*x)};
\addplot[draw=green!55!black, thick, mark=none,
domain=-9:-0.1]{(x^2+3*x+9)/(3*x)};
\end{axis}
\end{tikzpicture}
\end{document}