如何绘制函数的曲面图

如何绘制函数的曲面图

我想为和(y+ln(1-x))^2/y^2的范围创建一个曲面/轮廓图。x=0:0.5y=1:30

这是 Wolfram Alpha 想要的结果。

WolframAlpha 输出

然而我在 PGF 中的尝试却惨遭失败:

    \documentclass[tikz]{standalone}
    \usepackage{pgfplots}
    \usepackage{amsmath}
    \begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            declare function = {Z(\x,\y) = (\y+ln(1-\x))^2/\y^2;},
            point meta max=1,
            point meta min=0,
            samples=40,
            view={0}{90},
            xmin=0,
            xmax=0.5,
            xlabel={x},
            ymin=1,
            ymax=30,
            zmin=0,
            zmax=1,
            colorbar,
            ylabel={y}]
            \addplot3 [surf] {Z(x,y)};
        \end{axis}[]
    \end{tikzpicture}%
    \end{document}

在此处输入图片描述

我将非常感激您的帮助。

答案1

带轮廓线的 pgf Pgfplots 可以通过 gnuplot 及其轮廓 gnuplot 界面计算 z 轮廓。Windows 操作方法:

  • 安装 gnuplot 时可选择将 gnuplot 路径添加到搜索路径在此处输入图片描述

  • 重启

  • 将“--enable-write18”添加到您的 LaTeX 命令中。以下显示 pdflatex 和 TexStudio:在此处输入图片描述这是执行 gnuplot 所必需的。
  • 您应该能够使用以下代码生成图表:

    \documentclass[tikz]{standalone}
    \usepackage{pgfplots}
    \usepackage{amsmath}
    \begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            declare function = {Z(\x,\y) = (\y+ln(1-\x))^2/\y^2;},
            domain=0:0.5,
            y domain=1:10,
            point meta max=1,
            point meta min=0,
            samples=40,
            colorbar,
            colormap/bluered,
            colorbar style={title=$z$},
            view={0}{90},
            xmin=0,
            xmax=0.5,
            xlabel={x},
            zmin=0,
            zmax=1,
            colorbar,
            ylabel={y}]
            \addplot3 [surf,shader=interp] {Z(x,y)};
            \addplot3[
                contour gnuplot={
                    levels={0.4,0.6,0.7,0.8,0.9,0.95},
                    contour label style={
                        nodes={text=black,opacity=0,text opacity=1,anchor=south},
                        %/pgf/number format/fixed,
                        /pgf/number format/fixed zerofill=true,
                        %/pgf/number format/precision=1
                        },                  
                    output point meta=rawz,
                    %number=10,
                    %labels=false,
                    draw color = black
                },
                samples=41,
                contour/draw color={black},
                contour/label distance=80pt
                ]
                {Z(x,y)};
        \end{axis}[]
    \end{tikzpicture}%
    \end{document}
    

我将非常感激能够取得进一步的改进。

答案2

尝试Asymptote内联模式:

% cont.tex:
%
\documentclass{article}
\usepackage[inline]{asymptote}
\usepackage{lmodern}
\begin{document}
\begin{figure}
\centering
\begin{asy}
import graph;
import palette;
import contour;

real w=6cm,h=w;
size(w,h,IgnoreAspect);
import fontsize;defaultpen(fontsize(7pt));
real f(real x, real y) {return (y+log(1-x))^2/y^2;}

pair a=(  0, 1);
pair b=(0.5,30);

int N=200;
int Divs=10;
int divs=2;

defaultpen(0.4bp);
pen Tickpen=black;
pen tickpen=gray+0.5*linewidth(currentpen);
pen[] Palette=Gradient(rgb("A42420"),rgb("FFF2B4"));

bounds range=image(f,Automatic,a,b,N,Palette);

// Major contours

real[] Cvals=uniform(range.min,range.max,Divs);
draw(contour(f,a,b,Cvals,N,operator --),Tickpen);
// Minor contours
real[] cvals;
for(int i=0; i < Cvals.length-1; ++i)
  cvals.append(uniform(Cvals[i],Cvals[i+1],divs)[1:divs]);
draw(contour(f,a,b,cvals,N,operator --),tickpen);

xaxis("$x$",BottomTop, RightTicks(Step=0.1,step=0.02),above=true);
yaxis("$y$",LeftRight,LeftTicks(Step=6,step=1),above=true);

palette("$f(x,y)=\displaystyle\frac{(y+\log(1-x))^2}{y^2}$",range,point(NW)+(0,0.5),point(NE)+(0,3),Top,Palette,
        PaletteTicks(N=Divs,n=divs,Tickpen,tickpen));
\end{asy}
\caption{A surface/contour plot of $(y+\ln(1-x))^2/y^2$}
\end{figure}
\end{document}
%
%   Process this file as:
%
% pdflatex cont.tex
% asy cont-*.asy    
% pdflatex cont.tex

在此处输入图片描述

答案3

使用 R 和 knitr 包生成级别曲线和热图。

\documentclass[10pt,letterpaper]{article}

\begin{document}
Using \textbf{R} with the package 'knitr' to plot the level curve of a function plotted over a heatmap. 

Note: This is a quick demo of linking \textbf{R} and \textbf{LaTeX} using the 'knitr' package. This has been run on Windows 8.1, with MikTeX 2.9, and TeXmaker 4.4.1 as the IDE. The following code is saved as \textbf{knit03.Rnw} (and this is case sensitive). With the package 'knitr' installed in R 3.1.3 you run the command: \emph{knit("knit03.Rnw")}. This will generate the file \textbf{knit03.tex} which you now compile with pdflatex and view as a pdf.  

<<echo=FALSE>>=
f=function(x,y) (y+log(1-x))^2/y^2
 u=v=seq(0,.5,length=30)
 v=seq(1,30,length=30)
 f=outer(u,v,f)
 angle<-25
 image(u,v,f)
 contour(u,v,f,add=TRUE,levels=c(.5,.7,.75,.8,.85,.9,.95,.975))
@
\end{document}

输出为: 在此处输入图片描述

相关内容