简体   繁体   English

Gnuplot 未绘制 x**(1/3) 的负值

[英]Gnuplot not plotting negative values of x**(1/3)

This is the correct graph of x^(1/3):这是 x^(1/3) 的正确图:

x^(1/3) 的正态图

However, when trying to plot x**(1./3) on gnuplot, this is what I get:但是,当尝试在 gnuplot 上 plot x**(1./3)时,这就是我得到的:

使用 gnuplot 绘制 x^(1/3) 的图

How can I fix this?我怎样才能解决这个问题?

Additionally, it is not the first time that gnuplot doesn't plot values around zero in other functions (when it is supposed to, of course: I'm not talking about asymptotes etc.).此外,这不是 gnuplot 第一次在其他函数中没有 plot 值接近零(当然,当它应该是:我不是在谈论渐近线等)。 What can I do?我能做些什么?

The reason why your graph is not drawn closer to zero is because there is a default sampling of 100, ie your function will be plotted with 100 points within your xrange.您的图表未接近零的原因是因为默认采样为 100,即您的 function 将在您的 xrange 内绘制 100 个点。 If you increase this number, this will get you closer to zero.如果你增加这个数字,这将使你更接近于零。 Check help samples .检查help samples

About the missing negative values, this seems to be a bit special in gnuplot.关于缺失的负值,这在 gnuplot 中似乎有点特殊。 I guess x**b where b is a floating point number is special.我猜x**b其中b是一个浮点数是特殊的。 You can test the examples:您可以测试示例:

print sqrt(4)
print 4**0.5
print sqrt(-4)
print (-4)**0.5
print 8**(1./3)
print (-8)**(1./3)

which will give:这将给出:

2.0
2.0
{0.0, 2.0}
{1.22460635382238e-16, 2.0}   # rounding error {0.0, 2.0}
2.0
{1.0, 1.73205080756888}       # 1 out of 3 valid solutions, but not the expected -2

Values in {Re, Im} are imaginary numbers. {Re, Im}中的值是虚数。 First the real part then the imaginary part.首先是实部,然后是虚部。 In order to get your plot nevertheless you can try the following:为了获得您的 plot,您可以尝试以下操作:

Code:代码:

### cube root
reset session

set samples 500
set grid xtics, ytics

cuberoot(x) = sgn(x)*abs(x)**(1./3)

plot cuberoot(x) w l
### end of code

Result:结果:

在此处输入图像描述

Addition:添加:

I'll try to explain, but I'm not a mathematician.我会试着解释一下,但我不是数学家。 For the N-th root there are N solutions.对于第 N 个根,有 N 个解。 Apparently, gnuplot takes one of them.显然,gnuplot 采用了其中之一。 Apparently, one with a positive real part, and if there are several, the one with the smallest positive imaginary part.显然,一个具有正实部,如果有多个,则具有最小正虚部的一个。 I guess it is called "principal root".我猜它被称为“主根”。 Check also this .还要检查这个

So, this will explain why所以,这将解释为什么

print (-8)**(1./3)
print (-8)**(1./9)  # 9th root

will return将返回

{1.0, 1.73205}          # and not -2
{1.18393, 0.4309183}    # and not -1.25992

Code:代码:

### roots
reset session

set size ratio -1
set xlabel "real part"
set xrange [-3:3]
set ylabel "imaginary part"
set yrange [-3:3]
set grid xtics, ytics

set angle degrees
NRootRe(x,N,i) = -sgn(x)*abs(x)**(1./N)*cos(360.*i/N - 180*sgn(x))
NRootIm(x,N,i) = -sgn(x)*abs(x)**(1./N)*sin(360.*i/N - 180*sgn(x))

x = -8
do for [N=3:9:2] {
    do for [i=1:N] {
        print sprintf('x=%g, N=%g, i=%g: {% 9g, % 9g}',x,N,i,NRootRe(x,N,i), NRootIm(x,N,i))
    }
    print ""
}

plot for [N=3:9:2] [i=1:N:1] '+' u (0):(0):(NRootRe(x,N,i)):(NRootIm(x,N,i)) w vec ti sprintf("x=%g, N=%g",x,N)
### end of code

Result:结果:

x=-8, N=3, i=1: {        1,  -1.73205}
x=-8, N=3, i=2: {        1,   1.73205}
x=-8, N=3, i=3: {       -2,  7.34764e-16}

x=-8, N=5, i=1: {-0.468382,  -1.44153}
x=-8, N=5, i=2: {  1.22624, -0.890916}
x=-8, N=5, i=3: {  1.22624,  0.890916}
x=-8, N=5, i=4: {-0.468382,   1.44153}
x=-8, N=5, i=5: { -1.51572,  5.56847e-16}

x=-8, N=7, i=1: {-0.839155,  -1.05227}
x=-8, N=7, i=2: { 0.299491,  -1.31216}
x=-8, N=7, i=3: {  1.21261, -0.583964}
x=-8, N=7, i=4: {  1.21261,  0.583964}
x=-8, N=7, i=5: { 0.299491,   1.31216}
x=-8, N=7, i=6: {-0.839155,   1.05227}
x=-8, N=7, i=7: {  -1.3459,  4.94459e-16}

x=-8, N=9, i=1: {-0.965156, -0.809862}
x=-8, N=9, i=2: {-0.218783,  -1.24078}
x=-8, N=9, i=3: { 0.629961,  -1.09112}
x=-8, N=9, i=4: {  1.18394, -0.430918}
x=-8, N=9, i=5: {  1.18394,  0.430918}
x=-8, N=9, i=6: { 0.629961,   1.09112}
x=-8, N=9, i=7: {-0.218783,   1.24078}
x=-8, N=9, i=8: {-0.965156,  0.809862}
x=-8, N=9, i=9: { -1.25992,  4.62872e-16}

在此处输入图像描述

The cube root of x for x < 0 is a surface of complex values. x < 0 时 x 的立方根是复数曲面。 Gnuplot cannot easily plot that. Gnuplot 不能轻易 plot 那个。 The plot you show is presumably for您展示的 plot 大概是用于

f(x) = sgn(x) * abs(x)**(1./3) 

which is the intersection of that complex surface with the plane Imag(z) = 0. (Edit: see supplementary figures).这是该复杂表面与平面 Imag(z) = 0 的交点。(编辑:参见补充图)。

An alternative plot that makes intuitive sense is to plot x as the cube of y.另一种直观的 plot 是将 plot x 作为 y 的立方。

plot [t=-2:2] (t**3):(t) with lines

在此处输入图像描述

Supplementary figures补充数据

Figure 1 ) The first figure shows why the 'simple' root that lies along the negative real axis is undesireable because it runs along a discontinuity in the imaginary component of the complex surface.图 1 ) 第一个图显示了为什么位于负实轴上的“简单”根是不可取的,因为它沿着复曲面的虚部的不连续运行。

set title "Discontinuous imaginary component along negative real axis"
set yrange [-.1:.1]
set xrange [-10:10]
set xlabel "Real"
set ylabel "Imaginary"
set xyplane 0
set grid x y z vertical

f(x,y) = (x + I*y) ** (1./3)

splot real(f(x,y)), imag(f(x,y)), abs(f(x,y)), \
      [-10:10] '+' using (x):(0):(sgn(x)*abs(x)**(1./3)) with lines lt -1 lw 2 title "cuberoot(x)"

在此处输入图像描述

Figure 2 This figure shows the complex surface of solutions to the equation (Z)**(1/3) .图 2此图显示方程(Z)**(1/3)解的复曲面。 For each plane of constant Z the solutions lie on three curves.对于每个具有常数 Z 的平面,解位于三个曲线上。 I stacked the curves generated for sequential samples along Z to generate the surface.我沿 Z 堆叠为连续样本生成的曲线以生成曲面。 The heavy black line is the 'simple' cuberoot(Z) limited to real values, ie the same line as in Figure 1 and requested in the original question.粗黑线是限制为实际值的“简单”立方根(Z),即与图 1 中相同的线并在原始问题中要求。 The green line is the complex value returned by gnuplot for Z**(1./3) .绿线是 gnuplot 为Z**(1./3)返回的复数值。 For positive Z the two curves are the same.对于正 Z,两条曲线是相同的。 For negative Z they lie on different regions of the complex surface.对于负 Z,它们位于复曲面的不同区域。 I have confirmed that gnuplot's complex power function and the C library function cpow both return values from the same region of the surface.我已经确认 gnuplot 的复数幂 function 和 C 库function cpow 都从表面的同一区域返回值。 在此处输入图像描述

The reason why Gnuplot doesn't plot or calculate x**(1/3.) for negative numbers (in real numbers world) is because it treats power function as logarithm functions: x^n = exp(ln(x^n)) = exp(nln(x)) , where ln(x) is the natural logarithm. Gnuplot不 plot 或为负数(在实数世界中)计算x**(1/3.)的原因是因为它将幂 function 视为对数函数: x^n = exp(ln(x^n)) = exp(nln(x)) ,其中ln(x)是自然对数。 Notice that x^n = exp(n ln(x)) has its domain in x > 0 .请注意, x^n = exp(n ln(x))的域在x > 0中。

Of course, we can compute ln(x) for x<0 using complex numbers (as we can see when compute (-8)^(1/3.) )当然,我们可以使用复数计算x<0ln(x) (正如我们在计算(-8)^(1/3.)时看到的那样)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM