2

I am trying to plot the function $U(x,y)=6*\ln(x) + \ln(y)$ for $x=1,...,4$ and $y=1,...,14-3*x$, however, I can't specify that the domain for $y$ is a function of $x$.

MWE:

\documentclass[tikz,border=5pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[view/h=40,colormap/violet]
    \addplot3[
        surf,
        %shader=interp,
        shader=flat,
        samples=50,
        domain=1:4,y domain=1:14-3*x] % it goes wrong here
        {6*ln(x) + ln(y)};
    \end{axis}
\end{tikzpicture}
\end{document}

How can I do this?


UPDATE:

I have tried to use \pgfmathparse and \pgfmathresult, but I still can't get it to display the right surface.

MWE:

\documentclass[tikz,border=5pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
    %view/h=80,
    colormap/cool,
    xlabel = $x$,
    ylabel = $y$,
    zlabel = {$U(x,y)$}
    ]

    \foreach \i in {1,...,4}{
        \pgfmathparse{14 - 3*\i};
    \addplot3[
        surf,
        shader=interp,
        %shader=flat,
        samples=50,
        domain=1:4,y domain=1:\pgfmathresult] 
        {6*ln(x) + ln(y)};
        }
    \end{axis}
\end{tikzpicture}
\end{document}
lala_12
  • 356
  • Maybe helpful: https://tex.stackexchange.com/a/303151/124842 or https://tex.stackexchange.com/a/18779/124842. – Bobyandbob Oct 08 '17 at 06:45
  • @Bobyandbob I tried to follow the last approach, but I couldn't get it to work. I have updated my question with the new code. – lala_12 Oct 08 '17 at 07:04

1 Answers1

1

One approach could be using:

\addplot3[
    surf,
    %shader=interp,
    %shader=flat,
    samples=60,
    domain=1:4,
    y domain=1:14] 
    {y < 14-3*x ? 6*ln(x) + ln(y) : nan};
\end{axis}

which seems to almost work, because I have an error (the same as here) and I cannot see why. If you change nan (not-a-number) with 0, there is no error. Probably the shader is confused by the big number of NaN arounds...

As suggested by the OP, adding

restrict z to domain=-inf:inf, 

to the axis options does the trick:

enter image description here

(although the ragged end is not so nice...)

Rmano
  • 40,848
  • 3
  • 64
  • 125