《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)









































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