![各种各样的](https://linux22.com/image/388273/%E5%90%84%E7%A7%8D%E5%90%84%E6%A0%B7%E7%9A%84.png)
我是 LaTeX 的初学者,但我在边学边做。我想绘制以下线性规划问题:
(情节:制作图表)
f(x,y) = 2x - 3y -> max
x + y <= 12
x,y >=0
这是我目前所拥有的,但我找不到有关如何在 LaTeX 中绘制它的任何好答案:
\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{pgfplots}
\usepackage{tikz}
\usepackage{hyperref}
\usepackage[latin1]{inputenc}
\title{Linear Programming}
\date{12/27/18}
\begin{document}
\maketitle
\bigbreak
a) \begin{cases} f(x,y) = 2x - 3y \rightarrow max \\x + y \leq 12 \\ x , y \geq 0 \end{cases}
\bigbreak
\bigbreak
\begin{tikzpicture}
\begin{axis}
[
xmin=-20,xmax=20,
ymin=-20,ymax=20,
grid=both,
grid style={line width=.1pt, draw=darkgray!10},
major grid style={line width=.2pt,draw=darkgray!50},
axis lines=middle,
minor tick num=4,
enlargelimits={abs=0.5},
axis line style={latex-latex},
samples=100,
domain = -20:20,
]
\addplot[red] {x + y <= 12};
\end{axis}
\end{tikzpicture}
\end{document}
现在的问题是,当我应该绘制半平面时,我只绘制了线条(x + y <= 12,其中 x 和 y > 0,实际上应该是一个填充三角形)
我想要的是:
你能帮我吗?
答案1
您可以使用filldraw
vanilla 来tikz
绘制 + 填充区域。以下是示例
\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{pgfplots}
\usepackage{tikz}
\usepackage{hyperref}
\usepackage[latin1]{inputenc}
\pgfplotsset{compat=1.12}
\usetikzlibrary{patterns}
\title{Linear Programming}
\author{Random Numbers}
\date{12/27/18}
\begin{document}
\maketitle
\[
\begin{cases} f(x,y) = 2x - 3y \rightarrow max \\x + y \leq 12 \\ x , y \geq 0 \end{cases}
\]
\bigbreak
\bigbreak
\begin{tikzpicture}
\begin{axis}
[
xmin=-5,xmax=15,
ymin=-5,ymax=15,
grid=both,
grid style={line width=.1pt, draw=darkgray!10},
major grid style={line width=.2pt,draw=darkgray!50},
axis lines=middle,
minor tick num=4,
enlargelimits={abs=0.5},
axis line style={latex-latex},
samples=100,
domain = -20:20,
]
%\addplot[red] {x + y 12};
\filldraw[blue, pattern=north west lines, pattern color=blue] (0, 0) -- (0, 12) -- (12, 0) -- cycle;
\end{axis}
\end{tikzpicture}
\end{document}
答案2
非常类似于caverac 的回答很好除了这真正使用了一个函数之外,当约束更复杂时它也能起作用。
\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{patterns}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usepackage{hyperref}
\usepackage[latin1]{inputenc}
\title{Linear Programming}
\date{12/27/18}
\begin{document}
\maketitle
a) $\begin{cases} f(x,y) = 2x - 3y \rightarrow \text{max} \\x + y \leq 12 \\ x ,
y \geq 0 \end{cases}$
\begin{tikzpicture}
\begin{axis}
[
xmin=-20,xmax=20,
ymin=-20,ymax=20,
grid=both,
grid style={line width=.1pt, draw=darkgray!10},
major grid style={line width=.2pt,draw=darkgray!50},
axis lines=middle,
minor tick num=4,
enlargelimits={abs=0.5},
axis line style={latex-latex},
samples=100,
domain = -20:20,
]
\addplot[red,pattern=north east lines,pattern color=red] {12-x} -- (-20,-8) \closedcycle;
\end{axis}
\end{tikzpicture}
\end{document}
答案3
PSTricks 解决方案仅用于娱乐目的。
\documentclass[pstricks,border=12pt,12pt]{standalone}
\usepackage{pst-plot}
\begin{document}
\pspicture(-1,-1)(13,13)
\psaxes[Dx=2,Dy=2]{->}(0,0)(-1,-1)(12.5,12.5)[$x$,0][$y$,90]
\pspolygon[fillstyle=vlines,hatchsep=3pt,hatchangle=30](0,0)(12,0)(0,12)
\pcline[nodesep=-1](12,0)(0,12)
\endpspicture
\end{document}
各种各样的
\documentclass[pstricks,border=12pt]{standalone}
\def\RightPart
{
\psline(0.5,2.7)(!6.5 3.5 82 PtoCab)
\psbezier(1.2,1.3)(1.3,1.0)(2.0,1.0)
\psbezier(3.0,1.0)(3.0,2.2)(!6.5 3.5 73 PtoCab)
\psellipticarcn(6.5,3.5){(!6.5 3.5 73 PtoCab)}{(!6.5 3.5 73 neg PtoCab)}
\psbezier(4,-2)(4,0)(2.2,-1.8)
\psbezier(1.5,-1)(1,-1)(!6.5 3.5 -90 PtoCab)
}
\begin{document}
\begin{pspicture}(-7,-4)(7,4)
\pscustom[dimen=m,linejoin=0,fillstyle=vlines]
{
\RightPart
\reversepath
\scale{-1 1}
\RightPart
\closepath
}
\end{pspicture}
\end{document}