绘制凹凸函数

绘制凹凸函数

我想以类似于 Loring W. Tu 的书《流形导论》(第 129 页,图 13.4)中的方式绘制一个凹凸函数,但它总是不能按照我想要的方式工作。这是我的 MWE:

\documentclass[border=10pt]{standalone} 

\usepackage{pgfplots}
\usepackage{tikz}
\pgfplotsset{%
every x tick/.style={black, thick},
every y tick/.style={black, thick},
every tick label/.append style = {font=\footnotesize},
every axis label/.append style = {font=\footnotesize},
compat=1.12
  }
\begin{document}
\begin{tikzpicture}
\begin{axis}[xmin=-1.2, xmax=2, ymin=-0.7, ymax=1.2,

 xtick = {-1,0,1}, ytick = { 1},
 scale=0.4, restrict y to domain=-1.5:1.2,
 axis x line=center, axis y line= center,
 samples=40]


 \addplot[black, samples=100, smooth, domain=-1.2:0, thick]
   plot (\x, { 0 });

 \addplot[black, samples=100, smooth, domain=0:1, thick, label={x}]
   plot (\x, { exp( -1/\x)/(exp (-1/\x)+exp(1/(\x-1))) });


 \addplot[black, thick, samples=100, smooth, domain=1:2]
   plot (\x, {1} );

 \end{axis}
 \end{tikzpicture}

  \end{document}

我对这个结果的主要问题是,在 x=1 之前就已经达到了“稳定期”,这看起来确实不对。将样本大小更改为高于 100 会立即产生维度错误。有什么建议吗?

答案1

欢迎来到 TeX.SE!我没有那本书,但人们经常用tanh它。

\documentclass[border=10pt]{standalone} 

\usepackage{pgfplots}
\usepackage{tikz}
\pgfplotsset{%
every x tick/.style={black, thick},
every y tick/.style={black, thick},
every tick label/.append style = {font=\footnotesize},
every axis label/.append style = {font=\footnotesize},
compat=1.12
  }
\begin{document}
\begin{tikzpicture}
\begin{axis}[xmin=-1.2, xmax=2, ymin=-0.7, ymax=1.2,
 xtick = {-1,0,1}, ytick = { 1},
 scale=0.4, restrict y to domain=-1.5:1.2,
 axis x line=center, axis y line= center,
 samples=40]


 \addplot[black, samples=100, smooth, domain=-1.2:2, thick]
   plot (\x, {0.5*(1+tanh(5*(\x-0.5)))});
\end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

当然,您可以通过调整前因子(上面的 5)来改变步骤的宽度。

\documentclass[border=10pt,tikz]{standalone} 
\usepackage{pgfplots}
\pgfplotsset{%
every x tick/.style={black, thick},
every y tick/.style={black, thick},
every tick label/.append style = {font=\footnotesize},
every axis label/.append style = {font=\footnotesize},
compat=1.12
  }
\begin{document}
\foreach \X in {2,2.2,...,6,5.8,5.6,...,2.2}
{\begin{tikzpicture}
\begin{axis}[xmin=-1.2, xmax=2, ymin=-0.7, ymax=1.2,
 xtick = {-1,0,1}, ytick = { 1},
 scale=0.4, restrict y to domain=-1.5:1.2,
 axis x line=center, axis y line= center,
 samples=40,
 title={$f(x)=\left[1+\tanh\bigl(
 \pgfmathprintnumber[precision=1,fixed,zerofill]{\X}(x-1/2)\bigr)\right]/2$}]

 \addplot[black, samples=100, smooth, domain=-1.2:2, thick]
   plot (\x, {0.5*(1+tanh(\X*(\x-0.5)))});
\end{axis}
\end{tikzpicture}}
\end{document}

在此处输入图片描述

答案2

提供的答案中的情节看起来不像我理解的那样函数;相反,所示函数的导数的图将是凹凸函数。以下直接生成一个凹凸函数图,支持区间为 $[-1,1]$:

\documentclass[border=10pt]{standalone} 

\usepackage{pgfplots}
\usepackage{tikz}
\pgfplotsset{%
  every x tick/.style={black, thin},
  every y tick/.style={black, thick},
  every tick label/.append style = {font=\footnotesize},
  every axis label/.append style = {font=\footnotesize},
  compat=1.12
 }
\begin{document}
\begin{tikzpicture}
\begin{axis}[xmin=-1.2, xmax=1.2, ymin=-0.2, ymax=1.2,
  xtick = {-1,0,1}, ytick = { 1},
  scale=0.4, restrict y to domain=-0.2:1.2,
  axis x line=center, axis y line= center,
  samples=40]
\addplot[black, samples=100, smooth, domain=-1.2:-1, thick]
   plot (\x, { 0 });
\addplot[black, samples=100, smooth, domain=-1:1, thick, label={x}]
   plot (\x, {exp(1-1/(1-x^2)});
\addplot[black, thick, samples=100, smooth, domain=1:1.2]
   plot (\x, {0} );
\end{axis}
\end{tikzpicture}

\end{document}

撞击功能

(我不确定如何避免图中 $x=-1$ 右侧的明显间隙。)

相关内容