《GNU_Octave_Beginner_s_Guide》读书笔记3:函数与绘图

《GNU_Octave_Beginner_s_Guide》读书笔记3:函数与绘图


函数:
函数的通用形式:[output 1, output 2, ...] = function name(input 1, input 2, ...)
数学函数
辅助函数
操作函数


数学函数:
cos(pi)  //pi是一个内置函数
x = [0:pi/2:2*pi]  //生成一个序列,即一个行向量
cos(x) 
cos(x')  //列向量形式
>> cos(x')   //大多数数学函数都是元素级操作
ans =
  1.0000e+000
  6.1230e-017  //0
  -1.0000e+000
  -1.8369e-016 //0
  1.0000e+000
数学函数速查:
sin
cos
tan
cot
log
exp
power
sec  //正割函数
csc  //余割函数
log10
sqrt
类别有很多,每类都有对应的逆函数和逆双曲函数。 sin  asin  asinh
>> exp(I*pi)
ans = -1.0000e+000 + 1.2246e-016i  //-e
多项式:
>> c=[2 10.1 0 6];
>> polyval(c, 0)
ans =  6   //计算 f(x)=2*x三次方+10*x二次方+6,当x=0时的值
>> polyval(c,[0:pi/2:2*pi])
ans =
     6.0000    38.6723   167.6956   439.5791   900.8324  //计算一个序例
>> x=[0:pi/2:2*pi];2*x.^3+10.1*x.^2+6
ans =
     6.0000    38.6723   167.6956   439.5791   900.8324  //同上
包括各类专业数学函数,如贝塞耳(Bessel functions)函数 ,参看手册。
>> x=[0:0.1:1];exp(-5*sqrt(x)).*sin(2*pi*x)   //注意元素级操作‘.’很有用。
ans =
 Columns 1 through 6:
   0.00000   0.12093   0.10165   0.06150   0.02488   0.00000
 Columns 7 through 11:
  -0.01222  -0.01450  -0.01086  -0.00512  -0.00000  //另一个计算序列


辅助函数:
如多前的ismatrix, real, eye, ones,参见上一篇博客。http://blog.csdn.net/vagrantabc2017/article/details/77854598
ones(3,3);zeros(3,3); rand(3,3);  //rand是均匀分布
ones;zeros;rand; //只生成一个数,但这里没存
randn //正态分布
randg //伽马gamma分布
rande //指数exponential分布
randp //泊松Poisson分布
>> A=rand(3,5);min(min(A))
ans =  0.031716  //先找每列中最小的数,形成一个行向量;再找行向量中最小的数。
octave在缺省时以每列为单元进行操作。
>> sort(A)  //把矩阵按列升序排序
>> [SS tt]=sort(A,2,"descend")  //2指把A按行排序,若按列排用1; tt输出原来的下标的位置; 降序
SS =
   0.866961   0.780991   0.449936   0.363544   0.031716
   0.787317   0.552595   0.465141   0.363426   0.218747
   0.963004   0.876864   0.789374   0.531654   0.156382
tt =
   1   3   4   5   2
   4   3   1   5   2
   5   4   3   2   1
很多函数有sort类似的用法。
>> [i j]=find([0 0 ;0 1 ])  //找到非0元素的下标,如果不止一个,会全部给出。
i =  2
j =  2
>> A
A =
   0.866961   0.031716   0.780991   0.449936   0.363544
   0.465141   0.218747   0.552595   0.787317   0.363426
   0.156382   0.531654   0.789374   0.876864   0.963004
>> [i,j]=find(A<0.4);A(i(1),j(1))  //找到A的以列方式查找的小于0.4的第一个数。
ans =  0.15638
>> any([0 0; 1 0])  //矩阵中,每列运算,若有一个元素是非零的,为真。
ans =
  1  0
>> all([0 0; 1 0])  //矩阵中,每列运算,全部元素是非零的,为真。
ans =
  0  0
>> floor([1.1 2.2 5.5 9.9])
ans =
   1   2   5   9
>> ceil(2.01)
ans =  3
>> ceil(-2.01)
ans = -2
>> round(1.4999)
ans =  1
>> fix(1.999)  //截掉小数部分
ans =  1
>> fix(-1.999)
ans = -1
>> sum([1 2; 3 4])  //接列加总
ans =
   4   6
>> >> prod([1:4])
ans =  24
>> cumsum([1 2; 3 4]) //每列完成 sum+=i;
ans =
   1   2
   4   6
>> cumprod([1 2 3 4]) //sum*=i;
ans =
    1    2    6   24
复数比较:
>> abs(-2-2i)
ans =  2.8284  //到原点的距离
>> max([1+2i, 2+2i, 2-0.1i])  //在复数的比较运算中,比较的都是绝对值,即距离。如sort排序。
ans =  2 + 2i


操作函数
determinant:行列式
>> A=[2 1 -3; 4 -2 -2; -1 0.5 -0.5];det(A)   //计算行列式的值,用于n*n的矩阵
ans =  8
eigenvalues:特征值
>> A = [1 2; 3 4];eig(A)  //计算行列式的特征值,特征值的定义: det(A - lambda.I)=0
ans =
  -0.37228
   5.37228
eigenvectors:特征向量
>> A = [1 2; 3 4];[V, L] = eig(A)
V =                                //特征向量,V的列向量
  -0.82456  -0.41597
   0.56577  -0.90938
L =                                //特征值
Diagonal Matrix
  -0.37228         0
         0   5.37228
定义:
设 A 是n阶方阵,如果存在数m和非零n维列向量 x,使得 Ax=mx 成立,则称 m 是A的一个特征值(characteristic value)或本征值(eigenvalue)。非零n维列向量x称为矩阵A的属于(对应于)特征值m的特征向量或本征向量,简称A的特征向量或A的本征向量。
Ax=mx,等价于求m,使得(mI-A)x=0,其中E是单位矩阵,0为零矩阵。
|mE-A|=0,求得的m值即为A的特征值。|mE-A| 是一个n次多项式,它的全部根就是n阶方阵A的全部特征值,这些根有可能相重复,也有可能是复数。
如果n阶矩阵A的全部特征值为m1 m2 ... mn,则|A|=m1*m2*...*mn
同时矩阵A的迹是特征值之和:tr(A)=m1+m2+m3+…+mn
设Bx=b,B的零空间关心的是当b为零向量,即Bx=0时所有的解x.
(mI-A)x=0 ==> (A-mI)x=0 ==>Bx=0(零空间的定义), 定义B=(A-mI),这里可以直接计算B,也可以计算特征向量,这里做一个交叉验证,拿第一个特征向量为[-0.82456 0.56577]'例;
>> A = [1 2; 3 4];[V, L] = eig(A);m=L(1,1);B = A-m*eye(2);null(B)  
ans =
  -0.82456
   0.56577
>> V(:,1)
ans =
  -0.82456
   0.56577   //两个是一样的,只是验证一下
Orthonormal:正交
线性代数操作函数:
det          //求行列式的
inv/inverse  //求逆矩阵
orth         //求正交范围空间
eig          //求特征值和特征向量
null         //求正交零空间
rank         //矩阵评级
多项式操作函数:
>>  c=[2 10.1 0 6];roots(c)  //求一元三次方程的根: f(x)=2*x^3+10.1*X^2+0*x^1+6=0;
ans =
  -5.16256 + 0.00000i
   0.05628 + 0.76022i
   0.05628 - 0.76022i
>> c=[2 -6];roots(c)    //换个简单点的: 2x=6; x=3
ans =  3
>> polyder(c)           //求f(x)的微分,函数名有变化,原为polyderive
ans =
    6.00000   20.20000    0.00000
>> polyint(c)            //求f(x)的积分,函数名有变化,原为polyinteg
ans =
   0.50000   3.36667   0.00000   6.00000   0.00000


绘图:plot 和 set
有两套绘图系统:fltk和gnuplot,两套系统切换命令:
>> graphics_toolkit("fltk")
>> graphics_toolkit("gnuplot")
>> available_graphics_toolkits  //查看可以加载的绘图命令
ans =
{
  [1,1] = fltk
  [1,2] = gnuplot
  [1,3] = qt
}
>> c=[2 10.1 0 6];x = [-5.5:0.1:1]; f = polyval(c,x);plot(x, f)  //绘出f(x)的图
plot的通用语法:plot(x, y,fmt, property, value, ...)  //property指属性名,value指属性值
set的通用语法:set(handle, property, value, ...)  //set给handler赋属性值,修改图的局部风格,但由于没有x,y信息,自己画不了图。
>> plot(x,f,"linewidth",5)  //改绘图线的粗细
>>set(gca, "xlim", [-5.5 1]) //x轴限制,gca是一个函数名,指get current axis。
>>set(gca, "linewidth",2)  //修改边框盒座标的线的粗细
>> set(gca,"fontsize",25)  //修改边框盒座标的刻度的文字大小
>> set(gca, "xlabel", text("string", "x", "fontsize", 25))  //加x轴的描述文字
>> set(gca, "ylabel", text("string", "f(x)","fontsize", 25)) //加y轴的描述文字
>>set(gca, "xlim", [-5.5 1], "linewidth", 2, "fontsize", 25, //合并
"xlabel", text("string", "x", "fontsize", 25), "ylabel",
text("string", "f(x)", "fontsize", 25))
>> line([-5.16 -4], [-2 -20], "linewidth", 2)  //从(-5.16, -2) 到 (x, y)=(-4, -20)画直线,[-5.16 -4]是x向量,另一个是y向量
>> text(-3.9, -23, "root", "fontsize", 20);  //写上root,前面的x,y座标是起始点
>> line([0 0], [5 -20], "linewidth", 2)  //另一条线
>> text(-1.0, -22, "local minimum", "fontsize", 20)  //另一段文字
>> plot(x, f, "o")  //用小圆圈来画线,其它可用的符号包括:*, +, x, ., ^,这些符号可以用减号-连在一起用。
>> plot(x,f, "o", "markersize", 4); //用小小圆圈来画线
>> plot(x, f, "o-", "markersize", 4, "linewidth", 2, "color","red")  //线串起了红色的珍珠
>> set(gca, "xlim", [-5.5 1], "ylim", [-40 60], "linewidth", 2,
"fontsize", 25, "xlabel", text("string", "x", "fontsize", 25),
"ylabel", text("string", "f(x)", "fontsize", 25))  //全景图
标题与图例
figure:指大的图   graphs:指图线
>> legend("f(x)")   //增加f(x)的图例
>> set(gca, "title", text("string","My favorite polynomial","fontsize", 30)) //加一个标题
刻度齿:
>> set(gca, "ytick", [-40:20:60])   //设置y轴刻度齿, xtick
网格:
>> grid on
>> grid off
画数学函数图:fplot
>> fplot("sin", [0 2*pi], 50) //画sin,[0,2pi], 50个点拟合, 非常方便!
>> clf  //清空图
在一张图上画多条线:
f1(x)=2*x^3+10.1*x^2+6;
f2(x)=2*x^3+10.1*x^2-10.1*x+6:
>> x = [-5.5:0.1:2]; c_1 = [2 10.1 0 6];c_2 = [2 10.1 -10.1 6];
>> f_1 = polyval(c_1, x); f_2=polyval(c_2, x);
>> plot(x, f_1, "linewidth", 5, x, f_2,"linewidth", 5, "color", "red")  //画两条线,默认是蓝色,f2是红色。
>> clf  //清屏
>> plot(x, f_1, "linewidth", 5);
>> hold on                                       //别清屏,好让两条线在一个图上。
>> plot(x, f_2, "linewidth", 5, "color", "red")
>> hold off
>> text(-3.9, -23, "f_1(x)")  //注意:f_1,因为1前面是下划线,所以1被显示为下标。
>> text(-3.9, -23, "f_{123}(x)")  //123都为下标
>> text(-3.9, -3, "f^{123}(x)")   //123都为上标,gnuplot上测试,其它未测
>> figure(2)  //开第二个图形窗口
>> figure(1)  //指定当前图形窗口为1号窗口
>> figure(2)  //再次切换
>> gcf        //查询当前窗口是2号
ans =  2
子图:
>> subplot(2,3,1)  //分为2*3个子图,当前为1号子图,一排一排数
>> subplot(2,3,2)   //切到2号子图工作区
任务:画一个insets:
>> plot(x,f_1, "linewidth", 5)
>> set(gca, "xlim", [-6 2.5], "ylim", [-50 70])
>> axes("position",[0.3 0.2 0.3 0.3])    //子窗口左下角位于视口(30%,20%)的位置,宽30%,高20%. 视口包括上面菜单和下面位置栏。
>> plot(x, f_2, "color","red", "linewidth", 5)    //inset之间不能切换上下文,只能一次成功。
>> print("polynom.png", "-dpng"); //当前工作区应出现一个polynom.png文件, -d指device.
print支持的文件格式包括:eps,ps,pdf,jpg/jpeg,gif,tex,pslatex,png.
三维绘图:
f(x,y)=x^2-y^2; //一个曲面
f(x)=[cos(x),sin(x),exp(-x/2)];  //三维空间中的向量曲线


>> x = [-2:0.1:2]; y = x;  //定义域
>> [X Y] = meshgrid(x,y);   //生成网格
>> Z = X.^2 - Y.^2;  //计算曲面Z=f(x,y)
>> surface(X,Y, Z)   //需要用rotate工具翻转一下才能看到效果。
>> surface(X,Y,Z, "linewidth", 4)
>> set(gca, "linewidth", 2, "fontsize", 20, "xlim", [-2 2])
>> set(gca, "xlabel", text("string", "x", "fontsize", 30))
>> set(gca, "ylabel", text("string", "y", "fontsize", 30))
>> set(gca, "zlabel", text("string", "z", "fontsize", 30))
>> text(-3.2, 1, 3, "f(x,y)", "fontsize", 30)
>> line([0 0], [0 1], [0 2], "linewidth", 5, "color", "red")
>> text(-0.5, 1.5, 1.8, "Saddle point", "fontsize", 25)


鞍点图:




视角与颜色图:
φ :方位,空间向量线在xy平面的投影与x轴的夹角。
θ :仰角,空间向量线在xy平面的投影与空间向量线本身的夹角。


>> view(35,30)  //(φ, θ)=(35,30)
>> colormap("gray"); view(-35, 30);
>> colormap("summer"); view(0,0);  //不同的角度和颜色
可选颜色包括:
jet (default) summer copper
hsv spring gray
hot autumn bone
cool winter pink
轮廓图:contour, contourf, and contour3(三维轮廓图).
与surface函数调用方式一样。
>> contourf(X,Y,Z, 20);  //20指轮廓线的根数
>> contour3(X,Y,Z, "linewidth", 6);


三维空间曲线:f(x)=[cos(x),sin(x),exp(-x/2)];
>> x = linspace(0, 10*pi)';   //x在[1,10pi]
>> f = [cos(x), sin(x), exp(-0.5*x)];  //直接得到f的函数值
>> size(f)
ans =
   100     3        //100*3的矩阵,分别表示x,y,z的座标
>> plot3(f(:,1), f(:,2), f(:,3), "linewidth", 4)  //画出空间曲线
>> plot3(f(:,1), f(:,2), f(:,3), "linewidth", 4)
>> set(gca, "linewidth", 2, "fontsize", 20);
>> set(gca, "xlabel", text("string", "x","fontsize", 30));
>> set(gca, "ylabel", text("string", "y","fontsize", 30));
>> set(gca, "zlabel", text("string", "z","fontsize", 30));
>> set(gca, "zlim", [0 1.2])
>> text(0.9, -0.25, 0.9, "t=0", "fontsize", 30)
>> view(20,30)









































發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章