# restricting variables in pgf plots

I am trying to draw the 'level curves' of the following function:

where the variables are restricted such that x,y are non-negative, and x+y is less than or equal to one. In other words, this is a function of a vector (x(1),x(2),x(3)) such that the elements all add up to 1. So for example, I would want to set z=0.5 and plot the function for different values of z. If anyone has a simple example of this kind of paramaterisation it would be really helpful.

\documentclass[border=3pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
view/h=135,
axis lines=center,
xmax=1.5,
ymax=1.5,
zmax=1.5,
ytick={1},
xlabel={$x_{1}$},
ylabel={$x_{2}$},
zlabel={$x_{3}$},
]
\addplot3[patch,blue!70!black!50,forget plot]
coordinates
{
(1,0,0)
(0,1,0)
(0,0,1)
};
\addplot3[no markers,red!90!black]
coordinates
{
(0.25,0.1,0.45)
(0.2,0.1,0.4)
(0.24,0.36,0.4)
(0.16,0.32,0.62)
(0.12,0.3,0.58)
(0.1,0.26,0.64)
};
\addplot3[domain=0:0.5,y domain=0:0.5, red, thick,samples=50] {x*ln(3*x)+y*ln(3*y)+(1-x-y)*ln(3*(1-x-y)) };

\node[fill=red!90!black,inner sep=1pt,circle,label={180:$Y$}]
at (axis cs:0.1,0.26,0.64) {};
\end{axis}
\end{tikzpicture}

\end{document}


my goal is to get something along the lines of:

• @CroCo updated to include my attempt – WeakLearner Sep 18 '15 at 12:14
• Contouring is not easy and doing it with pgfmath is just plain wasteful. – John Kormylo Sep 20 '15 at 15:46
• Is a lualatex solution acceptable? – JPi Sep 24 '15 at 2:19
• @JPi yes, that would be great – WeakLearner Sep 24 '15 at 3:14
• ok, will try to do on the weekend; hope the deadline can be extended – JPi Sep 24 '15 at 18:57

## 1 Answer

I am not sure if I have understood the mathematical problem correctly, because you are talking about two different $z$ values. I assume that the $z$ on the left hand side of the equation is the same as on the right, written as $(1-x-y)$ and replaced the one on the left with the one on the right. Then the problem is reduced to two variables. Next I have moved the left $z$ to the right hand side of the equation, so it now shows as $0 = x ln[3x] + y ln[3y] + (1-x-y) ln[3(1-x-y)] - (1-x-y)$.

Unfortunately the variables cannot be separated, so by giving for example $x$ values you have to solve for the corresponding $y$ values in an external program. This is a quite easy task, because it is a simple root search.

Having then the $x$ and $y$ values the corresponding $z$ values can be calculated again and you can plot the result. Here I used the ternary library of pgfplots which you can find in section 5.13 of the PGFPlots manual on page 487 (v1.13).

\documentclass[border=2mm]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{
pgfplots.ternary,
}
\begin{document}
\begin{tikzpicture}
\begin{ternaryaxis}[
xlabel=$x$,
ylabel=$y$,
zlabel=$z$,
min=0,
max=1,
ternary limits relative=false,
smooth,
no markers,
]
\addplot3+ [thick] table {
x           y           z
1.000E-10   5.813E-01   4.187E-01
2.500E-02   6.280E-01   3.470E-01
5.000E-02   6.457E-01   3.043E-01
1.000E-01   6.542E-01   2.458E-01
1.500E-01   6.444E-01   2.056E-01
2.000E-01   6.237E-01   1.763E-01
2.500E-01   5.954E-01   1.546E-01
3.000E-01   5.611E-01   1.389E-01
3.500E-01   5.217E-01   1.283E-01
4.000E-01   4.777E-01   1.223E-01
4.500E-01   4.291E-01   1.209E-01
5.000E-01   3.753E-01   1.247E-01
5.500E-01   3.147E-01   1.353E-01
6.000E-01   2.425E-01   1.575E-01
6.250E-01   1.974E-01   1.776E-01
6.500E-01   1.294E-01   2.206E-01
6.525E-01   7.179E-02   2.757E-01
6.350E-01   3.269E-02   3.323E-01
6.120E-01   1.251E-02   3.755E-01
5.813E-01   1.000E-10   4.187E-01
};
\end{ternaryaxis}
\end{tikzpicture}
\end{document}