matlab 使用老出错,决定试试scilab.
几个例子来学习 scilab
例1: 画等值线的命令
//定义一个向量
x=[ 1 2 3 ; 1 3 5 ; 8 2 8];
//和matlab 相同的命令
a=1:3;
b=1:3;
nz=2;
//画等值线图,nz 表示等值线的取值点数。
contour2d(a,b,x,nz)
上面的命令可以存在一个文件里,例如 a.sci, 然后在scilab的命令行里可以用
exec("a.sci")
来执行,就会看到画的图如下
例2: matlab的reshape命令,在scilab下面是
//定义一个向量
-->x=[1 2 3 4 5 6 6 7 7 8]
x =
1. 2. 3. 4. 5. 6. 6. 7. 7. 8.
//向量的维数
-->size(x)
ans =
1. 10.
//定义个矩阵, 相当于matlab的 y=reshape(x,2,5)
-->y=matrix(x,2,5)
y =
1. 3. 5. 6. 7.
2. 4. 6. 7. 8.
例3:文件,输入输出
// 新建一个文件,向文件中输入随机数
u=file("open","aa.txt","unknown")
for k=1:4
// 生成 1-by-4 的随机矩阵
a=rand(1,4)
write(u,a)
end
//rewind 使得文件的指针回到文件头。
file("rewind",u)
//读出2-by-4的矩阵
x=read(u,2,4)
//关闭文件
file("close",u)
例4:和waterfall 和像的画图函数
x 如果是一个矩阵,则
mesh(x)会的得到一个图像,如下图
例5: 把生成的图像转化成eps格式保存
//设置标题
xtitle("Nk=1024, k=431:592")
//存成eps格式的文件,文件名将会是f1024zoom.eps
xs2eps(0,"f1024zoom")
例6: 打开和写入文件可以使用 C 程序的格式
p=mopen("s2xx1024.sci","r");
Nx=81;
Nk=1024;
d=512; d1=Nk/2-d+1; d2=Nk/2+d;
//文件保存的是Nx 行 Nk 列的矩阵
//下面的f读入所有的数据, %lg 表示读入 double 精度
f=mfscanf(-1,p,"%lg");
// 把上面读入的一维的向量转成矩阵,要注意行列参数的顺序, 转成 Nk 行 Nx 列的矩阵
f=matrix(f,Nk,Nx);
//再次转置才得到我们想要的 Nx 行 Nk 列的矩阵
f=f";
mclose(p);
p1=mopen("k1.txt","w");
k1=1:Nk;
k=(k1-Nk/2-0.5)/Nk;
kk=k(d1:d2);
b = kk";
pos = 1;
for pos
y=f(pos,1:Nk);
yy=y(d1:d2);
mv = mean(yy);
b = [b yy"];
mfprintf(p1,"%16.15e %16.15e
",b)
file("close",p1);
例7: 找一个好的spline 曲线来拟合数据
// this is an artifical example where the datas xd and yd
// are build from a perturbed sin function
a = 0; b = 2*%pi;
sigma = 0.1; // standard deviation of the gaussian noise
m = 200; // number of experimental points
//下面的函数给出[a,b]上200个等分点,包括两个端点。
xd = linspace(a,b,m)";
yd = sin(xd) + grand(xd,"nor",0,sigma);
//我们要构造一个spline曲线,使得曲线在点
//x(i) , i=1,2,...,6的函数值等于 y(i),
//曲线在点 x(i) 的导数值等于 d(i)
n = 6; // number of breakpoints
x = linspace(a,b,n)";
// compute the spline
//lsq_splin 函数就是计算这样的spline 曲线,记住 spline 曲线和
// {y(i), d(i), = 1,2,。。。,6} 有这一一对应的关系,lsq_splin
//计算出来的一条是原来给出的 xd,yd 这条曲线最好的逼近。
//逼近的好坏用
_m_ _m_
2 2
/ wd(k) (s(xd(k)) - yd(k)) <= / wd(k) (f(xd(k)) - yd(k))
--- ---
k=1 k=1
来判断。
[y, d] = lsq_splin(xd, yd, x); // use equal weights
// plotting
ye = sin(xd);
ys = interp(xd, x, y, d);
//xbasc 擦去原来的图像,准备画新图。
xbasc()
plot2d(xd,[ye yd ys],style=[2 -2 3], ...
leg="exact function@experimental measures (gaussian perturbation)@fitted spline")
xtitle("a least square spline")
xselect()
例8: 数据拟合
有一些数据 X(i), Y(i), 然后选定一个函数形式来这些数据。
这个函数形式中有一些参数,scilab 提供确定这些参数的方法。
//确定函数的形式是 y = a*(x-b) + c*x^2
// a, b, c 是需要确定的参数
deff("y=FF(x)","y=a*(x-b)+c*x.*x")
X=[];Y=[];
a=34;b=12;c=14;for x=0:.1:3, Y=[Y,FF(x)+100*(rand()-.5)];X=[X,x];end
Z=[Y;X];
//误差函数 G(p,z), 不同的参数,不同的误差,找到使得误差的最小的参数。
deff("e=G(p,z)","a=p(1),b=p(2),c=p(3),y=z(1),x=z(2),e=y-FF(x)")
//3;5;10 是三个参数的初始值
[p,err]=fit_dat(G,[3;5;10],Z)
xset("window",0)
xbasc();
plot2d(X",Y",-1)
plot2d(X",FF(X)",5,"002")
a=p(1),b=p(2),c=p(3);plot2d(X",FF(X)",12,"002")
a=34;b=12;c=14;
//还可以使用误差函数的导数的信息。
deff("s=DG(p,z)","y=z(1),x=z(2),s=-[x-p(2),-p(1),x*x]")
[p,err]=fit_dat(G,[3;5;10],Z,DG)
xset("window",1)
xbasc();
plot2d(X",Y",-1)
plot2d(X",FF(X)",5,"002")
a=p(1),b=p(2),c=p(3);plot2d(X",FF(X)",12,"002")
例9: 数据拟合的improved version of fit_data
//generate the data
function y=FF(x,p),y=p(1)*(x-p(2))+p(3)*x.*x,endfunction
X=[];Y=[];
pg=[34;12;14] //parameter used to generate data
for x=0:.1:3, Y=[Y,FF(x,pg)+100*(rand()-.5)];X=[X,x];end
Z=[Y;X];
//The criterion function
function e=G(p,z),
y=z(1),x=z(2);
e=y-FF(x,p),
endfunction
//Solve the problem
p0=[3;5;10]
[p,err]=datafit(G,Z,p0);
//下面这个行是为了画图,第一个说用window(0), 第二个是清除原来的图像。
scf(0);clf()
plot2d(X,FF(X,pg),5) //the curve without noise
plot2d(X,Y,-1) // the noisy data
plot2d(X,FF(X,p),12) //the solution
//the gradient of the criterion function
function s=DG(p,z),
a=p(1),b=p(2),c=p(3),y=z(1),x=z(2),
s=-[x-b,-a,x*x]
endfunction
[p,err]=datafit(G,DG,Z,p0);
scf(1);clf()
plot2d(X,FF(X,pg),5) //the curve without noise
plot2d(X,Y,-1) // the noisy data
plot2d(X,FF(X,p),12) //the solution
scilab 和 matlab 的主要差别 (汉字部分是我用过的)
函数
Functions in Scilab are NOT Matlab m-files but variables. One or several functions can be defined in a single file (say myfile.sci). The name of of the file is not necessarily related to the the name of the functions. The name of the function(s) is given by
function [y]=fct1(x)...function [y]=fct2(x)...The function(s) are not automatically loaded into Scilab. Usually you have to execute the command getf("myfile.sci") before using it.
Functions can also be defined on-line (or inside functions) by the command deff.
命令行构成的脚本文件,matlab 只要输入文件名即可,scilab的执行方式是输入 exec("filename")
Comment lines
Scilab comments begins with: //
Matlab comments begins with: %
Variables
Predefined variables usually have the % prefix in Scilab (%i, %inf, ...). They are write protected.
Strings
Strings are considered as 1 by 1 matrices of strings in Scilab. Each entry of a string matrix has its own length.
Boolean variables
Boolean variables are %T, %F in Scilab and 0, 1 in Matlab. Indexing with boolean variables may not produce same result. Example x=[1,2];x([1,1]) [which is NOT x([%T,%T])] returns [1,1] in Scilab and [1,2] in Matlab. Also if x is a matrix x(1:n,1)=[] or x(:)=[] is not valid in Matlab.
Polynomials
Polynomials and polynomial matrices are defined by the function poly in Scilab (built-in variables). They are considered as vectors of coefficients in Matlab.
Empty matrices
[ ]+1 returns 1 in Scilab and [ ] in Matlab.
Plotting
Except for the simple plot and mesh (plot3d) functions, Scilab and Matlab are not compatible.
翻译的中文文档: cintro.pdf