LaTeX 中的阴影区域

LaTeX 中的阴影区域

我有以下 LP

$$
\begin{array}{cc}
   \min & 15x+20y \\
   \text{subject to:} \\
   & x+2y\geq 10 \\
   & 2x-3y\leq 6 \\
   & x+y\geq 6 \\
   & x,y\geq0
\end{array}
$$

我想要绘制可行区域。

目前,我只绘制了线条,这是我的代码:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
    \draw[black,thick,-latex] (-.5,0) -- (11,0)
    node[right] {$x$};
    
    \foreach \x/\xtext in {1/1, 2/2, 3/3, 4/4, 5/5, 6/6, 7/7, 8/8, 9/9, 10/10}
    \draw[shift={(\x,0)},black] (0pt,2pt)--(0pt,-2pt) node[below] {$\xtext$};
    
    \draw[black,thick,-latex] (0,-.5) -- (0,6.5)
    node[above] {$y$};
    
    \foreach \y/\ytext in {1/1, 2/2, 3/3, 4/4, 5/5, 6/6}
    \draw[shift={(0,\y)},black] (2pt,0pt)--(-2pt,0pt) node[left] {$\ytext$};
    
    \draw[black,thick,-] (6,0) -- (0,6)
    node[black] at (2,2.5) {$x+y=6$};
    
    \draw[black,thick,-] (0,5) -- (10,0)
    node[black] at (10,1) {$x+2y=10$};
    
    \draw[black,thick,-] (3,0) -- (10,14/3)
    node[black] at (10,3.5) {$2x-3y=6$};
    
\end{tikzpicture}
\end{document}

我怎样才能阴影化代表可行区域的区域?

答案1

您需要找到定义可行区域的交点。您可以手动完成此操作,或者在本例中借助网格(因为坐标是整数)。或者,就像我的例子一样,您可以让 TiZ 为您完成工作。

代码(我稍微简化了一下):

\documentclass[tikz,border=2mm]{standalone}

\begin{document}
\begin{tikzpicture}[thick]
    % coordinates
    \coordinate (A1) at  (6,0);    %  x+ y= 6
    \coordinate (A2) at  (0,6);    %  x+ y= 6
    \coordinate (B1) at  (0,5);    %  x+2y=10
    \coordinate (B2) at (10,0);    %  x+2y=10
    \coordinate (C1) at  (3,0);    % 2x-3y= 6
    \coordinate (C2) at (11,16/3); % 2x-3y= 6
    \coordinate (I)  at (intersection of B1--B2 and C1--C2); % <-- intersection point
    % grid (you can remove it)
    \draw[help lines] (0,0) grid (10.99,6.5);
    % feasible region
    \fill[red,opacity=0.3] (11,0) -- (B2) -- (I) -- (C2);
    % axes
    \draw[-latex] (-0.5,0) -- (11,0) node[right] {$x$};
    \foreach \x in {1,...,10}
      \draw[shift={(\x,0)}] (0pt,2pt)--(0pt,-2pt) node[below] {$\x$};
    \draw[-latex] (0,-0.5) -- (0,6.5) node[above] {$y$};
    \foreach \y in {1,...,6}
      \draw[shift={(0,\y)}] (2pt,0pt)--(-2pt,0pt) node[left] {$\y$};
    % constraints
    \draw (A1) -- (A2) node[black] at (2,2.5)  {$x+y=6$};
    \draw (B1) -- (B2) node[black] at (10,1)   {$x+2y=10$};
    \draw (C1) -- (C2) node[black] at (10,3.5) {$2x-3y=6$};
\end{tikzpicture}
\end{document}

输出结果如下: 在此处输入图片描述

相关内容