gnuplot 入门教程 3

常量、操作符和函数

数字

gnuplot 表示数字可分成整数、实数及复数三类:

整数:gnuplot 与 语言相同,采用 4 byte 储存整数。故能表示 -2147483647 至 +2147483647 之间的整数。

实数:能表示约 或 位的有效位数,指数部份为不大于 308 的数字。

复数:以 {<real>,<imag>} 表示复数。其中<real>为复数的实数部分,<imag>为虚数部分,此两部分均以实数型态表示。 如 3 + 2i 即以 {3,2} 表示。 

gnuplot 储存数字的原则为,若能以整数方式储存则以整数储存数字,不然以实数方式储存,其次以复数方式储存。例如在 gnuplot 执行 

print 1/3*3
print 1./3*3

分别得到 和 1.0 的结果。这是因前者使用整数计算,而后者采用实数计算的结果。执行

print 1234.567
print 12345 + 0.56789
print 1.23e300 * 2e6
print 1.23e300 * 2e8

分别得到 1234.5712345.62.46e+304 和 undefined value 的结果。这些例子是受到实数的有效位数和所能表现最大数字的限制。这是我们要注意的。

操作符

gnuplot 的操作符与 语言基本相同。 所有的操作均可做用在整数、实数或复数上。

表格 1 Unary Operators

Symbol

Example

Explanation

-

-a

unary minus

~

~a

one's complement

!

!a

logical negation

!

a!

factorial

表格 2 Binary Operators

Symbol

Example

Explanation

**

a**b

exponentiation

*

a*b

multiplication

/

a/b

division

%

a%b

modulo

+

a+b

addition

-

a-b

subtraction

==

a==b

equality

!=

a!=b

inequality

&

a&b

bitwise AND

^

a^b

bitwise exclusive OR

|

a|b

bitwise inclusive OR

&&

a&&b

logical AND

||

a||b

logical OR

?:

a?b:c

ternary operation

函数

在 gnuplot 中函数的参数可以是整数,实数或是复数。表格 3是 gnuplot 所提供的函数。

表格 3 gnuplot functions

Function

Auguments

Returns

abs(x)

any

absolute value of x, |x|; same type

abs(x)

complex

length of x, sqrt( real(x)^2 + imag(x)^2 )

acos(x)

any

1/cos(x) (inverse cosine) in radians

Acosh(x)

any

cosh−1 x (inverse hyperbolic cosine) in radians

arg(x)

complex

the phase of x in radians

asin(x)

any

1/sin(x) (inverse sin) in radians

asinh(x)

any

sinh−1 x (inverse hyperbolic sin) in radians

atan(x)

any

1/tan(x) (inverse tangent) in radians

atan2(y,x)

int or real

tan−1(y/x) (inverse tangent)

atanh(x)

any

tanh−1 x (inverse hyperbolic tangent) in radians

besj0(x)

int or real

J0 Bessel function of x

besj1(x)

int or real

J1 Bessel function of x

besy0(x)

int or real

Y0 Bessel function of x

besy1(x)

int or real

Y1 Bessel function of x

ceil(x)

any

smallest integer not less than x (real part)

cos(x)

radians

cos x, cosine of x

cosh(x)

radians

cosh x, hyperbolic cosine of x

erf(x)

any

Erf(real(x)), error function of real(x)

erfc(x)

any

Erfc(real(x)), 1.0 - error function of real(x)

exp(x)

any

exponential function of x

floor(x)

any

largest integer not greater than x (real part)

gamma(x)

any

Gamma(real(x)),  gamma function of real(x)

ibeta(p,q,x)

any

Ibeta(real(p,q,x)), ibeta function of real(p,q,x)

inverf(x)

any

inverse error function of real(x)

igamma(a,x)

any

Igamma(real(a,x)), igamma function of real(a,x)

imag(x)

complex

imaginary part of x as a real number

invnorm(x)

any

inverse normal distribution function of real(x)

int(x)

real

integer part of x, truncated toward zero

lambertw(x)

real

Lambert W function

lgamma(x)

any

Lgamma(real(x)),  lgamma function of real(x)

log(x)

any

ln(x), natural logarithm (base e) of x

log10(x)

any

log(x),  logarithm (base 10) of x

norm(x)

any

normal distribution (Gaussian) function of real(x)

rand(x)

any

normal distribution (Gaussian) function of real(x)

real(x)

any

Rand(real(x)),  pseudo random number generator

sgn(x)

any

real part of x

sin(x)

any

1 if x>0, -1 if x<0, 0 if x=0. imag(x) ignored

sinh(x)

radians

sin(x), sine of x

sqrt(x)

radians

sinh(x), hyperbolic sine x

tan(x)

any

sqrt(x),  square root of x

tanh(x)

complex

tan(x),  tangent of x

column(x)

int

column x during datafile manipulation.

defined(X)

variable name

returns 1 if a variable X is defined, 0 otherwise.

tm hour(x)

int

the hour

tm mday(x)

int

the day of the month

tm min(x)

int

the minute

tm mon(x)

int

the month

tm sec(x)

int

the second

tm wday(x)

int

the day of the week

tm yday(x)

int

the day of the year

tm year(x)

int

the year

valid(x)

int

test validity of column(x) during datafile manip.


下面举一些例子:

plot [0.5:20] besj0(x), besj1(x), besy0(x), besy1(x)
plot [0:5] erf(x), erfc(x), inverf(x)

用户自定义函数和常量

在 gnuplot 中,用户可自定函数。函数可有 至 个自变量。 其定义函数的语法如下: 

<function-name> ( <dummy1> {,<dummy2> {, ...}}) = <expression>

而用户定义常数的语法如下: 

<variable-name> = <constant-expression>

下面举一些例子:

# 常数 w 为 2。
w = 2                       
# 常数 q 为小于但最接近 tan(pi/2 - 0.1) 的整数。
q = floor(tan(pi/2 - 0.1))  
# 函数 f(x) 为 sin(w*x),其中 w 为常数。
f(x) = sin(w*x)             
# 函数 sinc(x) 为 sin(pi*x)/(pi*x)。
sinc(x) = sin(pi*x)/(pi*x)  
# 函数 delta(t) 为脉冲函数。
delta(t) = (t == 0) 
# 函数 ramp(t) 当其小于零为零,当其大于零为斜率等于 1 的直线。
ramp(t) = (t > 0) ? t : 0 
# 函数 min(a,b) 取两者中较小的数。
min(a,b) = (a < b) ? a : b
comb(n,k) = n!/(k!*(n-k)!)
len3d(x,y,z) = sqrt(x*x+y*y+z*z)
plot f(x) = sin(x*a), a = 0.2, f(x), a = 0.4, f(x)

gnuplot 已定义的常数仅有 pi (pi = 3.14159)




  • 5
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值