如何使用 pgfplots 绘制 Lambert W 函数

如何使用 pgfplots 绘制 Lambert W 函数

我想绘制兰伯特·W使用 pgfplots 等函数绘制不同颜色这里但我不知道该怎么做。有没有办法根据线反射函数?我正在使用 LuaLaTeX。

答案1

朗伯W函数是一个多值非初等函数,那么 PGFplots 不能仅通过键入 来绘制它\addplot {LambertW(x)};

另一方面,反函数y e^y是基本的,可以轻松绘制,因此,一个简单的参数图就可以解决问题。如果要用不同的颜色绘制 Lambert W 函数的第 -1 和第 0 个分支,则需要分别绘制它们。转折点发生在,因此在以下示例中将(-1/e, -1)域拆分为。-1

\documentclass[tikz]{standalone}

\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}

\begin{document}
\begin{tikzpicture}
  \begin{axis}[
      samples=1001,
      enlarge y limits=false,
      axis lines=middle,
    ]
    \addplot [red!80!black, domain=-5:-1] (x * exp(x), x);
    \addplot [blue!80!black, domain=-1:2] (x * exp(x), x);
  \end{axis}
\end{tikzpicture}
\end{document}

输出

答案2

Lambert W 函数内置于 Sage 中,因此您可以通过 访问它sagetex。请注意,主要分支使用 1 个参数,但其他分支需要两个参数,以便明确您想要哪个分支;例如 lambert_w(-1,t2).n(digits=6) 指定 -1 分支。Sagetex 需要本地安装 Sage,或者为了避免这种情况,需要免费思杰云帐户。

\documentclass{standalone}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{pgfplots}
\usepackage{sagetex}
\usetikzlibrary{spy}
\usetikzlibrary{backgrounds}
\usetikzlibrary{decorations}
\pgfplotsset{compat=newest}% use newest version
\begin{document}
\begin{sagesilent}
####### SCREEN SETUP #####################
LowerX = -1
UpperX = 10.0
LowerY = -4.0
UpperY = 2.0
step = .01
Scale = 1.0
xscale=1.0
yscale=1.0
#####################TIKZ PICTURE SET UP ###########
output = r""
output += r"\begin{tikzpicture}"
output += r"[line cap=round,line join=round,x=8.75cm,y=8cm]"
output += r"\begin{axis}["
output += r"grid = none,"
output += r"minor tick num=4,"
output += r"every major grid/.style={Red!30, opacity=1.0},"
output += r"every minor grid/.style={ForestGreen!30, opacity=1.0},"
output += r"height= %f\textwidth,"%(yscale)
output += r"width = %f\textwidth,"%(xscale)
output += r"thick,"
output += r"black,"
output += r"axis lines=center,"
#Comment out above line to have graph in a boxed frame (no axes)
output += r"domain=%f:%f,"%(LowerX,UpperX)
output += r"line join=bevel,"
output += r"xmin=%f,xmax=%f,ymin= %f,ymax=%f,"%(LowerX,UpperX,LowerY,   UpperY)
#output += r"xticklabels=\empty,"
#output += r"yticklabels=\empty,"
output += r"major tick length=5pt,"
output += r"minor tick length=0pt,"
output += r"major x tick style={black,very thick},"
output += r"major y tick style={black,very thick},"
output += r"minor x tick style={black,thin},"
output += r"minor y tick style={black,thin},"
#output += r"xtick=\empty,"
#output += r"ytick=\empty"
output += r"]"
##############FUNCTIONS#################################
##FUNCTION 1
t1 =  var('t1')
x1_coords = srange(-1/e,10,step)
y1_coords = [(lambert_w(t1)).n(digits=6) for t1 in x1_coords]
output += r"\addplot[thin, NavyBlue, unbounded coords=jump]  coordinates {"
for i in range(0,len(x1_coords)):
    if (y1_coords[i])<LowerY or (y1_coords[i])>UpperY:
        output += r"(%f,inf) "%(x1_coords[i])
    else:
        output += r"(%f,%f) "%(x1_coords[i],y1_coords[i])
output += r"};"
##FUNCTION 2 #########################################
t2 =  var('t2')
x2_coords = srange(-1/e+.000001,-.01,.001)
y2_coords = [(lambert_w(-1,t2)).n(digits=6) for t2 in x2_coords]
output += r"\addplot[thin, orange, unbounded coords=jump] coordinates {"
for i in range(0,len(x2_coords)):
    if (y2_coords[i])<LowerY or (y2_coords[i])>UpperY:
        output += r"(%f,inf) "%(x2_coords[i])
    else:
        output += r"(%f,%f) "%(x2_coords[i],y2_coords[i])
output += r"};"
##### COMMENT OUT A LINE OF SAGESILENT BY STARTING WITH #
output += r"\end{axis}"
output += r"\end{tikzpicture}"
\end{sagesilent}
\sagestr{output}
\end{document}

输出结果如下: 在此处输入图片描述

答案3

Lambert W 函数的两个实数分支都包含在 GSL——GNU 科学库 并可作为名称和下的命令Asymptote后的任何标准函数使用:import gsl;W0Wm1

//    
// file `plot-W.asy`   
//    
// run `asy plot-W.asy`  to get `plot-W.pdf` 
//    
settings.tex="lualatex";
import graph;
import math;
import gsl;
size(8cm);

import fontsize;defaultpen(fontsize(7.5pt));
texpreamble("\usepackage{lmodern}"+"\usepackage{amsmath}"
+"\usepackage{amsfonts}"+"\usepackage{amssymb}"
+"\def\e{\mathrm{e}}\def\Wp{\operatorname{W_0}}\def\Wm{\operatorname{W_{-1}}}"
);

pen pen0=red+0.7bp, pen1=blue+0.7bp;
pen dashPen=gray(0.56)+0.7bp+linetype(new real[]{3,3})+linecap(0);

real sc=0.1;
add(shift(-12*sc,-30*sc)*scale(sc)*grid(43,43,paleblue+0.2bp));

xaxis(-1.2,3.1,RightTicks(Step=1,step=0.5,OmitTick(0)),above=true);
yaxis(-2.9,1.2,above=true);
for(int i: new int[]{-2,-1,0,1}){
  if(i!=0){
    ytick(z=(0,i),dir=plain.E,size=Ticksize);
    labely(Label("$"+string(i)+"$"),z=(0,i),align=3*plain.E);
  }
  ytick(z=(0,i-0.5),dir=plain.E,size=ticksize);
}

real e=exp(1);
guide g0=graph(W0,-1/e,3, n=1000);
guide g1=graph(Wm1,-1/e,(-2.8)*exp(-2.8), n=1000);

pair A=(-1/e,-1), B=(-1/e,0);

draw((0,-1)--A--B,dashPen);
draw(g0,pen0);
draw(g1,pen1);

dot(A--B,UnFill);
label("$-\tfrac1\e$",B,plain.N);
label("$\Wp(x)$",(1,W0(1)),plain.NW,pen0);
label("$\Wm(x)$",(-2*exp(-2),-2),plain.W,pen1);

在此处输入图片描述

答案4

为了完成 JP-Ellis 的回答(顺便说一句谢谢你,你帮了我大忙!)特别是对于像我这样不具备使用其他两个答案(sagetex或 GSL-GNU Scientific)知识的人。

添加转折点的图例、轴的名称以及整体“更好”的形状(x<0 的图形更不细长)。

这是我的代码:

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}

\begin{document}
\begin{tikzpicture}
  \begin{axis}[
      samples=1001,
      enlarge y limits=false,
      axis lines=middle,
      xmin=-1.1,
      ymax=1.4,
      ytick={-4, -3, -2, -1, 1},
      xtick={-1, 1, 2, 3},
      yticklabels={{$-4$}, {$-3$}, {$-2$}, {$-1$}, {$1$}},
      yticklabel pos=right,
      width=10cm,
      xlabel = \(x\),
      xlabel style={xshift=0.5cm, yshift=-0.2cm},
      every y tick label/.style={
        anchor=near yticklabel opposite,
        xshift=0.2em,
        },
      ylabel = \(W(x)\),
      ylabel style={xshift=-0.5cm, yshift=0.6cm},
         ]
   
    \addplot [red!80!black, domain=-4.2:-1, line width=0.3mm, dashed] (x * exp(x), x);
    
    \addplot [blue!80!black, domain=-1:1.1, line width=0.3mm] (x * exp(x), x);
    
    \draw[dotted] (-0.367879441171442321595523770161460867445811131031767834507836801,-1) -- (-0.367879441171442321595523770161460867445811131031767834507836801, 0);
    \node[above] at (-0.4, 0){\tiny{$-1/e$}};
  \end{axis}
\end{tikzpicture}

\end{document}

兰伯特 W 函数沿实轴

相关内容