為什麼我在 tikz 中的填充會產生奇怪的結果?

為什麼我在 tikz 中的填充會產生奇怪的結果?

我試圖填充一些線性函數之間的區域。但最終的填充看起來很奇怪。以下是代碼:


\usepackage{amsmath}  
\usepackage{systeme}      
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}


\pgfplotsset{compat=1.16}
   

\begin{document}

\section{Problem 2:}
Because all the constraints as well as the function to maximize are linear, by the graphical method, we only need to check corners of the feasible region, which looks like: 

\bigskip

\begin{tikzpicture}
\begin{axis}[
    axis lines = left,
    xlabel = $x_1$,
    ylabel = $x_2$,
    ymin=0, ymax=10
]
%Below the first constraint is defined
\addplot [name path=red, 
    domain= 0:15, 
    samples=100, 
    color=red,
]
{(x -4)/2};

%Here the second constraint is defined
\addplot [name path=blue, 
    domain=0:15, 
    samples=100, 
    color=blue,
    ]
{3+x};

%Here the third constraint is defined
\addplot [name path=green, 
domain= 0:15, 
    samples=100, 
    color=green,
    ]
{2};

\path[name path=redgreen,
    intersection segments={of=red and green,
    sequence=A1 -- B0,},
];

\addplot [gray] fill between[of = blue and redgreen]; 



\end{axis}
\end{tikzpicture}


\end{document}

這些代碼產生

在此輸入影像描述

有人可以解釋發生了什麼並嘗試修復它嗎?我只想填滿紅線、綠線和藍線之間的區域。非常感謝大家!

答案1

我假設文檔類為文章,因為沒有提到任何內容

在此輸入影像描述

\documentclass{article}
\usepackage{amsmath}  
\usepackage{systeme}      
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}


\pgfplotsset{compat=1.16}
   

\begin{document}

\section{Problem 2:}
Because all the constraints as well as the function to maximize are linear, by the 
 graphical method, we only need to check corners of the feasible region, which looks 
like: 

\bigskip

\begin{tikzpicture}
\begin{axis}[
    axis lines = left,
    xlabel = $x_1$,
    ylabel = $x_2$,
    ymin=0, ymax=10
]
%Below the first constraint is defined
\addplot [name path=red, 
    domain= 0:15, 
    samples=100, 
    color=red,
]
{(x -4)/2};

%Here the second constraint is defined
\addplot [name path=blue, 
    domain=0:15, 
    samples=100, 
    color=blue,
    ]
{3+x};

%Here the third constraint is defined
\addplot [name path=green, 
domain= 0:15, 
    samples=100, 
    color=green,
    ]
{2};

\path [
    name path=gr,
    intersection segments={of=green and red,
    sequence={L1 -- R2}%------the first segment of green and second segment of red
}];

\addplot [gray] fill between[of = blue and gr]; 
\end{axis}
\end{tikzpicture}


\end{document}

相關內容