如何为 tikz 生成的这个区域着色?

如何为 tikz 生成的这个区域着色?

我正在使用 xelatex,我想为下面代码中 y=f(x)、y=g(x) 和线 x=a 和 x=b 所包围的区域着色(如下图所示)。我该怎么做?

\documentclass{article}
\usepackage{tikz} 
\usetikzlibrary{arrows}
\begin{document}

\begin{tikzpicture}
\draw [->,>=triangle 45] (-2,0) -- (5,0) node [right] {$x$};
\draw [->,>=triangle 45] (0,-2.2) -- (0,3.2) node [above] {$y$};
\draw[very thick] (-1.5,.5) to [out=300,in=180] (-.5,-.5)
to [out=0,in=180] (3,2.5)
to [out=360,in=140] (4.5,2) ;
\draw[very thick] (-1.5,-1.5) to [out=350,in=240] (4.5,1) ;
\draw[very thick] (4.2,0)--(4.2,2.2);
\node at (4.2,0) [below] {$b$};
\draw[very thick] (-1,0)--(-1,-1.55);
\node at (-1,0) [above] {$a$};
\node at (1.5,2.2) [above] {$y=f(x)$};
\node at (2,-1.5) [right] {$y=g(x)$};
\end{tikzpicture}

\end{document}

在此处输入图片描述

答案1

在此处输入图片描述

\documentclass{article}
\usepackage{tikz} 
\usetikzlibrary{arrows}
\begin{document}

\begin{tikzpicture}
\begin{scope}
\clip(-1,-5)rectangle(4.2,5);
\clip (-1.5,.5) to [out=300,in=180] (-.5,-.5)
to [out=0,in=180] (3,2.5)
to [out=360,in=140] (4.5,2) --++(0,-5)--(-1,-5)--cycle;
\fill[red] (-1.5,-1.5) to [out=350,in=240] (4.5,1)--++(0,5)--(-1,5)--cycle;
\end{scope}
\draw [->,>=triangle 45] (-2,0) -- (5,0) node [right] {$x$};
\draw [->,>=triangle 45] (0,-2.2) -- (0,3.2) node [above] {$y$};
\draw[very thick] (-1.5,.5) to [out=300,in=180] (-.5,-.5)
to [out=0,in=180] (3,2.5)
to [out=360,in=140] (4.5,2) ;
\draw[very thick] (-1.5,-1.5) to [out=350,in=240] (4.5,1) ;
\draw[very thick] (4.2,0)--(4.2,2.2);
\node at (4.2,0) [below] {$b$};
\draw[very thick] (-1,0)--(-1,-1.55);
\node at (-1,0) [above] {$a$};
\node at (1.5,2.2) [above] {$y=f(x)$};
\node at (2,-1.5) [right] {$y=g(x)$};
\end{tikzpicture}

\end{document}

您可以使用命名坐标和重用路径来使代码变得更好。

我还没有找到一种自动确定曲线极值的方法,所以路径附录

--++(0,-5)--(-1,-5)--循环

--++(0,5)--(-1,5)--循环

可以用自动正确的值替换。

相关内容