我如何填充由函数给出边界的区域?

我如何填充由函数给出边界的区域?

我定义了一个由四个函数界定的区域。我找到了相关的答案,例如:“用阴影填充两个函数之间的区域“,但我无法适应这个解决方案。

这里是例子:

% !TEX program  = pdflatex
% !TEX encoding = UTF-8 Unicode
\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{amsmath}
\begin{document}
\begin{tikzpicture}
\begin{axis}[axis equal image,
width=10cm,
axis lines=middle,
axis line style={-latex},
clip =false,
xmin=0,xmax=2.45,
ymin=0,
no markers,
xlabel={$x$},ylabel={$y$},
every axis x label/.append style={anchor=north},
every axis y label/.append style={anchor=east},
]
\addplot[domain=0.4:1.2,thick] { 1/x } node[anchor=135] {$f(x)=\dfrac{1}{x}$};
\addplot[domain=.6:2,thick] { 2/x } node[right] {$f(x)=\dfrac{2}{x}$};
\addplot[domain=0.7:1.5,thick] { x } node[right] {$f(x)=x$};
\addplot[domain=0.4:.8,thick] { 4*x } node[right] {$f(x)=4x$};


%\addplot+[domain=0.5:1] { 1/x };
%\addplot+[domain=1:sqrt(2)] { x };
%\addplot+[domain=1/sqrt(2):sqrt(2)] { 2/x };
%\addplot+[domain=0.5:1/sqrt(2)] { 4*x };
\end{axis}
\end{tikzpicture}
\end{document}

结果是:

在此处输入图片描述

我想用垂直线或者简单的灰色填充该区域。

答案1

我找到了一个方法。

环境需要选项area style来绘制正确的轴。

现在我使用命令 closedcycle 再次绘制每个函数,其中上面的函数用灰色填充,下面的函数用白色填充。(绘制)。

% !TEX program  = pdflatex
% !TEX encoding = UTF-8 Unicode
\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{amsmath}
\begin{document}
\begin{tikzpicture}
\begin{axis}[axis equal image,
width=10cm,
axis lines=middle,
axis line style={-latex},
clip =false,
xmin=0,xmax=2.45,
ymin=0,
no markers,
xlabel={$x$},ylabel={$y$},
every axis x label/.append style={anchor=north},
every axis y label/.append style={anchor=east},
area style,
]

\addplot+[domain=1/sqrt(2):sqrt(2),fill,gray] { 2/x }  \closedcycle;
\addplot+[domain=0.5:1/sqrt(2),fill,gray] { 4*x }  \closedcycle;
\addplot+[domain=0.5:1,fill,white] { 1/x } \closedcycle;
\addplot+[domain=1:sqrt(2),fill,white] { x }  \closedcycle;

\addplot[domain=0.4:1.2,thick] { 1/x } node[anchor=135] {$f(x)=\dfrac{1}{x}$};
\addplot[domain=.6:2,thick] { 2/x } node[right] {$f(x)=\dfrac{2}{x}$};
\addplot[domain=0.7:1.5,thick] { x } node[right] {$f(x)=x$};
\addplot[domain=0.4:.8,thick] { 4*x } node[right,] {$f(x)=4x$};

\node at (axis cs:1,1.5) {$D$};

\end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

tkz-fct 无法正确回答,但使用 tikz 相对容易。在“剪辑”之前,我使用 tkz-fct 的宏来用 gnuplot 绘制图形。

在此处输入图片描述

\documentclass{scrartcl}
\usepackage{tkz-fct}   
\begin{document}

\begin{tikzpicture}[xscale=2,yscale=2,line width=0.6pt,>=latex]
  \tkzInit[xmax=3,ymax=3] 
  \tkzGrid
  \tkzAxeXY 
  \tkzFct[domain=0.5:2]{2/x}   
  \tkzFct[domain=0.2:1.5]{1/x}  
  \tkzFct[domain=0.2:1]{4*x} 
  \tkzFct[domain=0.5:1.5]{x}  
  \clip (0.5,0) -- plot[domain=0.5:2] function{4*x} -- (2,0)--cycle;
  \clip (0.5,3) -- plot[domain=0.5:2] function{x}   -- (2,3)--cycle;
  \clip (0.5,0) -- plot[domain=0.5:2] function{2/x} -- (2,0)--cycle;
  \clip (0.5,3) -- plot[domain=0.5:2] function{1/x} -- (2,3)--cycle;     
  \fill[blue!20,fill opacity=.5] (0.5,0) rectangle (2,3);
\end{tikzpicture}  
\end{document} 

使用 tkz-fct 后,最后步骤的答案会更简短:

\begin{tikzpicture}[xscale=2,yscale=2,line width=0.6pt,>=latex]
  \tkzInit[xmax=3,ymax=3] 
  \tkzGrid
  \tkzAxeXY 
  \tkzFct[domain=0.5:2]{2/x}   
  \tkzFct[domain=0.2:1.5]{1/x}  
  \tkzFct[domain=0.2:1]{4*x} 
  \tkzFct[domain=0.5:1.5]{x}  
  \clip (0.5,0) -- plot[domain=0.5:2] function{4*x} -- (2,0)--cycle;
  \clip (0.5,3) -- plot[domain=0.5:2] function{x}   -- (2,3)--cycle;  
  \tkzDrawAreafg[between= a and b,color = blue!20,domain = 0.5:1.5]    
\end{tikzpicture}

相关内容