gnuplot中的圆上的剪切向量

问题描述 投票:0回答:1

我尝试在圆形区域中绘制一些矢量场。考虑以下MWE

unset grid
unset tics
unset colorbox
unset border

set size square

besselj(n, x) = n > 1 ? 2*(n-1)/x*besselj(n-1,x) - besselj(n-2,x) : (n == 1 ? besj1(x) : besj0(x))
dbesselj(n, x) = n/x*besselj(n,x) - besselj(n+1,x)

rho(x,y) = sqrt(x**2+y**2)
phi(x,y) = atan2(y,x)

d = 1.0
l = 1.0
z = l/2

q = 1

set xrange [-d/2*1.1:d/2*1.1]
set yrange [-d/2*1.1:d/2*1.1]

Erho(x,y,n,ynp) = (-1/rho(x,y)) * besselj(n, (ynp*2/d)*rho(x,y)) * (-n*sin(n*phi(x,y))) * sin(q*pi*z/l)
Ephi(x,y,n,ynp) = (ynp*2/d) * dbesselj(n, (ynp*2/d)*rho(x,y)) * (cos(n*phi(x,y))) * sin(q*pi*z/l)

Ex(x,y,n,ynp) = rho(x,y) > d/2 ? NaN : cos(phi(x,y))*Erho(x,y,n,ynp) - sin(phi(x,y))*Ephi(x,y,n,ynp)
Ey(x,y,n,ynp) = rho(x,y) > d/2 ? NaN : sin(phi(x,y))*Erho(x,y,n,ynp) + cos(phi(x,y))*Ephi(x,y,n,ynp)

mag(x,y,n,ynp) = sqrt(Ex(x,y,n,ynp)**2 + Ey(x,y,n,ynp)**2)

set object circle at 0,0 size 0.5 fc black lw 3 front

set multiplot layout 1,2

set title 'TE_{01}'
set table 'tmp.dat'
set samples 16
set isosamples 16
plot '++' u 1:2:(Ex($1,$2,0,3.832)/50):(Ey($1,$2,0,3.832)/50) w vectors
unset table
set samples 250
set isosamples 250
plot '++' u 1:2:(mag($1,$2,0,3.832)) w image notitle, \
     'tmp.dat' u 1:2:3:4 w vectors head filled lc black lw 1 notitle

set title 'TE_{11}'
set table 'tmp.dat'
set samples 16
set isosamples 16
plot '++' u 1:2:(Ex($1,$2,1,1.841)/20):(Ey($1,$2,1,1.841)/20) w vectors
unset table
set samples 250
set isosamples 250
plot '++' u 1:2:(mag($1,$2,1,1.841)) w image notitle, \
     'tmp.dat' u 1:2:3:4 w vectors head filled lc black lw 1 notitle

unset multiplot

绘制矢量场及其在直径d的圆内的大小。结果是

output from the above code

对于左图像(TE01)完全可以,但是右图像(TE11)看起来很丑,因为在圆外绘制了一些矢量。我真正想要的结果是这个

desired result

我在黑圈外没有向量的地方。我该如何实现?我知道gnuplot中有clip函数,但这不允许指定用于剪切的形状。

gnuplot
1个回答
0
投票

这里是您可以尝试的。定义自己的剪辑功能,例如一个圆圈。首先,您需要检查数据点是否在圆之外。如果Clip(x,y)位于外部,则返回NaN;如果位于内部,则返回0。现在,在绘制时,只需将clip函数的值添加到您的值即可。您的数据将被剪切成一个圆,因为某些+0保持不变,而某些+NaN将为NaN并且不会被绘制。如果仅对x(向量开始)和delta x(向量结束)执行此操作就足够了。

代码:

### clip function in circle form
reset session
set size square

# create some test data
set samples 25
Scaling = 0.5
set table $Data
plot [-5:5] '++' u 1:2:(Scaling*$1/sqrt($1**2+$2**2)): \
         (Scaling*$2/sqrt($1**2+$2**2)) : (sqrt($1**2+$2**2)) with table
unset table

set palette rgb 33,13,10
CenterX = 0
CenterY = 0
Radius = 3.5
Clip(x,y) = sqrt((x-CenterX)**2 + (y-CenterY)**2) > Radius ? NaN : 0

set xrange[-6:6]
set yrange[-6:6]

set multiplot layout 1,3

    plot [-5:5] $Data u 1:2:3:4:5 w vec lc pal not

    plot [-5:5] $Data u ($1+Clip($1,$2)):2:($3+Clip($1+$3,$2+$4)):4:5 w vec lc pal not

    CenterX = 1
    CenterY = 1
    plot [-5:5] $Data u ($1+Clip($1,$2)):2:($3+Clip($1+$3,$2+$4)):4:5 w vec lc pal not

unset multiplot
### end of code

结果:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.