[英]Gnuplot not plotting negative values of x**(1/3)
您的圖表未接近零的原因是因為默認采樣為 100,即您的 function 將在您的 xrange 內繪制 100 個點。 如果你增加這個數字,這將使你更接近於零。 檢查help samples
。
關於缺失的負值,這在 gnuplot 中似乎有點特殊。 我猜x**b
其中b
是一個浮點數是特殊的。 您可以測試示例:
print sqrt(4)
print 4**0.5
print sqrt(-4)
print (-4)**0.5
print 8**(1./3)
print (-8)**(1./3)
這將給出:
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
{Re, Im}
中的值是虛數。 首先是實部,然后是虛部。 為了獲得您的 plot,您可以嘗試以下操作:
代碼:
### 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
結果:
添加:
我會試着解釋一下,但我不是數學家。 對於第 N 個根,有 N 個解。 顯然,gnuplot 采用了其中之一。 顯然,一個具有正實部,如果有多個,則具有最小正虛部的一個。 我猜它被稱為“主根”。 還要檢查這個。
所以,這將解釋為什么
print (-8)**(1./3)
print (-8)**(1./9) # 9th root
將返回
{1.0, 1.73205} # and not -2
{1.18393, 0.4309183} # and not -1.25992
代碼:
### 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
結果:
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}
x < 0 時 x 的立方根是復數曲面。 Gnuplot 不能輕易 plot 那個。 您展示的 plot 大概是用於
f(x) = sgn(x) * abs(x)**(1./3)
這是該復雜表面與平面 Imag(z) = 0 的交點。(編輯:參見補充圖)。
另一種直觀的 plot 是將 plot x 作為 y 的立方。
plot [t=-2:2] (t**3):(t) with lines
補充數據
圖 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)"
圖 2此圖顯示方程(Z)**(1/3)
解的復曲面。 對於每個具有常數 Z 的平面,解位於三個曲線上。 我沿 Z 堆疊為連續樣本生成的曲線以生成曲面。 粗黑線是限制為實際值的“簡單”立方根(Z),即與圖 1 中相同的線並在原始問題中要求。 綠線是 gnuplot 為Z**(1./3)
返回的復數值。 對於正 Z,兩條曲線是相同的。 對於負 Z,它們位於復曲面的不同區域。 我已經確認 gnuplot 的復數冪 function 和 C 庫function cpow 都從表面的同一區域返回值。
Gnuplot
不 plot 或為負數(在實數世界中)計算x**(1/3.)
的原因是因為它將冪 function 視為對數函數: x^n = exp(ln(x^n)) = exp(nln(x))
,其中ln(x)
是自然對數。 請注意, x^n = exp(n ln(x))
的域在x > 0
中。
當然,我們可以使用復數計算x<0
的ln(x)
(正如我們在計算(-8)^(1/3.)
時看到的那樣)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.