科学计算自由软件SCILAB

科学计算自由软件SCILAB Online Help
scilab中文帮助手册

Scilab keywords
backslash — (\) left matrix division.
brackets [,;] — Concatenation. Recipients of an assignment. Results of a function
colon (:) — Ranging operator. Addresses all elements along an array dimension or of a list.
comma — (,) comma; instruction, argument separator
comments — (// or /*...*/) comments
comparison — comparison, relational operators
dollar — ($) last index
dot — (.) symbol
equal — (=) assignment , comparison, equal sign
extraction — matrix and list entry extraction
getscilabkeywords — returns a list with all scilab keywords.
hat — (^) exponentiation
assignation — partial variable assignation
greater — (>) greater than comparison
minus — (-) subtraction operator, sign changes
not — (~) logical not
parentheses — ( ) left and right parenthesis
percent — (%) special character
plus — (+) addition operator
quote — (') transpose operator, string delimiter
semicolon — (;) ending expression and row separator
slash — (/) right division and feed back
star — (*) multiplication operator
symbols — scilab operator names
tilde — (~) logical not
Control flow
abort — interrupt evaluation.
break — keyword to interrupt loops
case — keyword used in statement "select"
continue — keyword to pass control to the next iteration of a loop
do — language keyword for loops
else — keyword in if-then-else and select-case-then-else
elseif — keyword in if-then-else
end — end keyword
for — language keyword for loops
halt — stop execution
if — keyword for conditional execution
pause — pause mode, invoke keyboard
resume — return or resume execution and copy some local variables
return — return or resume execution and copy some local variables
select — select keyword
then — keyword in control flows 'if' and 'select'
catch — beginning of catch block in try-catch control instruction
whilewhile keyword
Matrix generation
diag — diagonal including or extracting
empty — ([]) empty matrix
eye — identity matrix
linspace — generates linearly spaced numbers between 2 reached bounds
logspace — logarithmically spaced vector
meshgrid — create matrices or 3-D arrays
ndgrid — build matrices or N-D arrays by replicating some template vectors
ones — matrix made of ones
rand — Random numbers
repmat — Replicate and tile an array
squarewave — generates a square wave with period 2*%pi
testmatrix — generate special matrices, such as Hilbert, Franck
toeplitz — Toeplitz matrix (chosen constant diagonal bands)
zeros — matrix made of zeros
Log - exp - power
exp — element-wise exponential
expm — square matrix exponential
log — natural logarithm
log10 — base 10 logarithm
log1p — computes with accuracy the natural logarithm of its argument added by one
log2 — base 2 logarithm
logm — square matrix logarithm
nthroot — Real nth root of real numbers
polar — polar form
power — (^,.^) power operation
sqrt — square root
sqrtm — matrix square root
Floating point
ceil — round up
clean — cleans matrices (round to zero small entries)
double — converts inttype integers or booleans into decimal encoding
fix — round towards zero
floor — round down
formatset the default output format of decimal numbers
frexp — dissect floating-point numbers into base 2 exponent and mantissa
ieee — sets or gets the floating point exception mode
int — round towards zero
isinf — tests for infinite elements
isnan — check for "Not a Number" entries
nearfloat — get previous or next floating-point number
nextpow2 — next higher power of 2.
number_properties — determine floating-point parameters
round — round to nearest integer
Radix conversions
base2dec — convert from base b to decimal
bin2dec — convert from binary to decimal
dec2base — Convert decimal to base N number in string
dec2bin — convert from decimal to binary
dec2hex — convert from decimal to hexadecimal
dec2oct — convert from decimal to octal
hex2dec — convert from hexadecimal to decimal
oct2dec — convert from octal to decimal
Matrix operations
abs — absolute value, magnitude
and — logical AND between the elements of a boolean or numerical array
cross — vector cross product
cumprod — cumulative product of array elements
cumsum — cumulative sum of array elements
kron — Kronecker product (.*.)
max — maximum
min — minimum
norm — norms of a vector or a matrix
or — logical OR over the elements of a boolean or numerical array
prod — product of array elements
sign — signum function
signm — matrix signum function
sumsum of array elements
tril — lower triangular part of matrix
triu — upper triangle
unwrap — unwrap a Y(x) profile or a Z(x,y) surface. Unfold a Y(x) profile
Search and sort
dsearch — distribute, locate and count elements of a matrix or hypermatrix in given categories
findfind indices of boolean vector or matrix true elements
gsort — sorting by quick sort algorithm
members — count (and locate) in an array each element or row or column of another array
vectorfind — finds in a matrix rows or columns matching a vector
Trigonometry
acos — element wise cosine inverse (radians)
acosd — element wise cosine inverse, result in degree.
acosh — hyperbolic cosine inverse
acoshm — matrix hyperbolic inverse cosine
acosm — matrix wise cosine inverse
acot — computes the element-wise inverse cotangent of the argument.
acotd — computes the element-wise inverse cotangent of the argument, result in degree.
acoth — element wise hyperbolic cotangent inverse.
acsc — computes the element-wise inverse cosecant of the argument.
acscd — computes the element-wise inverse cosecant of the argument, results in degree.
acsch — computes the element-wise inverse hyperbolic cosecant of the argument.
asec — computes the element-wise inverse secant of the argument.
asecd — computes the element-wise inverse secant of the argument, results in degree.
asech — computes the element-wise inverse hyperbolic secant of the argument.
asin — sine inverse (radians)
asind — sine inverse, results in degree
asinh — hyperbolic sine inverse
asinhm — matrix hyperbolic inverse sine
asinm — matrix wise sine inverse
atan — 2-quadrant and 4-quadrant inverse tangent
atand — 2-quadrant and 4-quadrant element-wise inverse tangent, result in degree
atanh — hyperbolic tangent inverse
atanhm — matrix hyperbolic tangent inverse
atanm — square matrix tangent inverse
cos — cosine function
cosd — element-wise cosine function, argument in degree
cosh — hyperbolic cosine
coshm — matrix hyperbolic cosine
cosm — matrix cosine function
cotd — element-wise cotangent function, argument in degree
cotg — cotangent
coth — hyperbolic cotangent
cothm — matrix hyperbolic cotangent
csc — сomputes the element-wise cosecant of the argument
cscd — сomputes the element-wise cosecant of the argument given in degree
csch — Computes the element-wise hyperbolic cosecant of the argument.
csgn — returns the sign of a vector of real of complex values
sec — computes the element-wise secant of the argument
secd — computes the element-wise secant of the argument given in degree
sech — computes the element-wise hyperbolic secant of the argument
sin — sine function
sinc — sinc function
sind — sine function, argument in degree.
sinh — hyperbolic sine
sinhm — matrix hyperbolic sine
sinm — matrix sine function
tan — tangent
tand — tangent, argument in degree.
tanh — hyperbolic tangent
tanhm — matrix hyperbolic tangent
tanm — matrix tangent
&, && — Binary AND between integers. Logical AND over/between booleans and numbers
|, || — Binary OR between integers. Logical OR over/between booleans and numbers

基本操作
Matlab的注释是用“%”引导;在Scilab里面是“//”。
% prefix in Scilab (%i, %inf, …). %T, %F in Scilab and 0, 1 in Matlab
在Matlab里,“最后”这个不定变量为“end”,而在Scilab中为“$”

x=[ 1 2 3 ; 1 3 5 ; 8 2 8];  //定义一个向量
size(x) 
xd = linspace(a,b,m)'; 
yd = sin(xd) + grand(xd,"nor",0,sigma); 

定义函数

deff('y=FF(x)','y=a*(x-b)+c*x.*x') 

执行

x0=[-5,5];
exec('F:\sci-figure\fsolvetry.sci');
[x]=fsolvetry(x0); 

函数

function f=fsolvetry(x)
    f=[2*x(1)-x(2)-exp(-x(1)) -x(1)+2*x(2)-exp(-x(2))];
endfunction

rand()*
随机数发生器,它提供了多个工作模式并可以根据需要设置或返回随机分布类型及种子。

write(file-desc,a,[format])
write(file-desc,a,k,format)
[x]=read(file-desc,m,n,[format])
[x]=read(file-desc,m,n,k,format)

file-desc为字符串,给出保存的文件名,a是一个实矩阵或字符串列向量,k是一个整数向量,format指定一个’Fortran’风格的格式
file-desc为要读取的数据文件名,m,n指定读取的矩阵大小,如果不知道行数,可以设置m=-1,则把整个文件的数据都读进来。k为一个

save(filename [,x1,x2,…,xn])
save(fd [,x1,x2,…,xn])
load(filename [,x1,…,xn])
load(fd [,x1,…,xn])

save和load分别用于把变量列表中的变量保存到文件中或从文件中读取,如果省略其中的变量列表,则会保存工作空间中的所有变量或回复之前保存的所有变量。

加载该工具箱
函数atomsLoad()

atomsLoad('IPD') 
a = read_csv('test.txt')
a = evstr(a)
[y,fs,bits]=wavread("C63A 4331440.wav");
Plot(y);
u=file('open','aa.txt','unknown') 
for k=1:4 
a=rand(1,4) 
write(u,a) 
p=mopen('s2xx1024.sci','r'); 
f=mfscanf(-1,p,'%lg'); //下面的f读入所有的数据, %lg 表示读入 double 精度 
f=f'; //再次转置才得到我们想要的 Nx 行 Nk 列的矩阵 
mclose(p); 
p1=mopen('k1.txt','w'); 
mv = mean(yy); 
mfprintf(p1,'%16.15e %16.15e\n',b) 
file('close',p1); 

p=poly(a,vname, [“flag”])
poly用于定义多项式,在scilab中,多项式也是一个数据类型,它通过poly函数进行定义,并且可以对其进行运算。a可以为向量或矩阵,如果给出的是向量,则定义多项式,如果给出的是矩阵,则定义特征多项式。定义的形式由[“flag”]确定,flag可以为roots或coeff。而vname为多项式中的变量符号。roots函数用于求多项式的根。

freq函数
用于计算多项式在特定点上的值,还经常用于计算滤波器或系统的频域相应,其实也就是给出传输函数的形式,然后计算在各个点上的值。它有两个形式
[x]=freq(A,B,C [,D],f)
[x]=freq(NUM,DEN,f)
A,B,C,D,NUM,DEC均用于确定系统传输函数,而f列向量,用于指定要求那些点的值。

syslin
线性系统传输函数可以用有理式或状态空间法进行描述,采用syslin函数定义线性系统函数。
其格式为:
[sl]=syslin(dom,A,B,C [,D [,x0] ])
[sl]=syslin(dom,N,D)
[sl]=syslin(dom,H)
dom取值为‘c’,‘d’或[]或一个标量,表示系统为连续,离散或其它。A,B,C,D用于在状态空间中定义系统。x0为矢量,给出了初始状态 N/D为多项式矩阵 H为有理矩阵或线性状态空间表示
而sl表示线性系统,很多函数可以接受这样的数据类型,其实它也是一个list。

a=0:0.1:2*%pi; 
rand(3,2)                                          建立一个随机矩阵
rand(2,2,'gauss')                         高斯分布即期望为0,方差为1的正态分布
a=poly([1 2 3],'x','coeff')
b=poly([1 2 3],'x','roots')
freq(a,1,[1;2])
syslin('d',a,b)

Scilab的基本二维绘图函数

plot
完整格式为:plot(x1,y1,x2,y2,…,xN,yN,)
通过LineSpec1可以为每个点设置其线型,颜色,标记形式。线型有(-,–,:,-.,),颜色有(r,g,b,c,m,y,k,w,如果没有指定颜色,scilab内部会有一个颜色列表为不同的曲线绘制不同的颜色),标记类型(+,o,*,.,x,‘square’,‘diamon’,^,v,>,<,‘pentagram’,‘none’)。

plot2d
plot2d函数提供了功能强大的二维曲线绘制方法,并可以对线型,坐标轴,标记等进行控制
完整格式为:plot2d([x],y,)
x和y均可以为实矩阵或向量;中的style用于控制绘制线型,大于零为连接的曲线,而小于零则采用标记给出各个点的位置,线型取值为16,而小于零时取值为0-14,通过这些线型的选择,可以绘制出各种各样的曲线形式;**

logflag用于确定坐标轴的尺度为线性或对数形式,可以取值’n’或’l’

plot2d("nl", iter, err, style=2); //“nl” 表示,横坐标为正常的模式(normal),纵坐标为对数(log).

Style = 2 表示的是曲线的颜色。2 表示的是colormap 中的第二项,也就是蓝色。
**;rect用于设置包含曲线的最小矩形,该参数可以与frameflag配合用于指定坐标轴边界;frameflag用于从最小所需值计算真是的坐标范围,可以取值09。axesflag用于指定坐标的绘制方法,可以取值05。nax用于指定坐标的标注,格式为[nx,NX,ny,NY],NX和NY给出X和Y轴的主标注数量,而nx和ny给出X和Y轴的次标注数量;leg用于设置曲线的标题,但是该方法不灵活,采用captions或legend函数比较方便。

plot2dxx(xx=1~4)
形式的二维曲线绘制函数可以通过plot2d2函数选择相应的polyline_style得到相应的效果,因此plot2dxx只是plot2d具有相应线型设置的实现而已。plot2d1已经被淘汰,没有使用

fplot2d
fplot2d绘制由函数定义的曲线,具体格式为:fplot2d(xr,f,),xr为向量,f为y=f(x)形式的外部定义函数。f的值由函数feval(xr,f)计算得到。

mesh(x)
会的得到一个图像

频域
BODE
Syntax
bode(sl)
bode(sl, fmin, fmax)
bode(sl, fmin, fmax, step)
bode(sl, frq)
bode(frq, db, phi)
bode(frq, repf)
bode(…, Captions)
bode(…, Captions, “rad”)

s = poly(0, 's');
h = syslin('c', (s^2+2*0.9*10*s+100)/(s^2+2*0.3*10.1*s+102.01));
clf(); bode(h, 0.01, 100);

s = poly(0, 's');
h1 = syslin('c', (s^2+2*0.9*10*s+100)/(s^2+2*0.3*10.1*s+102.01));
num = 22801+4406.18*s+382.37*s^2+21.02*s^3+s^4;
den = 22952.25+4117.77*s+490.63*s^2+33.06*s^3+s^4;
h2 = syslin('c', num/den);
clf(); bode([h1; h2], 0.01, 100, ['h1'; 'h2']);

s = %s;
G = (10*(s+3))/(s*(s+2)*(s^2+s+2)); // A rational matrix
sys = syslin('c', G); // A continuous-time linear system in transfer matrix representation.
f_min = .0001; f_max = 16; // Frequencies in Hz
clf(); bode(sys, f_min, f_max, "rad"); // Converts Hz to rad/s

gainplot
gainplot(sl,fmin,fmax [,step] [,comments] )
gainplot(frq,db,phi [,comments])
gainplot(frq, repf [,comments])

s=poly(0,'s')
h1=syslin('c',(s^2+2*0.9*10*s+100)/(s^2+2*0.3*10.1*s+102.01))
h2=syslin('c',(s^2+2*0.1*15.1*s+228.01)/(s^2+2*0.9*15*s+225))
clf();gainplot([h1;h2],0.01,100,['h1';'h2'])
title('Gainplot')

phaseplot
frequency phase plot
Calling Sequence
phaseplot(sl)
phaseplot(sl,fmin,fmax [,step] [,comments] )
phaseplot(frq,db,phi [,comments])
phaseplot(frq, repf [,comments])

s=poly(0,'s')
h1=syslin('c',(s^2+2*0.9*10*s+100)/(s^2+2*0.3*10.1*s+102.01))
h2=syslin('c',(s^2+2*0.1*15.1*s+228.01)/(s^2+2*0.9*15*s+225))
clf();phaseplot([h1;h2],0.01,100,..
                   ["$\frac{s^2+18 s+100}{s^2+6.06 s+102.1}$";
                    "$\frac{s^2+3.02 s+228.01}{s^2+27 s+225}$"])
title('Phaseplot')

nyquist
nyquist plot
Syntax
nyquist( sl,[fmin,fmax] [,step] [,comments] [,symmetry])
nyquist( sl, frq [,comments] [,symmetry])
nyquist(frq,db,phi [,comments] [,symmetry])
nyquist(frq, repf [,comments] [,symmetry])

//Hall chart as a grid for nyquist
s=poly(0,'s');
Plant=syslin('c',16000/((s+1)*(s+10)*(s+100)));
//two degree of freedom PID
tau=0.2;xsi=1.2;
PID=syslin('c',(1/(2*xsi*tau*s))*(1+2*xsi*tau*s+tau^2*s^2));
clf();
nyquist([Plant;Plant*PID],0.5,100,["Plant";"Plant and PID corrector"]);
hallchart(colors=color('light gray')*[1 1])
//move the caption in the lower right corner
ax=gca();Leg=ax.children(1);
Leg.legend_location="in_upper_left"

**;

subplot(221);
分割子窗口
xgrid为二维图形增加格点
具体格式为:xgrid([style]),style为整数,定义格点的风格
xnumb绘制数值
具体格式为:xnumb(x,y,nums,[box,angle]);x,y,nums为大小相同的矢量,分别给出位置,数值
xstring绘制字符串
具体格式为:xstring(x,y,str,[angle,box]);x,y,str为大小相同的矢量,str为字符串
xarrows绘制一组箭头
具体格式为:xarrows(nx,ny,[arsize,style]);nx,ny为大小相同的矢量,arsize定义箭头头的大小,style定义风格。
titlepage
在图形窗口中心添加一个标题

保存图片
xs2png
xs2fig
xs2pdf
xs2gif
xs2svg
xs2jpg
xs2ps
xs2bmp
xs2emf
xs2ppm

ind = linspace(0,6,7);
iter = 10.^ind;
err1 = 10.^(-ind);
err2 = (10.^(-ind)).^2;
xset("font size", 4);
plot2d("ll", iter, err1, style=2);
plot2d("ll", iter, err2, style=3);
title("Loglog","fontsize", 4);
xlabel("Iterations","fontsize", 4);
ylabel("Error","fontsize", 4);
set(gca(),"grid",[5 5]);
legend(['error1';'error2'],"in_lower_left");

xs2png(0, "pic.png"); //打开网格:
set(gca(),"grid",[1 1]); //关闭网格:
set(gca(),"auto_clear",[-1 -1]);
xset("font size", 4);
xset("background", color);

基于Scilab的数字信号处理

syslin
可以用于系统传输函数及状态空间表示法的线性系统建立,它提供了多种建立方式
[sl]=syslin(dom,A,B,C [,D [,x0] ])
[sl]=syslin(dom,N,D)
[sl]=syslin(dom,H)
dom可以为离散(‘d’)、连续(‘c’)或[]或一个标量n,它指定系统的时域,d为离散系统,c为连续系统,n表示采样周期为n(单位为秒),[]表示未定义。

D为一个多项式矩阵,且p = degree(D(z))则
D(z)=D_0 + z D_1 + z^ 2 D_2 +…+ z^p D_p
y[t] = C x[t] + D_0 u[t] + D_1 u[t+1] +…+ D_[p] u[t+p]
[sl]=syslin(dom,N,D)
[sl]=syslin(dom,H)
定义传输函数,其中N,D用以定义传输函数的有理函数,而H为有理矩阵或线性状态空间表示法。输出结果格式为
sl=tlist([‘r’,‘num’,‘den’,‘dt’],N,D,dom)
或者
sl=tlist([‘r’,‘num’,‘den’,‘dt’],H(2),H(3),dom)
由syslin定义的传输函数表示或状态空间表示的线性系统可以如普通矩阵那样进行操作,即连接,提取,转置,相乘等等。大多数的状态空间控制函数都会接受syslin定义的系统表示方式。

sl=tf2ss(h [,tol])
用于把传输函数转换成状态空间表示,[Ds,NUM,chi]=ss2tf(sl)用于把状态空间表示法转换成传输函数
h为有理函数矩阵,tol为容差,可以是相对容差rtol或行向量[rtol atol]
sl为状态空间表示的线性系统

两个系统之间可以采用多种方式进行连接,主要有S1*S2,S1+S2,[S1;S2],[S1,S2]
dscr
转换成离散系统,其格式为[sld [,r]]=dscr(sl,dt [,m])
sl为由syslin定义的包含矩阵[A,B,C,D]的线性系统
dt实数表示采样率
m为输入噪声的协方差
r为输出噪声的协方差
sld为离散线性系统,采用状态空间表示法

计算线性系统的时域响应

[y [,x]]=flts(u,sl [,x0])
[y]=flts(u,sl [,past])
用于计算离散时间或采样系统的时域响应,u为输入向量,sl为线性系统,,x0为初始状态值,默认值为0,past为过去值矩阵,默认值为0;x为输出状态值,y为输出值。

s=poly(0,'s')       //定义符号变量 
H=[2/s,(s+1)/(s^2-5)]  //定义传输函数的有理多项式
D=poly([1,2],'s') 
S1=syslin('c',A,B,C)   //建立状态空间表示法的线性系统 
Sys=tf2ss(H)             //转换成状态空间表示法 
Sys=syslin('c',[1,1/(s+1);2*s/(s^2+2),1/s]) 
z=poly(0,'z'); 
h=syslin('d',(1-2*z)/(z^2+0.3*z+1)) 

freq
frequency response
Syntax
[x]=freq(A,B,C [,D],f)
[x]=freq(NUM,DEN,f)

repfreq
frequency response
Syntax
[ [frq,] repf]=repfreq(sys,fmin,fmax [,step])
[ [frq,] repf]=repfreq(sys [,frq])
[ frq,repf,splitf]=repfreq(sys,fmin,fmax [,step])
[ frq,repf,splitf]=repfreq(sys [,frq])

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值