这篇文章是我为那些试图绘制函数的 LaTeX 初学者提供的帮助,在尝试帮助这篇文章后,我终于找到了答案如何在 Tex 中绘制 2 个函数,但我的答案更为笼统,必须处理基本的 tikz 和 pgf 函数绘图,但同时也(和大多数)是一个建议和一个“如何”绘制函数,以防我们真的知道公式,以防我们不知道。答案的方法是向新手展示,如果他/她真的不知道实际公式,即使他/她知道一个形式,所需的数学有时会使问题脱离这个网站的目的,更接近https://math.stackexchange.com/但在这儿。
问题:如何绘制如下函数:
答案1
以下是完整答案的代码,可以作为 pdf 或直接阅读:
\documentclass{article}
\usepackage{pgfplots}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\section{Ploting Mathematical Functions Using tikz or pgfplots}
Let's suppose we want to plot a function like this:
\begin{center}
\begin{tikzpicture}
\draw[-,blue!30!green] (0,0)to[out=0,in=225](1.5,1);
\draw[-,blue!30!green] (1.5,1)to[out=45,in=190](3,2);
\end{tikzpicture}
\end{center}
\subsection{The easy way (without real function):}
\subsubsection{Observe the function:}
We have just to pay attention to the properties of the function that has to be ploted. Here we need:
\begin{itemize}
\item The function has to start from about $(0,0)$ with almost zero (but possitive) derivative.
\item The function has to pass through $(1.5,1)$ with a derivative of about 1 (angle of $45^{\circ}$)
\item The function has to finish on point $(3,2)$ by a derivative of about 0 and possitive.
\end{itemize}
\subsubsection{tikz tools}
The tool we will use is the lines and arrows of tikz:
\begin{itemize}
\item An axis can be drawn with (here it is a x axis):
\verb|\draw[->,thick] (0,0)--(3.5,0);|
\begin{itemize}
\item In the above example (0,0) is the starting point and (3.5,0) is the finishing
point.
\item \verb|->| means ``draw arrow'' for tikz.
\item \verb|--| connects these two points with an arrow (because of the previous)
\end{itemize}
\item A grid can be ploted like this (here it is vertical lines between 0 and 3 by 0.5 units (of x) and between -0.2 and 2.5 (of y)):
\begin{verbatim}
\def\ymax{2.5}
\foreach \x in {0,0.5,...,3.5}{
\draw[-,thin] (\x,-0.2)--(\x,\ymax);}
\end{verbatim}
\begin{itemize}
\item In the above code\verb| \foreach| is a command that will be executed for each one of the values 0, 0.5, 1, 1.5 \ldots that are named (and can be used) as \verb|\x|
\item The line discribed before, but now it is a thin line.
\item In the finished example we will use two \verb|\foreach| commands to draw both $x$ and $y$ grids.
\end{itemize}
\item A curved line (here the function) can be drawn like this:
\verb|\draw[-] (0,0) to[out=10,in=235](1.5,1);|
\begin{itemize}
\item (0,0) and (1.5,1) are the starting and ending points.
\item \verb|out| is the angle which the line is going to follow to get out of the first point (See below).
\item \verb|in| is the angle which the line is going to follow to get in the second point.
\end{itemize}
We can remember the angles by creating imaginatively an analog clock where
3 o'clock is 0 degrees and 12 o'clock is 90 degrees etc:
\begin{tikzpicture}
\draw (2,-5) circle (3);
\node at (2,-5) {\LARGE clock};
\node (A)at (5,-5) {$0^\circ$};
\node (B)at (-1,-5) {$180^\circ$};
\node (C)at (2,-2) {$90^\circ$};
\node (D)at (2,-8) {$270^\circ$};
\node[rotate=45] (E)at (4.1,-2.9) {$45^\circ$};
\node at ([xshift=-1.cm]A) {3 o'clock=};
\node at ([xshift=1.2cm]B) {=9 o'clock};
\end{tikzpicture}
A line that starts with $45^{\circ}$ by the point (0,0) and ends to a point (2,0) by $160^{\circ}$ can be drawn whith the command
\verb|\draw (0,0)to[out=45,in=160](2,0);|:
\begin{tikzpicture}
\draw (0,0) circle (5pt);
\draw (2,0) circle (5pt);
\draw (0,0)to[out=45,in=160](2,0);
\end{tikzpicture}
\item A \verb|\node| is something that we will use here to add labels in the axes
The node command can be used with coordinates and conetent, but the most usefull thing we have to know here is that can optionally have an `anchor' to be left,or east, or below etc of the point like:
\verb|\node[left] at (3,2) {Content};|
\end{itemize}
\subsubsection{Plotting the function}
\begin{center}
\begin{tikzpicture}[scale=2.5]
\draw[->,thick] (-0.2,0)--(3.7,0);%x axis
\draw[->,thick] (0,-0.2)--(0,2.7);%y axis
\foreach \y in {0,0.5,...,2.5}{
\draw[-,thin] (0.,\y)--(3.5,\y);
\node[left] at (0,\y) {\y};
}
\foreach \x in {0,0.5,...,3.5}{
\draw[-,thin] (\x,0)--(\x,2.5);
\node[below] at (\x,0) {\x};
}
\draw[-,blue!30!green] (0,0)to[out=5,in=225](1.5,1);
\draw[-,blue!30!green] (1.5,1)to[out=45,in=185](3,2);
\end{tikzpicture}
\end{center}
\subsection{The math way (using -extracting- the fanction):}
We want the graph to pass through $(0,0)$\footnote{Condition 1} and $(3,2)$\footnote{Condition 2} in a way that in $(1.5,1)$\footnote{Condition 3}
has a zero second derivative\footnote{Condition 4} and at $(3,2)$\footnote{Not a real condition} the first direvative will be almost zero.
(4 conditions)
The known shape comes from a function like:
$$f(x)=\frac{\alpha}{\beta+1\cdot e^{-\gamma (x+t)}}$$
(4 parameters)\footnote{The value of 1 as a mulriplier to $exp$ function
is just one of our free selected constand, since if we use any value, $\alpha$ and $\beta$ can always be written in a way that gives the same final function}
\begin{itemize}
\item Condition 1:\\
$$f(0)\approx 0 \Longrightarrow \alpha<< \beta+e^{-\gamma(t)} \Longrightarrow$$
\begin{equation}\alpha-\beta<< e^{-\gamma t}\label{eq:1}\end{equation}
\item Condition 4:\\
$$f^{\prime\prime}(1.5)=0 \Longrightarrow$$
But every derivative will give a product of $\alpha$, $exp(-\gamma(x+t))$ and $(x+t)$... And only $x-t$ can get zero, Thus, by knowing that $x=1.5$:
\begin{equation}
t=-1.5
\end{equation}
\item Condition 2:\\
$$f(3)=2\Longrightarrow$$
\begin{equation}\label{eq:3}
\alpha=2\beta+2\cdot e^{-1.5\gamma}
\end{equation}
\item Condition 3:\\
Finally condition 3 gives:
$$f(1.5)=1\Longrightarrow$$
\begin{equation}
\alpha=\beta+1
\end{equation}
which combined with \eqref{eq:1} [if we want an errorof about 1\% units (the exp has to be about 100)in the last referred equation] gives:
$\gamma\approx3$
\begin{equation}
\gamma=3
\end{equation}
\end{itemize}
But Condition 2 (\eqref{eq:3})gives also:
$$\alpha=2\beta+2\cdot e^{-1.5\cdot3}\Longrightarrow$$
$$2\beta-\alpha=\beta-1=0.011\Longrightarrow$$
\begin{equation} \beta=1.011\end{equation}
And Finaly we got the function:
\begin{equation}f(x)=\frac{2.011}{(1.011+e^{-3(x-1.5)})}\end{equation}
\begin{tikzpicture}
\begin{axis}[grid=both,xmax=3.25,ymax=2.5]
\addplot[blue,domain=0.1:3,samples=300] {2.011/(1.011 + exp(-3*(x-1.5)))};
\end{axis}
\end{tikzpicture}
\end{document}
上述代码没有使用 pgfplots 或公式来生成此图,而是使用纯 tikz 曲线:
经过数学分析后得出以下公式:
两个代码(如果你不想搜索以上所有内容)都在这里:
\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\begin{center}
\begin{tikzpicture}[scale=2.5]
\draw[->,thick] (-0.2,0)--(3.7,0);%x axis
\draw[->,thick] (0,-0.2)--(0,2.7);%y axis
\foreach \y in {0,0.5,...,2.5}{
\draw[-,thin] (0.,\y)--(3.5,\y);
\node[left] at (0,\y) {\y};
}
\foreach \x in {0,0.5,...,3.5}{
\draw[-,thin] (\x,0)--(\x,2.5);
\node[below] at (\x,0) {\x};
}
\draw[-,blue!30!green] (0,0)to[out=5,in=225](1.5,1);
\draw[-,blue!30!green] (1.5,1)to[out=45,in=185](3,2);
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[grid=both,xmax=3.25,ymax=2.5]
\addplot[blue,domain=0.1:3,samples=300] {2.011/(1.011 + exp(-3*(x-1.5)))};
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}
结论:
正如您所看到的,所需的数学知识(即使公式的“类型”是“几乎已知的”)有时比简单的 latex 用户背景更复杂,而其他时候则足够简单。因此,如果您真的想“提取”实际公式……这些问题在某种程度上是题外话,可以在这里挖掘:https://math.stackexchange.com/
但是您(几乎)总是能够使用像上面在第一种方法中所做的那样的多条曲线并获得接近您预期的结果。
答案2
我知道你可能不喜欢这个答案,但恕我直言,你的程序有点太复杂了。到目前为止,我总是能够通过以下方式得出相当接近的近似值
使用初等函数,例如 sin、cos、tan、exp 等。
你也提到的语法
to[in=...,out=...]
,添加一些变换(是的,有时 Ti 的极坐标变换钾Z 手册会很方便)。
您输入的函数看起来非常像拉伸(1-cos)函数。
\documentclass[tikz,border=3.14pt]{standalone}
\begin{document}
\begin{tikzpicture}[>=latex]
\draw (0,0) grid[step=0.5] (3.5,2.5);
\draw [->] (0,0) --(3.7,0) node[below]{$x$};
\draw [->] (0,0) --(0,2.7) node[left]{$y$};
\draw[blue,thick] plot[variable=\x,domain=0:3] ({\x},{1-cos(deg(\x)*pi/3)});
\end{tikzpicture}
\end{document}
我发布此信息只是为了为您的答案提供另一种选择。
答案3
如果我们不知道公式,可以使用to[in=...,out=...]
。另一种可能性是使用贝塞尔曲线。
\documentclass[12pt]{article}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\pgfplotsset{cartesian/.style={%
grid = both,
minor tick num = 5,
minor grid style = {line width=.1pt,draw=gray!10},
axis lines = middle,
axis line style={-stealth},
xlabel style={below right},
ylabel style={above left},
xtick = {0,0.5,...,3.5},
ytick = {0,0.5,...,2.5},
xmin = 0,
xmax = 3.5,
ymin = 0,
ymax = 2.5,
width=1\linewidth,
height=1\linewidth,
}}
%===============================================================================
\begin{document}
\noindent
\begin{tikzpicture}
\begin{axis}[cartesian]
\draw[green!50!black] (0,0) to[out=0,in=185] (3,2);
\end{axis}
\end{tikzpicture}
\end{document}
答案4
我通常只是绘制一组点,因为我发现这是一种更简单的方法,然后尝试找出最终正确的数学函数 - 或者尝试绘制贝塞尔线。
现在你可能想知道如何获取点数据。为此,我使用Engauge 数字化仪工具(我只是一个用户,而不是创建者)。经过一些练习,您甚至可以使用手绘、扫描并生成要绘制的点。
如果您使用这种方法,您的编译时间可能会变得非常长,这就是我使用外部化方法的原因,因此只有当上下文发生变化时才会将图形构建为 PDF,而不是每次都这样。
我目前无法测试是否所有东西都正常工作,但这里是它的大概情况(也许我以后可以提供一个更好的例子):
- 使用 Engauge Digitizer 生成一个数据文件(本例中为“elekguete.txt”),如下所示:
elekguete.txt:
0 1521.74
2 882.31
3 806.24
4 749.92
5 558.59
6 438.69
7 424.90
8 361.17
9 346.71
10 311.49
11 287.47
12 273.80
13 261.48
14 246.07
15 229.86
16 212.86
17 205.15
18 196.04
然后我用来生成图表的文件看起来像这样(当然,只生成曲线图,不需要数学模型):
\tikzsetnextfilename{elekguete}
\begin{tikzpicture}
\begin{axis}[
width = 0.7\textwidth,
height = 0.35\textheight,
grid=major,
xmin = 0,
xmax = 18,
xlabel=Eintauchtiefe in \si{\mm},
ylabel=elektrische Güte,
legend style={anchor = north east, at={(0.98,0.98)}}
]
\addplot[color=red,mark=x, only marks] table[x index=0,y index=1,header=false]
{elekguete.txt};
\addplot+[color=blue, no marks, domain=0:18]{204.23 +
1290.7*exp(x/(-3.8778))};
\end{axis}
\end{tikzpicture}
在使用这些现在已经臃肿的设置时(我现在把它从文档中取出,就像我说的,我希望能找到时间制作一个更好、更简约的工作示例):
\usepackage{tikz}
\usetikzlibrary{positioning}
\usetikzlibrary{shapes}
\usetikzlibrary{shapes.geometric}
\usetikzlibrary{calc}
\usetikzlibrary{shapes.symbols}
\usetikzlibrary{shapes.multipart}
\usetikzlibrary{scopes}
\usetikzlibrary{decorations.shapes}
\usepackage{pgfplots}
\pgfplotsset{width=0.7\textwidth}
\pgfkeys{/pgf/number format/.cd,fixed,use comma,1000 sep={\,}}
\usetikzlibrary{external}
\tikzsetexternalprefix{externalize/}
\tikzset{external/system call={lualatex \tikzexternalcheckshellescape --source-specials -interaction=batchmode -jobname "\image" "\texsource"}}
\pgfkeys{/tikz/external/only named={true}}
\tikzexternalize