我对 LaTeX 非常陌生,一直在尝试弄清楚如何绘制函数 2^(x/8)-x 的图形。当我使用时,pgfplots
结果是这样的:
\usepackage{pgfplots}
\begin{tikzpicture}
\begin{axis}[
grid=both,
axis lines = middle]
\addplot {(2)^(x/8)-x};
\end{axis}
\end{tikzpicture}
这与WolframAlpha 显示(我正在努力实现的不平等版本):
我的最终目标是拥有一个与 WolframAlpha 图(带有不等式阴影区域)相似的 LaTeX 图,并且我知道我需要修改域和一些其他样式,现在的主要问题是我甚至无法正确绘制方程式。
我究竟做错了什么?
顺便说一句,如果我能以某种方式自动打印出表中 x(或 n)值的 y 小于 0 的整数坐标,那就太好了。
答案1
问题是pgfplots
使用默认域 -5,5,并且在此域中函数的图形似乎是一条直线。提供适当的域(和samples
值):
\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
grid=both,
axis lines = middle,domain=-70:70,ymin=-200,samples=100]
\addplot[blue,no marks] {(2)^(x/8)-x};
\end{axis}
\end{tikzpicture}
\end{document}
使用以下代码,您可以获得曲线与 x 轴相交点的 x 坐标:
\documentclass{article}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{calc}
\newcommand\xcoord[2][center]{{%
% The actual point of interest
\pgfpointanchor{#2}{#1}%
\pgfgetlastxy{\ix}{\iy}%
% (0,0)
\pgfplotspointaxisxy{0}{0}%
\pgfgetlastxy{\ox}{\oy}
% (1,1)
\pgfplotspointaxisxy{1}{1}%
\pgfgetlastxy{\ux}{\uy}
\pgfmathparse{(\ix-\ox)/(\ux-\ox)}
\pgfmathprintnumber{\pgfmathresult}}
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
grid=both,
axis lines = middle,
domain=-70:70,
ymin=-100,
ymax=100,
samples=100
]
\addplot[blue,no marks,name path=curve] {(2)^(x/8)-x};
\addplot[draw=none,name path=xaxis,forget plot] {0};
\path[name intersections={of=curve and xaxis,by={a,b}}]
node[circle,fill=red!70!black,inner sep=1.5pt,pin={[red!70!black]-85:\xcoord{a}}] at (a) {}
node[circle,fill=red!70!black,inner sep=1.5pt,pin={[red!70!black]130:\xcoord{b}}] at (b) {};
\addplot[fill=none] fill between[
of=curve and xaxis,
split,
every segment no 1/.style={fill,blue!20}];
\end{axis}
\end{tikzpicture}
\end{document}
使用循环可以得到所需的表:
\documentclass{article}
\usepackage{pgfplots}
\usepackage{multicol}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{calc}
\pgfmathdeclarefunction{myfunc}{1}{%
\pgfmathparse{2^(#1/8)-#1}%
}
\newcommand\xcoord[2][center]{{%
% The actual point of interest
\pgfpointanchor{#2}{#1}%
\pgfgetlastxy{\ix}{\iy}%
% (0,0)
\pgfplotspointaxisxy{0}{0}%
\pgfgetlastxy{\ox}{\oy}
% (1,1)
\pgfplotspointaxisxy{1}{1}%
\pgfgetlastxy{\ux}{\uy}
\pgfmathparse{(\ix-\ox)/(\ux-\ox)}
\pgfmathprintnumber{\pgfmathresult}}
}
\begin{document}
\begin{center}
\begin{tikzpicture}
\begin{axis}[
grid=both,
axis lines = middle,
domain=-70:70,
ymin=-100,
ymax=100,
samples=100,
]
\addplot[blue,no marks,name path=curve] {myfunc(x)};
\addplot[draw=none,name path=xaxis,forget plot] {0};
\path[name intersections={of=curve and xaxis,by={a,b}}]
node[circle,fill=red!70!black,inner sep=1.5pt,pin={[red!70!black]-85:\xcoord{a}}] at (a) {}
node[circle,fill=red!70!black,inner sep=1.5pt,pin={[red!70!black]130:\xcoord{b}}] at (b) {};
\addplot[fill=none] fill between[
of=curve and xaxis,
split,
every segment no 1/.style={fill,blue!20}];
\end{axis}
\end{tikzpicture}
\end{center}
\begin{multicols}{2}
\par\noindent
\foreach \Value in {-50,...,50}
{%
\pgfmathparse{myfunc(\Value)}%
\ifdim\pgfmathresult pt <0pt\relax
$(\Value, \pgfmathparse{myfunc(\Value)}\pgfmathprintnumber[precision=2]{\pgfmathresult})$\par\noindent
\fi
}
\end{multicols}
\end{document}