% MATLAB 6.1 實(shí)用指南
help elmat
help eye
root=solve('a*x^2+b*x+c=0','x')
% 續(xù)行 ...
% LOOKFOR Search all M-files for keyword.
lookfor integral
% pi ,inf無(wú)窮大 i(j)復(fù)數(shù)中的虛數(shù)單位
MATLAB的輸入與輸出語(yǔ)句
--------------------------------------------------------------------------------
輸入語(yǔ)句
輸入數(shù)值
x=input('please input a number:')
please input a number:22
x = 22
輸入字符串
x=input('please input a string:','s')
please input a string:this is a string
x = this is a string
輸出語(yǔ)句
輸出顯示命令
自由格式 (disp)
disp(23+454-29*4)
361
disp([11 22 33; 44 55 66; 77 88 99])
11 22 33
44 55 66
77 88 99
disp('this is a string')
this is a string
格式化輸出 (fprintf)。
fprintf('The area is %8.5f\n', area) % 注意輸出格式前須有%符號(hào),
%跳行符號(hào)須有\(zhòng)符號(hào)
The area is 12.56637 % 輸出值為8位數(shù)含5位小數(shù)
在這里你如果學(xué)過(guò)c語(yǔ)言就能很好的理解了。
錯(cuò)誤消息顯示命令
error('this is an error')
this is an error
%%%%%%%%%%%%%%% 數(shù)組 %%%%%%%%%%%%%%%%%%%%%%%
%% 創(chuàng)建數(shù)組x=(0:0.5:2) first:increment:last 方法2: linspace(first_value,last_value,number)
% logspace(first_value,last_value,number)
A=[2,5,6,3;1,2,3,4]
B=[-7,9,-2,0;1,1,1,1]
c=A+B
c=c+2
c=c/2
%% 隨機(jī)數(shù)[10,50] to generate a 5-by-5 array of uniformly distributed random numbers on the interval [10,50]
a = 10; b = 50;
x = a + (b-a) * rand(5)
% 求行列式大小 det
a=[1 2 0;2 5 -1; 4 10 -1]
b=det(a)
%求逆及轉(zhuǎn)置
c=inv(a)
b=det(c)
B=C'
%%
A(:,j)is the j-th column of A
A(i,:)is the i-th row of A
A(:,:)is the equivalent two-dimensional array.
For matrices this is the same as A.
A(j:k)is A(j), A(j+1),...,A(k)
A(:,j:k)is A(:,j), A(:,j+1),...,A(:,k)
A(:,:,k)is the kth page of three-dimensional array A.A(i,j,k,:)is a vector in four-dimensional array A. The vector includes A(i,j,k,1), A(i,j,k,2), A(i,j,k,3), and so on.A(:)is all the elements of A, regarded as a single column. On the left side of an assignment statement, A(:) fills A, preserving its shape from before. In this case, the right side must contain the same number of elements as A.
%%%
A = pascal(3);
B = magic(3);
m = 3; n = 3;
for i = 1:m
for j = 1:n
C(i,j) = A(i,:)*B(:,j);
end
end
%矩陣冪運(yùn)算 ^ 不同于數(shù)組的冪運(yùn)算 .^
A=[1,1,1;1,1,1;1,1,1]
B=A.^2
C=A^2
%單位陣eye(n,m)
a=eye(4,3)
%元素全1矩陣
a=ones(4,3)
%零矩陣 zeros(n,m)
a=zeros(4,3)
%魔方矩陣 magic(n) 隨機(jī)陣rand(n,m)
magic(4)
%伴隨矩陣 compan(A) 注意A為向量,不能是矩陣
u=[1 0 -7 6];
A=compan(u)
%%(x+y)n楊輝三角形表組成的矩陣稱(chēng)為帕斯卡(Pascal)矩陣求(x+y)5系數(shù),對(duì)角線(xiàn)值
pascal(6)
% 函數(shù)rot90(A,k)將矩陣A旋轉(zhuǎn)90?的k倍,當(dāng)k為1時(shí)可省略
% 求方陣A的逆矩陣可調(diào)用函數(shù)inv(A)
% 特征值和特征向量的函數(shù)是eig(A)
% 有AX=B 則X=?
A=[21,34,20;5 78 20;21 14 17; 34 31 38];
B=[10 20 30 40]';
X=B\A
% diag(A)函數(shù)用于提取矩陣A主對(duì)角線(xiàn)元素,產(chǎn)生一個(gè)具有min(m,n)個(gè)元素的列向量。
%%%%%%%%%%%%%% % 多項(xiàng)式運(yùn)算 %%%%%%%%%%%%%%%%%%%%%%%%%%
T=[2 5 0 4 1 4]
poly2sym(T)
%f=2x^5+5x^4+4x^2+x+4
T=[2 5 0 4 1 4]
r=roots(T)
poly(r)
x=-2.7709
f=2x^5+5x^4+4x^2+x+4
%%
%多項(xiàng)式乘積 C=Conv(a,b) eg. (x+1)(x2+x+3)
a=[0,1,1]
b=[1,1,3]
mul=conv(a,b)
% 多項(xiàng)式除 (s4+7s3+16s2+18s+8)/(s+3)
a=[1,3]
b=[1,7,16,18,8]
[q,r]=deconv(b,a)
%求多項(xiàng)式值:polyval與polyvalm,它們的輸入?yún)?shù)均為多項(xiàng)式系數(shù)向量P和自變量x。
% 兩者的區(qū)別在于前者是代數(shù)多項(xiàng)式求值,而后者是矩陣多項(xiàng)式求值。
%多項(xiàng)式+ 乘conv(T1,T2);除 [T r]=deconv(t1,t2)
T1=[2 5 0 4 1 4];
T2=[0 0 5 1 3 2];
T=conv(T1,T2);
% 求導(dǎo) polyder
T1=[2 5 0 4 1 4];
h=polyder(T1);
%多項(xiàng)式求值 求x=[3 4]處的值 h=polyval(T1,[3 4])
T1=[2 5 0 4 1 4];
s=[1,4,5]
h=polyval(T1,s)
%求根 h=roots(T1)
h=roots(T1)
% 求多項(xiàng)式x4+8x3-10的根。命令如下:
A=[1,8,0,0,-10];
X=roots(A)
G=poly(X)
% 若已知多項(xiàng)式的全部根,則可以用poly函數(shù)建立起該多項(xiàng)式,其調(diào)用格式為:P=poly(x)
% 若x為具有n個(gè)元素的向量,則poly(x)建立以x為其根的多項(xiàng)式,且將該多項(xiàng)式的系數(shù)賦給向量P。
%多項(xiàng)式的擬合用 [p,s]=polyfit(x,y,n) 最小2乘法
x=linspace(0,2*pi,100);
y=cos(x)
T=polyfit(x,y,6)
y1=polyval(T,x)
plot(x,y,'go',x,y1,'b--')
%%% 關(guān)系與邏輯操作符 測(cè)試函數(shù) %%%%%%%%
% Eq(A,B) Lt(A,B) Gt(A,B) Le(A,B) Ge(A,B)
% And(A,B) Or(A,B) Not(A) Xor(A,B) Any(A) All(A)
A=[1 0 2 1]
B=[0 0 1 1]
C=And(A,B)
%%%%% 通用數(shù)據(jù)分析函數(shù) %%%%%%%
% Max Min Mean Median Std Sum Prod求積 hist trapz Sort Sumsum CumProd
a=[1 2 5; 4 5 3]
[y,I]=max(a)
[y,I]=max(a,[],2)
y=[0 1 2]
trapz(y)
% diff 差分 Gradient(F) 梯度
% 對(duì)多項(xiàng)式求導(dǎo)數(shù)的函數(shù)是:
p=polyder(P) %:求多項(xiàng)式P的導(dǎo)函數(shù)
p=polyder(P,Q) %:求P·Q的導(dǎo)函數(shù)
[p,q]=polyder(P,Q) %:求P/Q的導(dǎo)函數(shù),導(dǎo)函數(shù)的分子存入p,分母存入q。
%%%%%%%%%%%%%%%%%%%%%% 符號(hào)運(yùn)算 %%%%%%%
% (1)利用單引號(hào)來(lái)生成符號(hào)表達(dá)式。
% (2)用sym函數(shù)建立符號(hào)表達(dá)式。
% (3) 使用已經(jīng)定義的符號(hào)變量組成符號(hào)表達(dá)式
% syms 符號(hào)變量名1 符號(hào)變量名2 …
f='a*x^2+bx+c=0'
f=sym('ax+b=0') % sym生成符號(hào)表達(dá)式
symvar('a*x+y')
diff('x^n') %求導(dǎo)
diff('x^n','n') %對(duì)n求導(dǎo)
% 符號(hào)基本代數(shù)運(yùn)算 symadd,symsub,symmul,symdiv,sympow
f='4*x+5';
g='2*x^2+5*x+6';
symadd(f,g)
eval(t) %其中t為字符串。它的作用是把字符串的內(nèi)容作為對(duì)應(yīng)的MATLAB語(yǔ)句來(lái)執(zhí)行。