![我正在尝试绘制一个有理函数](https://linux22.com/image/402792/%E6%88%91%E6%AD%A3%E5%9C%A8%E5%B0%9D%E8%AF%95%E7%BB%98%E5%88%B6%E4%B8%80%E4%B8%AA%E6%9C%89%E7%90%86%E5%87%BD%E6%95%B0.png)
我想要绘制的函数是y = \frac{6x^2-3x+4}{2x^2-8}
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{titling}
\usepackage{amssymb}
\usepackage{pgfplots}
\begin{tikzpicture}
\begin{axis}
[
title = Graph of $\frac{6x^2-3x+4}{2x^2-8}$,
axis lines = center,
xlabel = $x$,
ylabel = $y$,
]
\addplot[color=red]{y = \frac{6x^2-3x+4}{2x^2-8}};
\end{axis}
\end{tikzpicture}
我是 LaTex 新手,我一直在尝试关注这里的其他答案,但还是有点困惑。抱歉发了重复的帖子。
编辑:抱歉,如果我说得不清楚,这里才是它应有的样子。
答案1
您可以尝试以下操作,如果您想要更流畅的图表,只需增加点数。
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{tikzpicture}
\begin{axis}
[
title = Graph of $\frac{6x^2-3x+4}{2x^2-8}$,
axis lines = center,
xlabel = $x$,
ylabel = $y$,
]
\addplot[color=red]{ (6*x^2-3*x+4) / (2*x^2-8) };
\end{axis}
\end{tikzpicture}
\end{document}
编辑:
正如我所说的,你可以通过和来增加点数,samples=<..>
同时你也可以限制ymin
和,ymax
因为在处有两个无限不连续点{±2}
:
\begin{tikzpicture}
\begin{axis}
[
title = Graph of $\frac{6x^2-3x+4}{2x^2-8}$,
axis lines = center,
xlabel = $x$,
ylabel = $y$,
samples=500,
ymin=-150, ymax=150,
]
\addplot[color=red]{ (6*x^2-3*x+4) / (2*x^2-8) };
\end{axis}
\end{tikzpicture}
答案2
匆忙尝试使用 MetaPost 和 LuaLaTeX,使用我自己的旧模板。
\documentclass[12pt,border=5mm]{standalone}
\usepackage{luatex85, luamplib}
\mplibsetformat{metafun}
\mplibtextextlabel{enable}
\mplibnumbersystem{double}
\begin{document}
\begin{mplibcode}
vardef function(expr xmin, xmax, xstep)(text f_x) =
save x; x := xmin;
(x, f_x)
forever: hide(x := x + xstep) exitif x > xmax;
.. (x, f_x)
endfor
if x - xstep < xmax: hide(x := xmax) .. (x, f_x) fi
enddef;
u = v = .5cm;
xmax = -xmin = 15; ymax = -ymin = 20; xstep := .01;
vardef f(expr x) = (6(x**2)-3x+4)/(2(x**2)-8) enddef;
beginfig(1);
drawoptions(withcolor green);
draw (-2u, ymin*v) -- (-2u, ymax*v);
draw (2u, ymin*v) -- (2u, ymax*v);
draw (xmin*u, 3v) -- (xmax*u, 3v);
drawoptions(withcolor red);
draw function(xmin, -2.1, .xstep)(f(x)) xyscaled (u,v);
draw function(-1.9, 1.95, xstep)(f(x)) xyscaled (u,v);
draw function(2.1, xmax, xstep)(f(x)) xyscaled (u,v);
clip currentpicture to ((xmin, ymin) -- (xmax, ymin) -- (xmax, ymax) -- (xmin, ymax) -- cycle) xyscaled (u,v);
drawoptions(withcolor black);
drawarrow (xmin*u, 0) -- (xmax*u, 0);
drawarrow (0, ymin*v) -- (0, ymax*v);
for i = 1 upto floor(xmax-.1):
draw (i*u, -2bp) -- (i*u, 2bp);
draw (-i*u, -2bp) -- (-i*u, 2bp);
endfor;
for j = 1 upto floor(ymax-.1):
draw (2bp, j*v) -- (-2bp, j*v);
draw (2bp, -j*v) -- (-2bp, -j*v);
endfor;
label.bot("$x$", (xmax*u,0)); label.lft("$y$", (0, ymax*v));
labeloffset := 5bp;
label.bot("$-2$", (-2u,0)); label.bot("$2$", (2u, 0));
label.lft("$3$", (0,3v));
endfig;
\end{mplibcode}
\end{document}
答案3
2024 年更新:Asymptote 提供了更好的精度和控制:用 缩放 x 方向和 y 方向unitsize
;用 进行总缩放;不同分支的size
变量;以及用 平滑来自采样点数量的曲线等。与相比,它相当令人满意branch
n=100
Spline
Wolfram Alpha或 [Symbolab]。2
// http://asymptote.ualberta.ca/
import graph;
import math;
unitsize(1cm,2cm);
size(8cm,5cm,false);
real a=10;
real f(real x) {
return ((6*x^2 - 3*x + 4) / (2*x^2 - 8));
}
bool3 branch(real x){return x^2 != 4;}
//ylimits(-3,7);
clip(box((-a,-3),(a,7)));
draw(graph(f,-a,a,branch,n=100,Spline),red+1pt);
// decorations: vertical and horizontal asymptotes
pen pen_asymptote=green;
drawline((2,0),(2,1),pen_asymptote);
drawline((-2,0),(-2,1),pen_asymptote);
drawline((0,3),(1,3),pen_asymptote);
label("$2$",(2,0),SE);
label("$-2$",(-2,0),SW);
label("$3$",(0,3),NW);
label("$y=\frac{6x^2-3x+4}{2x^2-8}$",point(NE),SW);
axes("$x$","$y$");
老的:使用普通的 TikZ:
\documentclass[tikz,border=5mm]{standalone}
\begin{document}
\begin{tikzpicture}[yscale=1/30,xscale=1.2,
declare function={
f(\x)=(6*\x*\x-3*\x+4)/(2*\x*\x-8);
}]
\def\xmin{-4} \def\xmax{4}
\def\ymin{-100} \def\ymax{100}
\draw[->] (\xmin,0)--(\xmax,0) node[below]{$x$};
\draw[->] (0,\ymin)--(0,\ymax) node[right]{$y$};
\draw (2,\ymax)--(2,\ymin) (-2,\ymax)--(-2,\ymin);
\draw[magenta,smooth,samples=100]
plot[domain=-1.95:1.97] (\x,{f(\x)})
plot[domain=2.03:3.8] (\x,{f(\x)})
plot[domain=-3.8:-2.05] (\x,{f(\x)});
\draw
(0,50)--+(0:1mm)--+(180:1mm) node[left]{$50$}
(0,-50)--+(0:1mm)--+(180:1mm) node[left]{$-50$};
\path
(0,0) node[below left]{O}
(2,0) node[below right]{$2$}
(-2,0) node[below left]{$-2$}
(current bounding box.north) node[above]
{The graph of $y=\frac{6x^2-3x+4}{2x^2-8}$};
\end{tikzpicture}
\end{document}
答案4
我想我会考虑分别绘制这三个分支,如下所示:
\addplot[maxgonz,domain=-10:-2.1]{ (6*x^2-3*x+4) / (2*x^2-8) };
\addplot[maxgonz,domain=-1.99:1.99]{ (6*x^2-3*x+4) / (2*x^2-8) };
\addplot[maxgonz,domain=2.01:10]{ (6*x^2-3*x+4) / (2*x^2-8) };
这使
\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}
[
axis lines = center,
xlabel = $x$,
ylabel = $y$,
xmin=-10,xmax=10,
ymin=-10,ymax=10,
maxgonz/.style={color=red,thick,samples=100},
]
\addplot[maxgonz,domain=-10:-2.1]{ (6*x^2-3*x+4) / (2*x^2-8) };
\addplot[maxgonz,domain=-1.99:1.99]{ (6*x^2-3*x+4) / (2*x^2-8) };
\addplot[maxgonz,domain=2.01:10]{ (6*x^2-3*x+4) / (2*x^2-8) };
\end{axis}
\end{tikzpicture}
\end{document}