我想以與 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$ 右側的明顯間隙。)