260 likes | 550 Vues
MATLAB 簡介. 授課教授 林沛群老師 課程助教 李宇修 / 林明賢 工綜 425 室. Outline. 1. MATLAB 簡介 2. Matlab Basics (Excise 1, 2) 3. Polynomials (Excise 3) 4. Transfer Function Analysis (Excise4). 附錄:台灣大學軟體銀行. MATLAB 簡介. 由 MathWorks 公司於 1984 年推出的數學軟體。名稱是由「矩陣實驗室」而來。 ( MAT rix LAB oratory)
E N D
MATLAB簡介 授課教授 林沛群老師 課程助教 李宇修/林明賢 工綜425室
Outline 1. MATLAB簡介 2. Matlab Basics (Excise 1, 2) 3. Polynomials (Excise 3) 4. Transfer Function Analysis (Excise4)
MATLAB簡介 • 由MathWorks公司於1984年推出的數學軟體。名稱是由「矩陣實驗室」而來。 (MATrix LABoratory) • MATLAB為各種動態系統模擬、數位訊號處理、科學計算、科學目視等領域的標準程式語言。
MATLAB簡介 • MATLAB 早期以矩陣運算為主,後推出握把式圖形(Handle Graphics),並且允許使用者建立許多不同的資料型態,如多維陣列、結構陣列、異質陣列等。 • MATLAB 是一個計算核心,圍繞著這個計算核心,有許多針對不同應用所開發的應用程式,稱為(Toolboxes)。
MATLAB簡介 • 點選Desktop/Desktop Layout/可將改變視窗型式。 workspace current directory Command history Command window
Matlab Basics • 一般數學符號運算 • 在MATLAB 命令視窗(Command Window)內的提示符號(>>)之後輸入運算式,並按入 Enter 鍵即可。例如: >> (5*2+3.5)/5 ans = 2.7000 • 若不想讓 MATLAB 每次都顯示運算結果,只需在運算式最後加上分號(;)即可,例如: >> (5*2+3.5)/5;
Matlab Basics >>% Statements and variables >> A = [1 2; 4 6]; >> a = 2+3*i; >> a = 2+3*i a = 2.0000 + 3.0000i >> A A = 1 2 4 6 >> pi ans = 3.1416
Matlab Basics >>%Matrices1 >> A = [1 2 3; 4 5 6]; >> B = [2 3 4; 7 8 9]; >> C = A + B C = 3 5 7 11 13 15 >> b = [1;2;3]; >> A*b ans = 14 32 >> A' ans = 1 4 2 5 3 6
Matlab Basics >> a = [3;4;5]; >> b = [7;8;9]; >> a'*b ans = 98 >> a*b' ans = 21 24 27 28 32 36 35 40 45 >>cross(a,b) ans = -4 8 -4
Matlab Basics Element-by-element operations >> M = [1 2; 3 4] M = 1 2 3 4 >> M^2 ans = 7 10 15 22 >> M.^2 ans = 1 4 9 16
Matlab Basics (m.file) Commands can run by press the button after save as a *.m file Input commands here
Command window Save as M-file Matlab Basics (m.file) New M-file % M-file, tutex2.m % 計算一個球的體積 r = input('Type radius:'); area=pi*r^2; volume=(4/3)*pi*r^3; fprintf('The radius is %12.5f\n',r) fprintf('The area of a circle is %12.5f\n',area) fprintf('The volume of a sphere is %12.5f\n',volume) >> test Type radius: Type radius:2 The radius is 2.00 The area of a circle is 12.56 The volume of a sphere is 33.51
Matlab Basics (plot) %plotTest x = [0:0.1:1]'; y1 = x.*sin(x); y2 = sin(x); plot(x,y1,'--',x,y2,'-.'); title('Plot of xsin(x) vs x'); text(0.1,0.85,' y_1 = xsin(x) ---'); text(0.1,0.75,' y_2 = sin(x) .\_.\_'); xlabel('x'); ylabel('y_1 and y_2'); grid on;
Polynomials Transfer function is a ratio of polynomials. In Matlab, polynomials are represented by row vectors >> p = [1 3 0 4]; >> r = roots(p) r = -3.3553 0.1777 + 1.0773i 0.1777 - 1.0773i >> p = poly(r) p = 1.0000 3.0000 0.0000 4.0000
Polynomials Using conv and polyval to multiply and evaluate polynomials >> p = [3 2 1]; >> q = [1 4]; >> n = conv(p, q) n = 3 14 9 4 >> value = polyval(n,-5) value = -66
Excise 3 • A polynomial have roots at (-1+3i, -1-3i, 0, 0, -2.3,2.5), plot it in the domain x=[-3, 3] >> w=[-1+3i,-1-3i,0,0,-2.3,2.5]; >> ww = poly(w) ww = 1.0000 1.8000 3.8500 -13.5000 -57.5000 0 0 >> x=[-3:0.1:3]; >> y=1*x.^6+1.8*x.^5+3.85*x.^4-13.5*x.^3-57.5*x.^2; >> plot(x,y,'--')
Transfer Function Analysis >> num1 = [10]; >> den1 = [1 2 5]; >> sys1 = tf(num1,den1) >> num2 = [1]; >> den2 = [1 1]; >> sys2 = tf(num2,den2) >> sys = sys1 + sys2 Transfer function: 10 G(s) = ------------- s^2 + 2 s + 5 Transfer function: 1 G(s) = ----- s + 1 Transfer function: s^2 + 12 s + 15 G(s) = --------------------- s^3 + 3 s^2 + 7 s+ 5
Transfer Function Analysis >> numg = [6 0 1]; deng = [1 3 3 1]; >> sysg = tf(numg,deng) Transfer function: 6 s^2 + 1 ---------------------------- s^3 + 3 s^2 + 3 s + 1 >> pole(sysg) ans = -1.0000 -1.0000 + 0.0000i -1.0000 - 0.0000i >> zero(sysg) ans = 0 + 0.4082i 0 - 0.4082i
Transfer Function Analysis Series Connection Parallel Connection + +
Transfer Function Analysis Unit Feedback + ± Feedback + ±
Excise 4 + -