1

How to plot the 3D graph of the following three inequalities in latex (tikz/pgf) ?

z <= x+y+1,

z <= 4x,

z <= 5y.


My attempt:

\documentclass[12pt,leqno]{amsart}
\usepackage{pgfplots}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture} \begin{axis} \addplot3 [ domain=-5:20, domain y = -3:10, samples = 20, samples y = 8, surf] {x+y+1}; \addplot3[domain=-5:20, domain y = -3:10, samples = 20, samples y = 8, surf, opacity=0.25]{4x}; \addplot3[domain=-5:20, domain y = -3:10, samples = 20, samples y = 8, surf, opacity=0.25]{5y}; \end{axis} \end{tikzpicture} \end{document}

The above Latex code produces the above figure: enter image description here

But the above graph is not clear, the intersecting line of the three half-planes are not clear.

Any help to draw clear image with clear intersection.

Any help both with the 3D graph of the given 3 inequalities.

Thanks

Edit:

The 2D projection of the above 3 inequalities becomes:

enter image description here

and this is justified by the answer of @Juan Castaño.

learner
  • 663

2 Answers2

7

This is not very difficult to draw with tikz (and it looks better than with pgfplots in this case IMHO). The main problem is a maths problem: finding all the intersection points and lines. And then you have to split the planes and order each piece to have the visibility right.

Something like this:

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

\tikzset {% plane 1/.style={thick,blue,fill=cyan!20,fill opacity=0.9}, % z=x+y+1 plane 2/.style={thick,green!40!black,fill=green!20,fill opacity=0.9}, % z=4x plane 3/.style={thick,brown,fill=yellow!20,fill opacity=0.9}, % z=5y inter/.style ={thick,red}, % intersection lines }

\begin{document} \begin{tikzpicture} [% x={(-0.4cm,-0.2cm)},y={(0.8cm,-0.3cm)},z={(0cm,0.4cm)},% line cap=round,line join=round% ] % z=x+y+1 \coordinate (A1) at (0,0,1); \coordinate (A2) at (4,0,5); \coordinate (A3) at (4,4,9); \coordinate (A4) at (0,4,5); % z=4x \coordinate (O) at (0,0,0); \coordinate (B2) at (4,0,16); \coordinate (B3) at (4,4,16); \coordinate (B4) at (0,4,0); % z=5y \coordinate (C2) at (4,0,0); \coordinate (C3) at (4,4,20); \coordinate (C4) at (0,4,20); % intersection points \coordinate (P) at (4,3.2,16); \coordinate (Q) at (4,1.25,6.25); \coordinate (R) at (5/11,4/11,20/11); \coordinate (S) at (5/3,4,20/3); \coordinate (T) at (0,0.25,1.25); \coordinate (U) at (1/3,0,4/3); % projection points \coordinate (V) at (4,4,0); \coordinate (W) at (5/11,4/11,0); \coordinate (X) at (4,1.25,0); \coordinate (Y) at (5/3,4,0); % axes and dashed lines \draw[dashed] (4,0,0) -- (B2); \draw[dashed] (0,4,0) -- (C4); \draw[thick,-latex] (O) -- (5,0,0) node [left] {$x$}; \draw[thick,-latex] (O) -- (0,5,0) node [right] {$y$}; \draw[thick,-latex] (O) -- (0,0,15) node [above] {$z$}; % planes and intersection lines \draw[plane 1] (A1) -- (U) -- (R) -- (T) -- cycle; \draw[plane 3] (O) -- (P) -- (C3) -- (C4) -- cycle; \draw[plane 1] (T) -- (A4) -- (S) -- (R) -- cycle; \draw[plane 2] (O) -- (B2) -- (B3) -- (B4) -- cycle; \draw[plane 1] (U) -- (A2) -- (Q) -- (R) -- cycle; \draw[plane 3] (O) -- (C2) -- (C3) -- (P) -- cycle; \draw[inter] (O) -- (P); \draw[plane 1] (Q) -- (R) -- (S) -- (A3) -- cycle; \draw[inter] (Q) -- (R) -- (S); % projection \draw[fill=gray!20] (W) -- (Y) -- (V) -- (X) -- cycle; \draw[fill=gray!30] (O) -- (B4) -- (Y) -- (W) -- cycle; \draw[fill=gray!40] (O) -- (W) -- (X) -- (C2) -- cycle; % more dashed lines \draw[dashed] (V) -- (C3); \draw[dashed] (W) -- (R); \draw[dashed] (X) -- (Q); \draw[dashed] (Y) -- (S); % labels \node[blue] at (A4) [right] {$z=x+y+1$}; \node[green!40!black] at (B2) [above] {$z=4x$}; \node[brown] at (C4) [above] {$z=5y$}; \fill[red] (R) circle (1pt) node [below right] {$\left(\frac{5}{11},\frac{4}{11},\frac{20}{11}\right)$}; \end{tikzpicture} \end{document}

enter image description here

Edit 1: This is the 2D projection. It's essentially the same code from above (labeled 'projection') but removing the planes, lines and the third coordinate in each point.

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

\tikzset {% plane 1/.style={fill=cyan!20 ,fill opacity=0.9}, % z=x+y+1 plane 2/.style={fill=green!20 ,fill opacity=0.9}, % z=4x plane 3/.style={fill=yellow!20,fill opacity=0.9}, % z=5y }

\begin{document} \begin{tikzpicture}[thick,line cap=round,line join=round] % z=4x \coordinate (O) at (0,0); \coordinate (B4) at (0,4); % z=5y \coordinate (C2) at (4,0); % projection points \coordinate (V) at (4,4); \coordinate (W) at (5/11,4/11); \coordinate (X) at (4,1.25); \coordinate (Y) at (5/3,4); % axes \draw[thick,-latex] (O) -- (5,0) node [right] {$x$}; \draw[thick,-latex] (O) -- (0,5) node [above] {$y$}; % projection \draw[plane 1] (W) -- (Y) -- (V) -- (X) -- cycle; \draw[plane 2] (O) -- (B4) -- (Y) -- (W) -- cycle; \draw[plane 3] (O) -- (W) -- (X) -- (C2) -- cycle; % labels \node at (2.5,2.5) {$z=x+y+1$}; \node at (0.7,3.5) {$z=4x$}; \node at (3 ,0.5) {$z=5y$}; \fill[red] (W) circle (1pt) node [above right] {$\left(\frac{5}{11},\frac{4}{11},\frac{20}{11}\right)$}; \end{tikzpicture} \end{document}

enter image description here

Edit 2: following the OP comments I joined the two pictures using subcaptions. Depending of the document geometry it probably will need changes of scales or widths.

Sorry for the duplicated code:

\documentclass{article}
\usepackage   {caption}
\usepackage   {lipsum}    % dummy text
\usepackage   {showframe} % just for the example
\usepackage   {tikz}

\tikzset {% plane 1/.style={thick,blue,fill=cyan!20,fill opacity=0.9}, % z=x+y+1 plane 2/.style={thick,green!40!black,fill=green!20,fill opacity=0.9}, % z=4x plane 3/.style={thick,brown,fill=yellow!20,fill opacity=0.9}, % z=5y inter/.style ={thick,red}, % intersection lines }

\begin{document} \lipsum[1]

\begin{figure}[h]\centering \begin{minipage}[b]{0.54\textwidth}\centering % b = bottom alignment \begin{tikzpicture} [% scale=0.6, % <-- added x={(-0.4cm,-0.2cm)},y={(0.8cm,-0.3cm)},z={(0cm,0.4cm)},% line cap=round,line join=round% ] % z=x+y+1 \coordinate (A1) at (0,0,1); \coordinate (A2) at (4,0,5); \coordinate (A3) at (4,4,9); \coordinate (A4) at (0,4,5); % z=4x \coordinate (O) at (0,0,0); \coordinate (B2) at (4,0,16); \coordinate (B3) at (4,4,16); \coordinate (B4) at (0,4,0); % z=5y \coordinate (C2) at (4,0,0); \coordinate (C3) at (4,4,20); \coordinate (C4) at (0,4,20); % intersection points \coordinate (P) at (4,3.2,16); \coordinate (Q) at (4,1.25,6.25); \coordinate (R) at (5/11,4/11,20/11); \coordinate (S) at (5/3,4,20/3); \coordinate (T) at (0,0.25,1.25); \coordinate (U) at (1/3,0,4/3); % projection points \coordinate (V) at (4,4,0); \coordinate (W) at (5/11,4/11,0); \coordinate (X) at (4,1.25,0); \coordinate (Y) at (5/3,4,0); % axes and dashed lines \draw[dashed] (4,0,0) -- (B2); \draw[dashed] (0,4,0) -- (C4); \draw[thick,-latex] (O) -- (5,0,0) node [left] {$x$}; \draw[thick,-latex] (O) -- (0,5,0) node [right] {$y$}; \draw[thick,-latex] (O) -- (0,0,15) node [above] {$z$}; % planes and intersection lines \draw[plane 1] (A1) -- (U) -- (R) -- (T) -- cycle; \draw[plane 3] (O) -- (P) -- (C3) -- (C4) -- cycle; \draw[plane 1] (T) -- (A4) -- (S) -- (R) -- cycle; \draw[plane 2] (O) -- (B2) -- (B3) -- (B4) -- cycle; \draw[plane 1] (U) -- (A2) -- (Q) -- (R) -- cycle; \draw[plane 3] (O) -- (C2) -- (C3) -- (P) -- cycle; \draw[inter] (O) -- (P); \draw[plane 1] (Q) -- (R) -- (S) -- (A3) -- cycle; \draw[inter] (Q) -- (R) -- (S); % projection \draw[fill=gray!20] (W) -- (Y) -- (V) -- (X) -- cycle; \draw[fill=gray!30] (O) -- (B4) -- (Y) -- (W) -- cycle; \draw[fill=gray!40] (O) -- (W) -- (X) -- (C2) -- cycle; % more dashed lines \draw[dashed] (V) -- (C3); \draw[dashed] (W) -- (R); \draw[dashed] (X) -- (Q); \draw[dashed] (Y) -- (S); % labels \node[blue] at (A4) [right] {$z=x+y+1$}; \node[green!40!black] at (B2) [above] {$z=4x$}; \node[brown] at (C4) [above] {$z=5y$}; \fill[red] (R) circle (1pt) node [below right] {$\left(\frac{5}{11},\frac{4}{11},\frac{20}{11}\right)$}; \end{tikzpicture} \caption{Subpicture 1}\label{fig:figA} \end{minipage} \begin{minipage}[b]{0.44\textwidth}\centering % b = bottom alignment \begin{tikzpicture} [% scale=0.8, % <-- added thick,line cap=round,line join=round ] % z=4x \coordinate (O) at (0,0); \coordinate (B4) at (0,4); % z=5y \coordinate (C2) at (4,0); % projection points \coordinate (V) at (4,4); \coordinate (W) at (5/11,4/11); \coordinate (X) at (4,1.25); \coordinate (Y) at (5/3,4); % axes \draw[thick,-latex] (O) -- (5,0) node [right] {$x$}; \draw[thick,-latex] (O) -- (0,5) node [above] {$y$}; % projection \draw[plane 1] (W) -- (Y) -- (V) -- (X) -- cycle; \draw[plane 2] (O) -- (B4) -- (Y) -- (W) -- cycle; \draw[plane 3] (O) -- (W) -- (X) -- (C2) -- cycle; % labels \node at (2.6,2.5) {$z=x+y+1$}; % <-- changed \node at (0.7,3.5) {$z=4x$}; \node at (3 ,0.3) {$z=5y$}; % <-- changed \fill[red] (W) circle (1pt) node [above right] {$\left(\frac{5}{11},\frac{4}{11},\frac{20}{11}\right)$}; \end{tikzpicture} \caption{Subpicture 1}\label{fig:figB} \end{minipage} \end{figure}

\lipsum[2] \end{document}

enter image description here

Juan Castaño
  • 28,426
  • Thanks. I think the three planes meet at the point (5/11,4/11,9/11), which is the point where the two red lines and one yellow line meet. Am I right ? – learner Jul 09 '21 at 12:54
  • can you please help with the 2D projection of the above graph in z=0 plane ? As you see in the Edit, that the 2D projection matches with your plot in xy-plane. – learner Jul 09 '21 at 13:05
  • @M.A.SARKAR, the intersection point is, if I'm not wrong, (5/11,4/11,20/11) which is indeed where the red lines meet. For the other comment, you can draw the 2D projection reusing the code thus labeled. You only need to remove the third coordinate of each point W,Y,V,X,... – Juan Castaño Jul 09 '21 at 13:06
  • ok, I will try. But can you label the point where the red lines meet by pointing the coordinates there ? – learner Jul 09 '21 at 13:08
  • @M.A.SARKAR, I just made an edit with the 2D projection and the intersection point. – Juan Castaño Jul 09 '21 at 13:30
  • Great. Thank you very much. You plotted exactly what I was expecting. – learner Jul 09 '21 at 13:33
  • Castano, I wish to place the two figures above side-by-side. I have used \begin{center} (1st tikz code) \qquad (2nd Tikz code) \end{center}. This places the above two figures side by side by using the command \qquad. But how to write captions below each figure ? Can you demonstrate the way in form of an answer ? – learner Aug 26 '21 at 15:59
  • @M.A.SARKAR, you can do something like this: https://tex.stackexchange.com/questions/612112/side-by-side-pictures-on-a-landscape-page. Put the tikzpictures instead of the \includegrahics.... – Juan Castaño Aug 26 '21 at 16:30
  • But I am not including graphics, I am plotting with Tikz. So \includegraphics would not help. I have got something here-https://tex.stackexchange.com/questions/45991/customizing-caption-in-side-by-side-figures Which I have tried to use in the following answer. – learner Aug 26 '21 at 17:17
  • @M.A.SARKAR, just change \includegraphics... for \begin{tikzpicture}...\end{tikzpicture}. – Juan Castaño Aug 26 '21 at 17:21
  • Can you please check the following code in my own answer ? This is giving side by side figures but the captions levels are not in same horizontal lines. Can you please run it ? I just need to get the caption levels along same horizontal lines – learner Aug 26 '21 at 17:30
  • @M.A.SARKAR, I made an edit. Hope that helps. – Juan Castaño Aug 26 '21 at 18:06
  • Yes that helps. But I don;t need the overall caption instead I want to get numerical numberings in place of (a) and (b) e.g., Figure 1 and Figure 2. Is it possible to do ? – learner Aug 26 '21 at 18:31
  • @M.A.SARKAR, I hadn't understood you well. In that case I would use minipages and the caption package. See the last edit. – Juan Castaño Aug 26 '21 at 18:58
  • That is what I wanted. Thank you very much – learner Aug 26 '21 at 18:59
2

Any help both with the 2D and 3D graph of the given 3 inequalities.

The straight forward way would be to plot the 3 inequalites for z = 0 in 2D. Unfortunately I never worked with the package before, so I have to leave the code details to the experts.

However ..

0 <= x+y+1, 0 <= 4x, 0 <= 5y.

The 2D projection is as follows (i.e., z=0-plane):

... are you sure the diagram matches these 3 inequalities?

  • x >= 0
  • y >= 0
  • x + y >= -1
MS-SPO
  • 11,519