1、矩陣編程
一、定義和簡單使用(通常的編程語言,數組下標都是從0開始的,可是MATLAB是從1開始的) 數組
>> a=[1 4 7; 2 5 8; 3 6 9] a = 1 4 7 2 5 8 3 6 9 >> b=[2 3 4;3 4 5;4 5 3]; >> c=[1;2;3]; >> a+b ans = 3 7 11 5 9 13 7 11 12 >> a*b ans = 42 54 45 51 66 57 60 78 69 >> a*c ans = 30 36 42
二、經常使用矩陣運算和函數編程語言
A':矩陣轉置;函數
A+B,A-B,A*b:矩陣加減;spa
inv(A):矩陣求逆;code
rank(A):矩陣的秩;blog
B/A:等價於B*inv(a);ci
A.*B:點乘,即對應元素相乘;it
A(i,:),A(:,j):第i行,第j列;class
zeros(n):n階零矩陣;
eye(n):單位矩陣;
[X,D]=eig(A):X,特徵向量,D,特徵值
A([i,j],:)=A([j,i],:0):第i行和第j行交換位置;
2、極限(limit),求導(diff),積分(int)
>> F=sym('(1+a/x)^x'); >> limit(F,'x',inf,'left') ans = exp(a) >> syms x; >> y=log((x+2)/(1-x)); >> diff(y,x) ans = ((1/(x - 1) - (x + 2)/(x - 1)^2)*(x - 1))/(x + 2) >> diff(y,x,3) ans = (2*(1/(x - 1) - (x + 2)/(x - 1)^2)*(x - 1))/(x + 2)^3 - (2*(2/(x - 1)^2 - (2*(x + 2))/(x - 1)^3))/(x + 2) - (2*(1/(x - 1) - (x + 2)/(x - 1)^2))/(x + 2)^2 + (2*(2/(x - 1)^2 - (2*(x + 2))/(x - 1)^3)*(x - 1))/(x + 2)^2 + ((6/(x - 1)^3 - (6*(x + 2))/(x - 1)^4)*(x - 1))/(x + 2) >> y=x^5+y^3-sqrt(x)/4; >> int(y) Warning: Explicit integral could not be found. ans = int(log(-(x + 2)/(x - 1))^3, x) - x^(3/2)/6 + x^6/6 >> pretty(ans) 3 - / 2 6 | / x + 2 \3 x x | log| - ----- | dx - -- + -- / \ x - 1 / 6 6
3、繪圖
經常使用的繪圖函數有fplot,plot,plot3,mesh,還有一個輔助函數meshgrid。fplot是根據一個已知的函數表達式畫圖,plot是畫一個二維圖,已知x,y的座標,plot3是畫三維圖,mesh是畫有顏色的三維網狀(將空間中每三個點連成一個三角片)圖。
fplot('x^3+2*x^2+exp(x)',[-3,1]); subplot(2,2,1); fplot('x^3+2*x^2+exp(x)',[-3,1]); title('fplot'); x=-3:0.1:1; y=x.^3+2*x.^2+exp(x); subplot(2,2,2); plot(x,y); title('plot'); t=0:0.1:6; x=t.^3; y=cos(t); z=sin(2*t); subplot(2,2,3); plot3(x,y,z); title('plot3'); subplot(2,2,4); x=-10:0.1:5; y=-10:0.1:10; [x,y]=meshgrid(x,y); z=sqrt(x.^2+y.^2); mesh(x,y,z); title('mesh');
運行(F5)結果如圖所示: