Tikz:使用輪廓 gnuplot 繪製隱式函數

Tikz:使用輪廓 gnuplot 繪製隱式函數

我試圖繪製一個隱式函數。我不斷收到相同的錯誤訊息:「未定義的變數:設定」。

提前致謝。

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{pgfplots.fillbetween} 
\pgfplotsset{compat=newest}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    title={Implicit Function Plot},
    xlabel={$x$},
    ylabel={$y$},
    xmin=0, xmax=50,
    ymin=0, ymax=50,
    view={0}{90}, % 
]

\addplot3[
    thick,
    contour gnuplot={levels={0}, labels=false}, % Adjust 'levels' as needed
    samples=50, % Increase for higher resolution
    samples y=50, % Explicitly defining samples y for clarity
    domain=0:50,
    domain y=0:50,
] 
    gnuplot {
        set contour base;
        set cntrparam levels discrete 0;
        unset surface;
        set view map;
        splot -1/2*(x+y)**(-1/2)*x + 100 - (x+y)**(1/2) - 2*x;
    };

\end{axis}
\end{tikzpicture}
\end{document}

答案1

在此輸入影像描述

在圖中,我們可以看到f(x, y) = x/(2\sqrt{x +y}) +\sqrt{x +y} +2x對應於值 20 的函數的水平曲線。

這個解決方案基於一些計算,這些計算y作為 的函數x(主要是求解二次方程式)。當然,有一個正性條件給出 的定義域x。繪圖基於 TikZ 而不是 pgfplots。我不知道如何使該命令contour起作用。

繪製的函數被命名tmpMtmpP引入;它們是在兩個區間的並集上繪製的,其邊界由函數計算xLIni(即左側區間的初始 x),...

在下面的程式碼中,有一個變數\a被設定為10;它對應於2*\a水平曲線的值。

評論。所涉及的數字很快就會變得太大。因此,必須全域修改單位(請參閱[x=6.7pt, y=.1pt])。

程式碼

\documentclass[11pt, margin=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{math}

\begin{document}

\tikzmath{%
  function xLIni(\c) {%
    return {\c -2*\c};
  };
  function xLEnd(\c) {%
    return {(2*\c +1/2 -pow(2*\c +1/4, .5))/2};
  };
  function xRIni(\c) {%
    return {(2*\c +1/2 +pow(2*\c +1/4, .5))/2};
  };
  function xREnd(\c) {%
    return {\c +2*\c};
  };
  function tmpM(\t, \c) {%
    \tmp = (\t -\c)*(\t -\c) -\t/2;
    return {2*\tmp -\t/2 -2*(\t -\c)*pow(\tmp, .5)};
  };
  function tmpP(\t, \c) {%
    \tmp = (\t -\c)*(\t -\c) -\t/2;
    return {2*\tmp -\t/2 +2*(\t -\c)*pow(\tmp, .5)};
  };
}
\begin{tikzpicture}[x=6.7pt, y=.1pt,
  evaluate={\a = 10; \b = int(\a*\a); \cst = int(2*\a);}]
  \draw[->] ({xLIni(\a) -1}, 0) -- ({xREnd(\a) +1}, 0) node[above] {$x$};
  \draw[->] (0, -.2) -- (0, {1.7*xREnd(\a)*xREnd(\a)}) node[left] {$y$};

  \draw (\a, 0) -- +(0, 20) -- +(0, -20) node[below] {$\a$};
  \draw (0, \b) -- +(.3, 0) -- +(-.3, 0) node[left] {$\b$};
  
  \begin{scope}[every path/.style={%
      blue, very thick, variable=\t, samples=100
    }]
    \draw[domain={xLIni(\a)}:{xLEnd(\a)}] plot (\t, {tmpM(\t, \a)});
    \draw[domain={xLIni(\a)}:{xLEnd(\a)}] plot (\t, {tmpP(\t, \a)});
    \draw[domain={xRIni(\a)+.05}:{xREnd(\a)}] plot (\t, {tmpP(\t, \a)});
    \draw[domain={xRIni(\a)+.05}:{xREnd(\a)}] plot (\t, {tmpM(\t, \a)});      
  \end{scope}
\end{tikzpicture}

\end{document}

答案2

如果您想使用該gnuplot set語法,則必須使用選項來呼叫它raw gnuplot

除非您確實需要 rawgnuplot來做不支援的事情pgfplots,否則我建議gnuplot從內部使用pgfplots,如下所示。請注意,要繪製的函數必須是pgfplots數學語法中,即,您需要使用^而不是**.

編輯:Gnuplot 需要write18啟用(外部命令)。編譯使用pdflatex --shell-escape file.tex.

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{pgfplots.fillbetween} 
\pgfplotsset{compat=newest}

\begin{document}
  \begin{tikzpicture}
    \begin{axis}[
      title={Implicit Function Plot},
      xlabel={$x$},
      ylabel={$y$},
      xmin=0, xmax=50,
      ymin=0, ymax=50,
      view={0}{90}, % 
      ]
      \addplot3[
        thick,
        contour gnuplot={levels={0,10,20,30}, labels=false},
        samples=100, 
        samples y=100,
        domain=0:50,
        domain y=0:50,
        ]{-1/2*(x+y)^(-1/2)*x + 100 - (x+y)^(1/2) - 2*x};
    \end{axis}
  \end{tikzpicture}
\end{document}

您也可以使用contour lua完全繞過gnuplot以支援使用 進行編譯lualatex。有關pgfplots等高線圖的更多詳細信息,請參見pgfplots手冊

相關內容