
我使用以下程式碼來描述數學函數。這是我的座標系:
\newenvironment{graph}[2]{\begin{tikzpicture}[scale=0.6]
\draw[->] (0,0) -- (10,0) node[right] {$#1$};
\draw[->] (0,0) -- (0,10) node[above] {$#2$};
}
{
\end{tikzpicture}
}
要建立函數,我使用以下命令:
\newcommand{\gf}[2]{\draw plot[variable=\t,samples=1000,scale=1,domain= #1 ,smooth] ({\t},{#2});}
它將域和函數本身作為輸入。
問題是,每次繪製新函數時我都必須定義域。正如您從我的座標系中看到的,我總是在十乘十的座標系中描繪圖形。我想建立一個新命令,它可以確定我選擇的任何函數的域,以便它包含在我的十乘十座標系中。這可能嗎?
具有兩個函數的範例:
\begin{graph}{x}{y}
\gf{0:10}{\t^2}
\gf{0:10}{3-2\t}
\end{graph}
在此範例中,在 10 處計算的第一個函數的值為 100,這是越界的。我們將函數稱為 g(x)。我希望 Latex 找到 x 的值,使函數的值為 10,g(x)=10,並將該值插入為域的上限。
第二個函數也有類似的問題。我們稱之為 f(x)。它變得非常負,並在底部退出坐標系。在這種情況下,我希望 Latex 找到函數的值,以便 f(x)=0 並將其插入作為上限。
答案1
好的,這是一個帶有 的範例pgfplots
。我同意 Johannes_B 和 Benedikt Bauer 使用 pgfplots 的觀點。
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.9}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines=left,
scaled ticks=true,
xlabel=$x$,
ylabel=$y$,
xmax=11,
ymax=110,
small,
domain=1:10,
samples=100
]
\addplot[red,thick,-stealth] {x^2};
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[
axis lines=left,
scaled ticks=true,
xlabel=$x$,
ylabel=$y$,
xmax=11,
ymax=11,
small,
domain=1:10,
samples=100
]
\addplot[blue,thick,loosely dotted,-stealth] {-2*x+3};
\draw[fill=red] (axis cs:6,-10) circle (3pt);
\draw[magenta,-stealth] (axis cs:8,0) -- (axis cs:6.1,-9.6);
\end{axis}
\end{tikzpicture}
\end{document}
總而言之,pgfplots
它非常靈活,您可以tikz
在圖表內使用。