
我想要指示 TikZ 繪製有理函數圖形的程式碼y=(x^{2}+5x+6)/(x^{2}+2x-3)
。我嘗試使用addplot[very thin,blue]{frac(x^{2}+5x+6)/(x^{2}+2x-3)}
.圖中有一條垂直漸近線 x = 1。
\documentclass[10pt]{amsart}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{amsthm}
\begin{document}
\hspace*{\fill}
\begin{tikzpicture}
\begin{axis}[axis equal image,
xmax=8,ymax=7,
axis lines=middle,
restrict y to domain=-7:7,
enlargelimits={abs=1cm},
axis line style={latex-latex},
ticklabel style={fill=white},
ytick=\empty,
xtick={-3}
%xlabel=$x$,ylabel=$y$,
]
\addplot[domain=-10:10,mark=none,samples=10] {frac{x + 2}{x - 1} node [above left, yshift=3pt]{$\scriptstyle{y}=\frac{x^{2}+5x+6}{x^{2}+2x-3}$};
\draw [fill=white] (-3,0) circle [radius=1.5pt];
\end{axis}
\end{tikzpicture}
\hspace{\fill}
\end{document}
答案1
使用我的 PGFplots 版本,\addplot
您提供的命令甚至無法編譯,所以我必須猜測您想要實現什麼。在 PGFplots 中,您可以像這樣寫分數:1/(x^2)
。
馬修對傑克的回答的評論這個問題x=1
您可以使用 鍵繪製漸近線vasymptote=1
。為了在不使用許多樣本的情況下可靠地截斷繪圖(橙色曲線),您可以分別繪製兩個部分,並指定domain
稍早結束x=1
(黑色曲線)。
\documentclass[10pt]{amsart}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}
\pgfplotsset{vasymptote/.style={
before end axis/.append code={
\draw[densely dashed] ({rel axis cs:0,0} -| {axis cs:#1,0})
-- ({rel axis cs:0,1} -| {axis cs:#1,0});
}
}}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis equal image,
axis lines=middle,
xmin=-10,xmax=10,
ymin=-10,ymax=10,
enlargelimits={abs=1cm},
axis line style={latex-latex},
ticklabel style={fill=white},
ytick={-10,10},
xtick={-3,-10,10},
vasymptote=1,
]
% This doesn't clip to y=-10:10 nicely
% because there are too few samples near the asymptote:
\addplot[very thick, orange, domain=-10:10,samples=200, restrict y to domain=-10:10]
{(x^2+5*x+6)/(x^2+2*x-3)};
% Draw the two parts separately with individual domains:
\addplot[samples=50,domain=-10:1-0.27] {(x^2+5*x+6)/(x^2+2*x-3)};
\addplot[samples=50,domain=1+0.33:10] {(x^2+5*x+6)/(x^2+2*x-3)};
\draw [fill=white] (-3,0) circle [radius=1.5pt]; % What is this?
\end{axis}
\end{tikzpicture}
\end{document}
答案2
Mark Wibrow 的解決方案將產生永久性的災難性的plotpoints=14*(2^n)+1
(對於任何非負整數n
)。
因此可以使用以下解決方案來克服這樣的問題(只是為了好玩)。
\documentclass[pstricks,border=20pt,12pt]{standalone}
\usepackage{pst-plot}
\def\f{(x+2)/(x-1)}
\begin{document}
\begin{psgraph}[algebraic,plotpoints=1000,Dy=5,Dx=2]{->}(0,0)(-11,-30)(11,35){20cm}{20cm}
\psset{linecolor=red}
\psplot{-10}{0.9}{\f}
\psplot{1.1}{10}{\f}
\psset{linestyle=dashed,linecolor=blue}
\psline(1,-30)(1,35)
\psline(-11,1)(11,1)
\end{psgraph}
\end{document}
答案3
只是為了好玩,使用 PSTricks :O
\documentclass[pstricks,border=5]{standalone}
\usepackage{pst-plot}
\begin{document}
\psset{xunit=20pt,yunit=20pt}
\begin{pspicture}*(-7.5,-7.5)(7.5,7.5)
\psgrid[gridlabels=0,gridcolor=gray!25,subgridcolor=gray!10](-7,-7)(7,7)
\psaxes[labels=none,ticksize=-2pt 2pt]{<->}(0,0)(-7,-7)(7,7)[$x$,-90][$y$,0]
\psplot[linecolor=blue,plotstyle=line,algebraic, yMaxValue=7,plotpoints=1000]
{-7}{7}{(x^2+5*x+6)/(x^2+2*x-3)}
\psline[linestyle=dashed](1,-7)(1,7)
\rput(4.5,4){$\displaystyle f(x)=\frac{x^2+5x+6}{x^2+2x-3}$}
\end{pspicture}
\end{document}
答案4
您所需要的只是幾個 addplot 指令:
\addplot[domain=-10:10,samples=1000]
{(x^2+5*x+6)/(x^2+2*x-3)};
\addplot[dashed, purple, latex-latex]
coordinates{(1,-6) (1,6)};
第一個應該繪製您的實際函數,第二個應該使用座標繪製一條垂直線。該選項在兩端latex-latex
添加箭頭。latex
如果您不想兩端都有箭頭,只需刪除相應的latex
.如果你想讓垂直線更長,只需用更大的東西替換坐標中的-6和6,例如。 -15 和 15。
這是一個完整的例子:
\begin{tikzpicture}
\begin{axis}[axis equal image,
xmin=-8, xmax=8,
ymin=-7, ymax=7,
axis lines=middle,
restrict y to domain=-10:10,
enlargelimits={abs=1cm},
axis line style={-latex},
ytick=\empty,
xtick={-3},
%xlabel=$x$,ylabel=$y$,
]
\addplot[domain=-10:10,samples=1000]
{(x^2+5*x+6)/(x^2+2*x-3)};
\addplot[dashed, purple, latex-latex]
coordinates{(1,-6) (1,6)};
\end{axis}
\end{tikzpicture}