
864e2b82c7f9982849dfc27f11b8f7ed.ppt
- Количество слайдов: 126
MATLAB 程式設計與 程應用 Chap. 3 二維平面繪圖 方煒 台大生機系 1
A complete plot w/ title, axis labels, legend, grid and multiple line styles MATLAB 程式設計與 程應用 2
MATLAB 程式設計與 程應用 5 -2 3
MATLAB 程式設計與 程應用 基本的繪圖指令 n n Plot : 最基本的繪圖指令 對 x 座標及相對應的 y 座標進行作圖 n 範例: plotxy 01. m x = linspace(0, 2*pi); % 在 0 到 2π 間,等分取 100 個點 y = sin(x); % 計算 x 的正弦函數值 plot(x, y); % 進行二維平面描點作圖 4
MATLAB 程式設計與 程應用 Plot基本繪圖 -1 n n n linspace(0, 2*pi) 產生 從 0 到 2π且長度為 100 (預設值 )的向量 x y 是對應的 y 座標 只給定一個向量 n n 該向量則對其索引值 (Index)作圖 plot(y)和 plot(1: length(y), y)會得到相同的結果 5
MATLAB 程式設計與 程應用 Plot基本繪圖 -2 (I) n 一次畫出多條曲線 n n 將 x 及 y 座標依次送入 plot 指令 範例: plotxy 02. m x = linspace(0, 2*pi); 點 % 在 0 到 2 間,等分取 100 個 plot(x, sin(x), x, cos(x), x, sin(x)+cos(x)); % 進行多條曲線描點作 圖 6
MATLAB 程式設計與 程應用 Plot基本繪圖 -2 (II) Plot(x, sin(x), x, cos(x), x, sin(x)+cos(x)); § 畫出多條曲 線時,會自動 輪換曲線顏 色 7
MATLAB 程式設計與 程應用 Plot基本繪圖 -3 (I) n 若要以不同的線標 (Marker)來作圖 n 範例: plotxy 03. m x = linspace(0, 2*pi); 點 % 在 0 到 2 間,等分取 100 個 plot(x, sin(x), 'o', x, cos(x), 'x', x, sin(x)+cos(x), '*'); 8
MATLAB 程式設計與 程應用 Plot基本繪圖 -3 (II) 9
MATLAB 程式設計與 程應用 Plot基本繪圖 -4 (I) n 只給定一個矩陣 n n y 對矩陣 y 的每一個行向量 (Column Vector) 作圖 範例: plot 04. m y = peaks; % 產生一個 49× 49 的矩陣 plot(y); % 對矩陣 y 的每一個行向量作圖 10
MATLAB 程式設計與 程應用 Plot基本繪圖 -4 (II) n n n peaks 指令產生一個 49× 49的矩陣,代表二維函數的值 plot(y) 直接畫出 49 條直線 類似於從側面觀看 peaks 函數 11
MATLAB 程式設計與 程應用 Plot基本繪圖 -5 (I) n n x 和 y 都是矩陣 plot(x, y) 會取用 y 的每一個行向量和 對應的 x 行向量作圖 n 範例: plotxy 05. m x = peaks; y = x'; % 求矩陣 x 的轉置矩陣 x' plot(x, y); % 取用矩陣 y 的每一行向量,與對應矩陣 % 的每一個行向量作圖 x 12
MATLAB 程式設計與 程應用 Plot基本繪圖 -5 (II) 13
MATLAB 程式設計與 程應用 提示 n n 一般情況下, MATLAB 將矩陣視為行 向量的集合 對只能處理向量的函數 (Ex : max、 min、 mean) n 給定一個矩陣,函數會對矩陣的行向量 一一進行處理或運算 14
MATLAB 程式設計與 程應用 Plot基本繪圖 -6 (I) n z 是一個複數向量或矩陣 n n n plot(z) 將 z 的實部 (即 real(z))和虛部 (即 imag(z))當成 x 座標和 y 座標來作圖, 其效果等於 plot(real(z), imag(z)) 範例: plotxy 06. m x = randn(30); % 產生 30× 30 的亂數 (正規分佈 )矩陣 z = eig(x); % 計算 x 的「固有值」 (或稱「特徵值」 ) plot(z, 'o') grid on % 畫出格線 15
MATLAB 程式設計與 程應用 Plot基本繪圖 -6 (II) n n n x 是一個 30× 30 的亂數矩陣 z 則是 x 的「固有值」 (Eigenvalue, 或「特徵值」 ) z 是複數向量,且每一個複數都和其共軛複數同時出現, 因此畫出的圖是上下對稱 16
MATLAB 程式設計與 程應用 Z=0. 1+0. 9 i; N=[0: 0. 1: 10]; Plot(z. ^n), xlabel(‘Real’), ylabel(‘Imaginary’); 17
MATLAB 程式設計與 程應用 基本二維繪圖指令 指令 說明 Plot x 軸和 y 軸均為線性刻度 (Linear Scale) loglog semilogx x 軸和 y 軸均為對數刻度 (Logarithmic Scale) x 軸為對數刻度, y 軸為線性刻度 semilogy x 軸為線性刻度, y 軸為對數刻度 plotyy 畫出兩個刻度不同的 y軸 18
MATLAB 程式設計與 程應用 Plot基本繪圖 -7 (I) n Semilogx指令 n n 使 x 軸為對數刻度,對正弦函數作圖 範例: plotxy 07. m x = linspace(0, 8*pi); semilogx(x, sin(x)); 圖 % 在 0 到 8 間,等分取 100 個點 % 使 x 軸為對數刻度,並對其正弦函數作 19
MATLAB 程式設計與 程應用 Plot基本繪圖 -7 (II) X軸為對數刻度 20
MATLAB 程式設計與 程應用 21
MATLAB 程式設計與 程應用 Ex 4_5. m n n n n n x=0: . 1: 8; y=exp(x); semilogx(x, y); title('Semilogx for y=exp(x)') pause semilogy(x, y); title('Semilogy for y=exp(x)') pause loglog(x, y); title('Loglog for y=exp(x)') 22
MATLAB 程式設計與 程應用 Plot基本繪圖 -8 (I) n plotyy 指令 n n 畫出兩個刻度不同的 範例: plotxy 08. m x = linspace(0, 2*pi); y軸 % 在 0 到 2 間,等分取 100 個點 y 1 = sin(x); y 2 = exp(-x); plotyy(x, y 1, x, y 2); % 畫出兩個刻度不同的 y 2 y 軸,分別是 y 1, 23
MATLAB 程式設計與 程應用 Plot基本繪圖 -8 (II) Y 2的刻度 Y 1的刻度 n n n y 1 的刻度是在左手邊 y 2 的刻度是在右手邊 兩邊的刻度不同 24
MATLAB 程式設計與 程應用 圖形的控制 n plot 指令,可以接受一個控制字串輸 入 n n 用以控制曲線的顏色、格式及線標 使用語法 plot(x, y, ‘CLM‘) n n n C: 曲線的顏色 (Colors) L: 曲線的格式 (Line Styles) M: 曲線所用的線標 (Markers) 25
MATLAB 程式設計與 程應用 圖形控制範例 -1 (I) n n n 用黑色點線畫出正弦波 每一資料點畫上一個小菱形 範例: plotxy 09. m x = 0: 0. 5: 4*pi; % x 向量的起始與結束元素為 % 0. 5為各元素相差值 0 及 4, y = sin(x); plot(x, y, ‘k: diamond’) % 其中「 代表黑色,「:」代表點 k」 % 線,而「 diamond 」 則指定菱形為曲 % 線的線標 26
MATLAB 程式設計與 程應用 圖形控制範例 -1 (II) 27
MATLAB 程式設計與 程應用 plot 指令的曲線顏色 Plot指令的曲線顏色字 串 曲線顏色 RGB值 b 藍色 (Blue) (0, 0, 1) c 青藍色 (Cyan) (0, 1, 1) g 綠色 (Green) (0, 1, 0) k 黑色 (Black) (0, 0, 0) m 紫黑色 (Magenta) (1, 0, 1) r 紅色 (Red) (1, 0, 0) w 白色 (1, 1, 1) y 黃色 (Yellow) (1, 1, 0) 28
MATLAB 程式設計與 程應用 plot 指令的曲線格式字串 曲線格式 - 實線 (預設值 ) -- 虛線 : 點線 -. 點虛線 29
MATLAB 程式設計與 程應用 plot 指令的曲線線標 plot 指令的曲 線線標字串 曲線符號符號 > 朝右三角形 < 朝左三角形 square 方形 星號 diamond 菱形 . 點號 pentagram 五角星形 ^ 朝上三角形 hexagram 六角星形 V 朝下三角形 None 無符號 (預設值 ) 30 plot 指令的 曲線線標字 串 曲線符號符號 O 圓形 + 加號 X 叉號 *
MATLAB 程式設計與 程應用 31
MATLAB 程式設計與 程應用 Enhanced control of plotted lines n Plot(x, y, ’Property. Name’, value, . . ) n Property 的名稱 n n n Line. Width Marker. Edge. Color Marker. Face. Color Marker. Size … X=0: pi/15: 4*pi; y=exp(2*sin(x)); Plot(x, y, ’-ko’, ’Line. Width’, 3, ’Marker. Size’, 6, … ‘Marker. Edge. Color’, ’r’, ‘Marker. Face. Color’, ’g’) 32
MATLAB 程式設計與 程應用 圖軸的控制 n n plot 指令會根據座標點自動決定圖軸範 圍 也可以使用 axis 指令指定圖軸範圍 n 使用語法: axis([xmin, xmax, ymin, ymax]) n n xmin , xmax: 指定 x 軸的最小和最大值 ymin , ymax: 指定 y 軸的最小和最大值 33
MATLAB 程式設計與 程應用 圖軸控制範例 -1 (I) n 畫出正弦波在 y 軸介於 0 和 1 的部份 n 範例: plotxy 10. m x = 0: 0. 1: 4*pi; % 起始與結束元素為 % 元素相差值 0 及 4, 為各 0. 1 y = sin(x); plot(x, y); axis([-inf, 0, 1]); 份 % 畫出正弦波 y 軸介於 0 和 1 的部 34
MATLAB 程式設計與 程應用 圖軸控制範例 -1 (II) inf指令: n 以資料點 (上例: x 軸的資料點 )的最小和最大值取代 之 35
MATLAB 程式設計與 程應用 Ex 4_7. m close all; x=. 01: 20; y=cos(x). /x; plot(x, y); axis([0 25 -5 5]); %xlim([0, 20]); %ylim([-5, 5]); 36
MATLAB 程式設計與 程應用 圖軸控制範例 -2 (I) n 指定圖軸上的格線點 (Ticks) n 範例: plotxy 11. m x = 0: 0. 1: 4*pi; plot(x, sin(x)+sin(3*x)) set(gca, ‘ytick’, [-1 -0. 3 0. 1 1]); % 在 y 軸加上格線點 grid on % 加上格線 37
MATLAB 程式設計與 程應用 圖軸控制範例 -2 (II) 使 用 者 和加 文入 字的 格 線 點 n n grid on: 加上格線 gca: n get current axis的簡稱 n 傳回目前使用中的圖軸 n gca屬 Handle Graphics的指令 38
MATLAB 程式設計與 程應用 圖軸控制範例 -3 (I) n 將格線點 ytick的數字改為文字 n 範例: plotxy 12. m x = 0: 0. 1: 4*pi; plot(x, sin(x)+sin(3*x)) set(gca, 'ytick', [-1 -0. 3 0. 1 1]); % 改變格線點 set(gca, ‘yticklabel’, {‘極小 ’, ‘臨界值 ’, ‘崩潰值 ’, ‘極大 ’}); 字 grid on % 改變格線點的文 % 加上格線 39
MATLAB 程式設計與 程應用 圖軸控制範例 -3 (II) 40
MATLAB 程式設計與 程應用 圖軸控制範例 -4 (I) n 將格線點 xtick的數字改為文字 n 範例: x = [1: 6]; y=[13, 5, 7, 14, 10, 12]; plot(x, y, ‘o’, x, y) set(gca, ‘xtick', [1: 6], axis([1 6 0 15]), xlabe; (‘Month’); set(gca, ‘xticklabel’, [‘Jan’; ’’Feb’; ’Mar’; ’Apr’; ’May’; ’Jun’]); Ylabel(‘Monthly Sales ($1000)’); Title(‘Printer sales for January to June, 1997’); 41
MATLAB 程式設計與 程應用 圖軸控制範例 -4 (II) 42
MATLAB 程式設計與 程應用 一次畫出多條曲線在同一圖上 Ex 4_3. m n n n dx=. 01; x=. 5*dx: 10 -0. 5*dx; y=sin(5*x); y 2=cos(x); % the second function % plot both plot(x, y, 'r-', x, y 2, 'b-'); 43
MATLAB 程式設計與 程應用 一次畫出多條曲線在同一圖 上 x=-pi: pi/20: pi; y 1=sin(x); y 2=cos(x); plot(x, y 1, ’b-’, x, y 2, ’k—’); legend (‘sin(x)’, ’cos(x)’); 44
MATLAB 程式設計與 程應用 分批 畫出多條曲線在同一圖 上 x=-pi: pi/20: pi; y 1=sin(x); y 2=cos(x); plot(x, y 1, ’b-’); hold on; plot(x, y 2, ’k—’); hold off legend (‘sin(x)’, ’cos(x)’); 45
MATLAB 程式設計與 程應用 legend 指令 x = [0: 0. 01: 2]; y = sinh(x); z = tanh(x); plot(x, y, x, z, ’--’), xlabel(’x’), . . . ylabel(’Hyperbolic Sine and Tangent’), . . . legend(’sinh(x)’, ’tanh(x)’) 46
MATLAB 程式設計與 程應用 Possible locations for a plot legend 47
MATLAB 程式設計與 程應用 48
MATLAB 程式設計與 程應用 分批 畫出多條曲線在不同視 窗 figure(1) x=-pi: pi/20: pi; y 1=sin(x); y 2=cos(x); plot(x, y 1, ’b-’); legend (‘sin(x)’); figure(2) plot(x, y 2, ’k—’); legend (’cos(x)’); 49
MATLAB 程式設計與 程應用 Subplot n subplot n n n 在一個視窗產生多個圖形 (圖軸 ) 一般形式為 subplot (m, n, p) 將視窗分為 m ×n 個區域 下一個 plot 指令繪圖於第 p 個區域 p 的算法為由左至右,一列一列 50
MATLAB 程式設計與 程應用 圖軸控制範例 -4 (I) n 同時畫出四個圖於一個視窗中 n 範例: plotxy 13. m x = 0: 0. 1: 4*pi; subplot(2, 2, 1); plot(x, sin(x)); % 此為左上角圖形 subplot(2, 2, 2); plot(x, cos(x)); % 此為右上角圖形 subplot(2, 2, 3); plot(x, sin(x). *exp(-x/5)); % 此為左下角圖形 subplot(2, 2, 4); plot(x, x. ^2); % 此為右下角圖形 51
MATLAB 程式設計與 程應用 圖軸控制範例 -4 (II) Subplot(2, 2, 1 ) Subplot(2, 2, 3 ) Subplot(2, 2, 2 ) Subplot(2, 2, 4 ) 52
MATLAB 程式設計與 程應用 Subplot(2, 1, 1 ) Subplot(2, 1, 2 ) 53
MATLAB 程式設計與 程應用 54
MATLAB 程式設計與 程應用 圖軸控制範例 -5 (I) n 長寬比 (Aspect Ratio) n n n 一般圖軸長寬比是視窗的長寬比 可在 axis 指令後加不同的字串來修改 範例: plotxy 14. m t = 0: 0. 1: 2*pi; x = 3*cos(t); y = sin(t); subplot(2, 2, 1); plot(x, y); axis normal subplot(2, 2, 2); plot(x, y); axis square subplot(2, 2, 3); plot(x, y); axis equal subplot(2, 2, 4); plot(x, y); axis equal tight 55
MATLAB 程式設計與 程應用 圖軸控制範例 -5 (II) axis normal axis equal axis square axis equal tight 56
MATLAB 程式設計與 程應用 改變圖軸長寬比的指令 n n 改變目前圖軸長寬比的指令 需在 plot 指令之後呼叫才能發揮效用 指令 說明 axis normal 使用預設長寬比 (等於圖形長寬比 ) axis square 長寬比例為 1 axis equal 長寬比例不變,但兩軸刻度一致 axis equal tight 兩軸刻度比例一致,且圖軸貼緊圖形 axis image 兩軸刻度比例一致 (適用於影像顯示 ) 57
MATLAB 程式設計與 程應用 改變圖軸背景顏色的指令 n colordef n n 改變圖軸與視窗之背景顏色 先呼叫 colordef 指令,其後 plot 指令產生 的圖形才有效用 指令 說明 colordef white 圖軸背景為白色,視窗背景為淺灰色 colordef black 圖軸背景為黑色,視窗背景為暗灰色 colordef none 圖軸背景為黑色,視窗背景為黑色 MATLAB 第 4 版的預設值 ) (這是 58
MATLAB 程式設計與 程應用 grid 和 box 指令 n 畫出格線或畫出圖軸外圍的方形 指令 說明 grid on 畫出格線 grid off 取消格線 box on 畫出圖軸的外圍長方形 box off 取消圖軸的外圍長方形 59
MATLAB 程式設計與 程應用 加入說明文字 n 在圖形或圖軸加入說明文字,增進整 體圖形的可讀性 指令 說明 title 圖形的標題 xlabel x 軸的說明 ylabel y 軸的說明 zlabel z 軸的說明 (適用於立體繪圖 ) legend 多條曲線的說明 text 在圖形中加入文字 gtext 使用滑鼠決定文字的位置 60
MATLAB 程式設計與 程應用 說明文字範例 -1 (I) n 範例: plotxy 15. m subplot(1, 1, 1); x = 0: 0. 1: 2*pi; y 1 = sin(x); y 2 = exp(-x); plot(x, y 1, '--*', x, y 2, ': o'); xlabel('t = 0 to 2pi'); ylabel('values of sin(t) and e^{-x}') title('Function Plots of sin(t) and e^{-x}'); legend('sin(t)', 'e^{-x}'); 61
MATLAB 程式設計與 程應用 說明文字範例 -1 (II) n legend 指令 n n 畫出一小方塊,包 含每條曲線的說明 「 」為特殊符號 n n 產生上標、下標、希 臘字母、數學符號 等 遵循一般 La. Tex 或 Te. X數學模式 62
MATLAB 程式設計與 程應用 說明文字範例 -2 (I) n text指令 n n 使用語法: text(x, y, string) x、 : y 文字的起始座標位置 string : 代表此文字 範例: plotxy 16. m x = 0: 0. 1: 2*pi; plot(x, sin(x), x, cos(x)); text(pi/4, sin(pi/4), 'leftarrow sin(pi/4) = 0. 707'); text(5*pi/4, cos(5*pi/4), 'cos(5pi/4) = -0. 707rightarrow', 'Horizontal. Alignment', 'right'); 63
MATLAB 程式設計與 程應用 說明文字範例 -2 (II) n 「 Horizontal. Alignment」 及 「 right」 指示 text 指令將 文字向右水平靠齊 64
MATLAB 程式設計與 程應用 Enhanced control of Text Strings bf Boldface 粗體字 it Italics 斜體字 rm Remove modifiers 取消字體修飾,恢復預設字體 fontname{fontname) 定義使用的字型名稱 fontsize{fontsize} 定義使用的字型大小 _{xxx} {}內的字為下標 ^{xxx} {}內的字為上標 65
MATLAB 程式設計與 程應用 練習 ‘bf{B}_{it. S}’ Bs ‘tauit_{m}’ m ‘bfitx_{1}^{2} + x_{2}^{2} rm(units: bfm^{2}rm) ’ 66
MATLAB 程式設計與 程應用 Enhanced control of Text Strings circ sim infty pm leq geq neq prop div int cong leftarrow rightarrow uparrow downarrow leftrightarrow 67
MATLAB 程式設計與 程應用 範例: ex 4_1. m close all; x=. 01: 20; y=cos(x). /x; plot(x, y); axis([0 25 -5 5]); str 1='alpha beta gamma delta epsilon phi '; str 2='theta kappa lambda mu nu pi '; str 3='rho sigma tau xi zeta'; xlabel([str 1 str 2 str 3]); text(10, 1, 'theta_1'); text(10, 2, 'theta_{12}'); text(10, 3, 'theta^{10}'); 68
MATLAB 程式設計與 程應用 Ex 4_2. m dx=. 01; x=. 5*dx: 10 -0. 5*dx; y=sin(5*x); %plot(x, y, 'r-'); nhalf=ceil(length(x)/2); plot(x(1: nhalf), y(1: nhalf), 'b-') hold on plot(x(nhalf: end), y(nhalf: end), 'k-') xlabel('theta') ylabel('F(theta)') %title('F(theta)=sin(5 theta)') s=sprintf('F(\theta)=sin(%i \theta)', 5); title(s) 69
MATLAB 程式設計與 程應用 gtext指令 n 使用語法 gtext(string) n n 在圖上點選一位置後, string 顯示在其上。 gtext 只能用在二維平面繪圖 70
MATLAB 程式設計與 程應用 gtext 71
MATLAB 程式設計與 程應用 Enhanced control of Text Strings alpha beta lambda mu gamma delta epsilon theta tau omega nu pi phi rho sigma Omega Sigma Pi Lambda Delta Gamma 72
MATLAB 程式設計與 程應用 練習 :ideal gas law PV=n. RT n n n n n=1; R=8. 314; T=273; P=1: 0. 1: 1000; V=(n*R*T). /P; figure(1); loglog(……); … grid on; hold on; T=373; V=(n*R*T). /P; figure(1); loglog(…); hold off; legend(‘T=273 K’, ’T=373 K’); 請完成本程式,結果如下圖所示 73
MATLAB 程式設計與 程應用 The power function y = 2 x -0. 5 and the exponential function y = 101 -x. 74
MATLAB 程式設計與 程應用 Exponential function : polyfit(x, y, n) 75
MATLAB 程式設計與 程應用 The exponential function: y = b(10)mx. In this case log 10 y = mx + log 10 b which has the form w = p 1 z + p 2 where the polynomial variables w and z are related to the original data variables x and y by w = log 10 y and z = x. We can find the exponential function that fits the data by typing p = polyfit(x, log 10(y), 1) The first element p 1 of the vector p will be m, and the second element p 2 will be log 10 b. We can find b from b = 10 p 2. 76
MATLAB 程式設計與 程應用 時間,秒 溫度,o. F 0 145 620 130 2266 103 3482 90 詳見 coffee. m 範例:咖啡冷卻問題 n 室溫 68 o. F下瓷杯內的咖啡由 145 o. F逐漸冷卻,溫度對 應於經過時間的數據如上,請建立溫度對時間的函數, 並與此模式預測,經過多少時間,杯內溫度會達 120 o. F? time=[0, 620, 2266, 3482]; temp=[145, 130, 103, 90]; temp=temp-68; subplot(2, 2, 1); plot(time, temp, ’o’), xlabel(‘Time (sec)’), … ylabel(‘Relative Temperature (deg F)’) subplot(2, 2, 2); semilogy(time, temp, ’o’), xlabel(‘Time (sec)’), … ylabel(‘Relative Temperature (deg F)’) 77
MATLAB 程式設計與 程應用 範例:咖啡冷卻問題 (續 ) P=polyfit(time, log 10(temp), 1); m=p(1); b=10^p(2); t_120=(log 10(120 -68)-log 10(b))/m %show thederived curve and estimated point t=[0: 10: 4000]; T=68+b*10. ^(m*t); subplot(2, 2, 3); semilogy(t, T-68, time, temp, 'o', t_120, 120 -68, '+'), xlabel('Time (sec)'), . . . ylabel('Relative Temperature (deg F)'); %show in rectilinear scales subplot(2, 2, 4); plot(t, T, time, temp+68, 'o', t_120, '+'), xlabel('Time (sec)'), . . . ylabel('Temperature (deg F)'); ); 78
MATLAB 程式設計與 程應用 79
MATLAB 程式設計與 程應用 Power function: polyfit(x, y, n) 80
MATLAB 程式設計與 程應用 The power function: y = bxm. In this case log 10 y = m log 10 x + log 10 b which has the form w = p 1 z + p 2 where the polynomial variables w and z are related to the original data variables x and y by w = log 10 y and z = log 10 x. Thus we can find the power function that fits the data by typing p = polyfit(log 10(x), log 10(y), 1) The first element p 1 of the vector p will be m, and the second element p 2 will be log 10 b. We can find b from b = 10 p 2. 81
MATLAB 程式設計與 程應用 互動式繪圖 Interactive Plotting 82
MATLAB 程式設計與 程應用 Tools on the figure toolbar 83
MATLAB 程式設計與 程應用 Edit tool 84
MATLAB 程式設計與 程應用 85
MATLAB 程式設計與 程應用 86
MATLAB 程式設計與 程應用 87
MATLAB 程式設計與 程應用 88
MATLAB 程式設計與 程應用 產生 M 檔案 n n 點選 File 選單 選取 Generate M-File 選項 89
MATLAB 程式設計與 程應用 Figures 的存檔 Figure 的 預設存入檔案名稱為 . fig. 選取 Figure 視窗中 File menu 內的 Save 選項 或者直接 點選 具列內的 Save 按鈕 (磁碟片圖示 ). 該檔案為首次存入時會先出現 Save As 對話框確認存 入的是 MATLAB Figure (*. fig). 確認檔名後可以點選 OK. 90
MATLAB 程式設計與 程應用 Figures 的輸出 To save the figure in a format that can be used by another application, such as the standard graphics file formats TIFF or EPS, perform these steps. 1. Select Export Setup from the File menu. This dialog lets you specify options for the output file, such as the figure size, fonts, line size and style, and output format. 2. Select Export from the Export Setup dialog. A standard Save As dialog appears. 3. Select the format from the list of formats in the Save As type menu. This selects the format of the exported file and adds the standard file name extension given to files of that type. 4. Enter the name you want to give the file, less the extension. Then click Save. 91
MATLAB 程式設計與 程應用 Figures 的剪貼 On Windows systems, you can also copy a figure to the clipboard and then paste it into another application: 1. Select Copy Options from the Edit menu. The Copying Options page of the Preferences dialog box appears. 2. Complete the fields on the Copying Options page and click OK. 3. Select Copy Figure from the Edit menu. 92
MATLAB 程式設計與 程應用 其他平面繪圖指令 n 各種二維繪圖指令 指令 說明 errorbar 在曲線加上誤差範圍 fplot、 ezplot 較精確的函數圖形 polar、 ezpolar 極座標圖形 hist 直角座標直方圖 (累計圖 ) rose 極座標直方圖 (累計圖 ) compass 羅盤圖 feather 羽毛圖 area 面積圖 (第五章「特殊圖形」介紹) stairs 階梯圖 (第五章「特殊圖形」介紹) 93
MATLAB 程式設計與 程應用 其他平面繪圖範例 -1 (I) n 已知資料的誤差範圍,用 示 n n errorbar 表 以 y 座標高度 20% 作為做資料的誤差範 圍 x = linspace(0, 2*pi, 30); % 在 0 到 2 間,等分取 30 個點 y = sin(x); 範例: plotxy 17. m e = y*0. 2; errorbar(x, y, e) % 圖形上加上誤差範圍 e 94
MATLAB 程式設計與 程應用 其他平面繪圖範例 -1 (II) 誤差範 圍 95
MATLAB 程式設計與 程應用 其他平面繪圖範例 -2 (I) n fplot 指令 n n 對劇烈變化處進行較密集的取樣 範例: plotxy 18. m fplot('sin(1/x)', [0. 02 0. 2]); % [0. 02 0. 2]是繪圖範圍 96
MATLAB 程式設計與 程應用 其他平面繪圖範例 -2 (II) 此區作較精確的取點繪圖 97
MATLAB 程式設計與 程應用 x=([1: 0. 01: 2]; plot(x, cos(tan(x)-tan(sin(x))); 98
MATLAB 程式設計與 程應用 fplot(‘cos(tan(x)-tan(sin(x))', [1 2]); % [1 2]是繪圖範圍 99
MATLAB 程式設計與 程應用 Plotting Polynomials with the polyval Function. To plot the polynomial 3 x 5 + 2 x 4 – 100 x 3 + 2 x 2 – 7 x + 90 over the range – 6 £ x £ 6 with a spacing of 0. 01, you type >>x = [-6: 0. 01: 6]; >>p = [3, 2, -100, 2, -7, 90]; >>plot(x, polyval(p, x)); >>xlabel(’x’); >>ylabel(’p’); 100
MATLAB 程式設計與 程應用 其他平面繪圖範例 -3 (I) n polar 指令 n n 產生極座標圖形 範例: plotxy 19. m theta = linspace(0, 2*pi); r = cos(4*theta); polar(theta, r); % 進行極座標繪圖 101
MATLAB 程式設計與 程應用 其他平面繪圖範例 -3 (II) 102
MATLAB 程式設計與 程應用 範例 n n 某環繞太陽運轉的星球之極座標方程式 r = p / (1 – ε cosθ) ε為離心率 = 0. 5 (橢圓 ) p 為該星球的軌道極圖 = 2 AU AU 為天文單位,代表地球與太陽的距離 theta=[0: pi/90: 2*pi]; r=2. /(1 -0. 5*cos(theta)); polar(theta, r), title(‘Orbital Eccentricity = 0. 5’); 103
MATLAB 程式設計與 程應用 原點為太陽的位置 半徑為 1的圓為地 球的軌跡 180 o時,該星 球與太陽的距 離為最短 ,僅 1. 3 AU 徑向直線顯示 90與 270 o時,該星球與太陽 的距離為 2 AU 0 o時,該星球 與太陽有最遠 的距離 , 4 AU 達 橢圓為該星球的軌跡, 太陽位於其焦點之一 104
MATLAB 程式設計與 程應用 直方圖及 hist指令 n 直方圖 (Histogram) n n 對大量的資料 , 顯示資料的分佈情況和 統計特性 hist指令 n 將資料依大小分成數堆,將每堆的個數 畫出 105
MATLAB 程式設計與 程應用 其他平面繪圖範例 -4 (I) n n 將 10000 個由 randn 產生的正規分佈之 亂數分成 25 堆 範例: plotxy 20. m x = randn(10000, 1); % 產生 10000 個正規分佈亂數 hist(x, 25); 情 % 繪出直方圖,顯示 x 資料的分佈 依 條 %況和統計特性,數字 25 代表資料 %大小分堆的堆數,即是指方圖內長 %的個數 set(findobj(gca, ‘type’, ‘patch’), ‘edgecolor’, ‘w’); % 將長條 106
MATLAB 程式設計與 程應用 其他平面繪圖範例 -4 (II) n 直方圖逼近亂數的機率分佈函數,且資 料量越大時,逼近程度越高 107
MATLAB 程式設計與 程應用 其他平面繪圖範例 -5 (I) n rose指令 n n 角度:資料大小 距離:資料個數 以極座標繪製表示 範例: plotxy 21. m x = randn(5000, 1); % 產生 5000 個正規分佈的亂數 rose(x); % x 資料大小為角度, x 資料個數為距離, %以繪製類似玫瑰花瓣的極座標直方圖 108
MATLAB 程式設計與 程應用 其他平面繪圖範例 -5 (II) 109
MATLAB 程式設計與 程應用 其他平面繪圖範例 -6 (I) n compass 指令 n n n 畫出以原點為起始點的向量圖 稱為「羅盤圖」 範例: plotxy 22. m theta = linspace(0, 2*pi, 50); rho = sin(0. 5*theta); [x, y] = pol 2 cart(theta, rho); 標 % 由極座標轉換至直角座 compass(x, y); 點 % 畫出以原點為向量起始 % 的羅盤圖 110
MATLAB 程式設計與 程應用 其他平面繪圖範例 -6 (II) n 若只有一個引數輸入 z n n n 範例: plotxy 23. m theta = linspace(0, 2*pi, 50); n 將 z 的實部做為 x 座 標,將 z 的虛部做為 y 座標,再進行作圖 compass(z) 即等效於 compass(real(z), imag(z) ) 上述四列程式碼可簡 化 compass(sin(0. 5*theta). *exp(j*theta)); 111
MATLAB 程式設計與 程應用 其他平面繪圖範例 -7 (I) n 羽毛圖 n n 起始點是 (k, 0), = 1~n , k 其中 n 是向量 個數 範例: plotxy 24. m theta = linspace(0. 2*pi, 50); rho = 10; [x, y] = pol 2 cart(theta, rho); % 由極座標轉換至直角座標 feather(x, y); % 繪製羽毛圖 112
MATLAB 程式設計與 程應用 其他平面繪圖範例 -7 (II) 113
MATLAB 程式設計與 程應用 練習 試寫一函數 reg. Poly(n),其功能為畫出一個圓心在 (0, 0)、 半徑為 1 的圓,並在圓內畫出一個內接正 n 邊形,其中一 頂點位於 (0, 1)。例如 reg. Poly(8) 可以畫出如下之正八邊型: function regpoly(n) vertices=[1]; for i=1: n step=2*pi/n; vertices=[vertices, exp(i*step*sqrt(-1))]; end plot(vertices, '-o'); axis image % 畫外接圓 hold on theta=linspace(0, 2*pi); plot(cos(theta), sin(theta), '-r'); hold off axis image 114
MATLAB 程式設計與 程應用 一條參數式的曲線可由下列方程式表示: x = sin(t), y = 1 - cos(t) + t/10 當 t 由 0 變化到 4*pi 時,請寫一個 MATLAB 的腳本 plot. Param. m, 畫出此曲線在 XY 平面的軌跡。 Function plot. Param t = linspace(0, 4*pi); x = sin(t); y = 1 -cos(t)+t/10; plot(x, y, '-o'); 115
MATLAB 程式設計與 程應用 試寫一函數 reg. Star(n),其功能為畫出一個圓心在 (0, 0)、半徑為 1 的圓,並在圓內畫出一個內接正 n 星形,其中一頂點位於 (0, 1)。 例如 reg. Star(7) 可以畫出如下之正 7 星型: function reg. Star(n) vertices=[1]; for i=1: n step=2*pi*floor(n/2)/n; vertices=[vertices, exp(i*step*sqrt(-1))]; end plot(vertices, '-o'); % 畫外接圓 hold on theta=linspace(0, 2*pi); plot(cos(theta), sin(theta), '-r'); hold off axis image 116
MATLAB 程式設計與 程應用 平面上的橢圓可表成: (x/a)2+(y/b)2=1, 橢圓參數式表示成: x = a*cos(t) 與 y = a*sin(t) 請利用上述參數式,寫一個函數 ellipse(a, b),其功能是畫出一個 橢圓,且橢圓上共有 100個點。如 ellipse(7, 2) 可以產生下列圖形: function ellipse(a, b) % 畫橢圓 theta=linspace(0, 2*pi, 101); plot(a*cos(theta), b*sin(theta)); axis image 117
MATLAB 程式設計與 程應用 請用寫一個 MATLAB 腳本 fig. Solve. m,利用圖解法,說明下列聯立方程 式有無窮多組解: y = x; y = sin(1/x) Function fig. Solve x=linspace(0, 1); plot(x, x, x, sin(1. /x)); 118
MATLAB 程式設計與 程應用 當一個小圓輪在平面上滾動時,輪緣的一點在滾動時所形成的 軌跡稱為「擺線」( Cycloid)。請用 MATLAB 畫出一個典型的擺線, 其中小圓輪的半徑為 1,而且至少要滾三圈。 clear all; r=1; t 1=linspace(0, 2*pi, 1000); t 2=linspace(0, 6*pi, 1000); x 1=r*(t 1 -sin(t 1)); y 1=r*(1 -cos(t 1)); x 2=r*(t 2 -sin(t 2)); y 2=r*(1 -cos(t 2)); subplot(2, 1, 1); plot(x 1, y 1); title('小圓輪滾一圈'); subplot(2, 1, 2); plot(x 2, y 2); title('小圓輪滾三圈'); 119
MATLAB 程式設計與 程應用 當一個小圓輪沿著一條曲線行進時,輪緣任一點的軌跡就會產生變化 豐富的擺線。假設小圓輪的半徑 r=2。當小圓輪繞著一個大圓(半徑 R=5) 的外部滾動時,請畫此「圓輪擺線」或「外花瓣線」。重複上小題,但改成 在大圓的內部滾動,請畫出此「內花瓣線」。 R=5; % 大圓半徑 r=2; % 小圓半徑 n=r/gcd(r, R); % 圈數 t=linspace(0, n*2*pi, 1000); c=R*exp(i*t); % 內花瓣線 c 1=(R-r)*exp(i*t)+r*exp(i*(-R*t/r+t)); % 外花瓣線 c 2=(R+r)*exp(i*t)+r*exp(i*(pi+R*t/r+t)); plot(real(c), imag(c), real(c 1), imag(c 1), real(c 2), imag(c 2)); axis image title('內花瓣線(綠色)和外花瓣線(紅色)'); 120
MATLAB 程式設計與 程應用 121
MATLAB 程式設計與 程應用 若大圓和小圓的半徑成整數比,當小圓在大圓的內部滾動時,小圓內的 任一點 A 的軌跡就會形成一個漂亮無缺的花瓣線。當大圓半徑 R 為 10, 小圓半徑 r 為 3,且 A 點離小圓圓心距離 r 1 為 2 時,請畫出此完整的花 瓣線。 r=3; % 小圓半徑 R=10; % 大圓半徑 d=2; % 筆心離小圓圓心距離 n=r/gcd(r, R); % 圈數 t=linspace(0, n*2*pi, 1000); c=R*exp(i*t); % 內花瓣線 c 1=(R-r)*exp(i*t)+d*exp(i*(-R*t/r+t)); % 外花瓣線 c 2=(R+r)*exp(i*t)+d*exp(i*(pi+R*t/r+t)); plot(real(c), imag(c), real(c 1), imag(c 1), real(c 2), imag(c 2)); axis image title('內花瓣線(綠色)和外花瓣線(紅色)'); 122
MATLAB 程式設計與 程應用 123
MATLAB 程式設計與 程應用 124
MATLAB 程式設計與 程應用 HW#2 Snell’s Law n 1 sin(θ 1)=n 2 sin(θ 2) a) 光線由空氣進入水中, 請繪圖 plot(x, y) ,其 中 x為入射角由 0~90 度,y為 R折射角 b) 光線由水進入空氣中, 其他同 (a) 125
MATLAB 程式設計與 程應用 HW#2 126
864e2b82c7f9982849dfc27f11b8f7ed.ppt