Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chap.3 二維平面繪圖 方煒 台大生機系.

Similar presentations


Presentation on theme: "Chap.3 二維平面繪圖 方煒 台大生機系."— Presentation transcript:

1 Chap.3 二維平面繪圖 方煒 台大生機系

2 A complete plot w/ title, axis labels, legend, grid and multiple line styles

3 5-2

4 基本的繪圖指令 Plot : 最基本的繪圖指令 對 x 座標及相對應的 y 座標進行作圖 範例:plotxy01.m
x = linspace(0, 2*pi); % 在 0 到 2π 間,等分取 100 個點 y = sin(x); % 計算 x 的正弦函數值 plot(x, y); % 進行二維平面描點作圖

5 Plot基本繪圖-1 linspace(0, 2*pi) 產生從 0 到 2π且長度為 100 (預設值)的向量 x y 是對應的 y 座標
只給定一個向量 該向量則對其索引值(Index)作圖 plot(y)和plot(1:length(y), y)會得到相同的結果

6 Plot基本繪圖-2 (I) 一次畫出多條曲線 將 x 及 y 座標依次送入plot 指令 範例:plotxy02.m
x = linspace(0, 2*pi); % 在 0 到 2 間,等分取 100 個點 plot(x, sin(x), x, cos(x), x, sin(x)+cos(x)); % 進行多條曲線描點作圖

7 Plot基本繪圖-2 (II) 畫出多條曲線時,會自動輪換曲線顏色
Plot(x,sin(x), x, cos(x), x, sin(x)+cos(x)); 畫出多條曲線時,會自動輪換曲線顏色

8 Plot基本繪圖-3 (I) 若要以不同的線標(Marker)來作圖 範例:plotxy03.m
x = linspace(0, 2*pi); % 在 0 到 2 間,等分取 100 個點 plot(x, sin(x), 'o', x, cos(x), 'x', x, sin(x)+cos(x), '*');

9 Plot基本繪圖-3 (II)

10 Plot基本繪圖-4 (I) 只給定一個矩陣 y 對矩陣 y 的每一個行向量(Column Vector)作圖 範例:plot04.m
y = peaks; % 產生一個 49×49 的矩陣 plot(y); % 對矩陣 y 的每一個行向量作圖

11 Plot基本繪圖-4 (II) peaks 指令產生一個49×49的矩陣,代表二維函數的值 plot(y) 直接畫出 49 條直線

12 Plot基本繪圖-5 (I) x 和 y 都是矩陣 plot(x, y) 會取用 y 的每一個行向量和對應的 x 行向量作圖
範例:plotxy05.m x = peaks; y = x'; % 求矩陣 x 的轉置矩陣 x' plot(x, y); % 取用矩陣 y 的每一行向量,與對應矩陣 x % 的每一個行向量作圖

13 Plot基本繪圖-5 (II)

14 提示 一般情況下,MATLAB 將矩陣視為行向量的集合 對只能處理向量的函數(Ex : max、min、 mean)
給定一個矩陣,函數會對矩陣的行向量一一進行處理或運算

15 Plot基本繪圖-6 (I) z 是一個複數向量或矩陣
plot(z) 將 z 的實部(即 real(z))和虛部(即 imag(z))當成 x 座標和 y 座標來作圖, 其效果等於 plot(real(z), imag(z)) 範例:plotxy06.m x = randn(30); % 產生 30×30 的亂數(正規分佈)矩陣 z = eig(x); % 計算 x 的「固有值」(或稱「特徵值」) plot(z, 'o') grid on % 畫出格線

16 Plot基本繪圖-6 (II) x 是一個 30×30 的亂數矩陣 z 則是 x 的「固有值」(Eigenvalue,或「特徵值」)

17 Z= i; N=[0:0.1:10]; Plot(z.^n), xlabel(‘Real’), ylabel(‘Imaginary’);

18 基本二維繪圖指令 指令 說明 Plot x 軸和 y 軸均為線性刻度(Linear Scale) loglog
x 軸和 y 軸均為對數刻度(Logarithmic Scale) semilogx x 軸為對數刻度,y 軸為線性刻度 semilogy x 軸為線性刻度,y 軸為對數刻度 plotyy 畫出兩個刻度不同的y軸

19 Plot基本繪圖-7 (I) Semilogx指令 使 x 軸為對數刻度,對正弦函數作圖 範例:plotxy07.m
x = linspace(0, 8*pi); % 在 0 到 8 間,等分取 100 個點 semilogx(x, sin(x)); % 使 x 軸為對數刻度,並對其正弦函數作圖

20 Plot基本繪圖-7 (II) X軸為對數刻度

21

22 Ex4_5.m 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)') loglog(x,y); title('Loglog for y=exp(x)')

23 Plot基本繪圖-8 (I) plotyy 指令 畫出兩個刻度不同的 y 軸 範例:plotxy08.m
x = linspace(0, 2*pi); % 在 0 到 2 間,等分取 100 個點 y1 = sin(x); y2 = exp(-x); plotyy(x, y1, x, y2); % 畫出兩個刻度不同的 y 軸,分別是 y1, y2

24 Plot基本繪圖-8 (II) y1 的刻度是在左手邊 y2 的刻度是在右手邊 兩邊的刻度不同 Y2的刻度 Y1的刻度

25 圖形的控制 plot 指令,可以接受一個控制字串輸入 用以控制曲線的顏色、格式及線標 使用語法 C:曲線的顏色(Colors)
plot(x, y, ‘CLM‘) C:曲線的顏色(Colors) L:曲線的格式(Line Styles) M:曲線所用的線標(Markers)

26 圖形控制範例-1 (I) 用黑色點線畫出正弦波 每一資料點畫上一個小菱形 範例:plotxy09.m
x = 0:0.5:4*pi; % x 向量的起始與結束元素為 0 及 4, % 0.5為各元素相差值 y = sin(x); plot(x, y,‘k:diamond’) % 其中「k」代表黑色,「:」代表點 % 線,而「diamond 」則指定菱形為曲 % 線的線標

27 圖形控制範例-1 (II)

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

29 plot 指令的曲線格式 plot 指令的曲線格式字串 曲線格式 - 實線(預設值) -- 虛線 點線 -. 點虛線

30 plot 指令的曲線線標 plot 指令的曲線線標字串 曲線符號符號 > 朝右三角形 < 朝左三角形 square 方形
diamond 菱形 pentagram 五角星形 hexagram 六角星形 None 無符號(預設值) plot 指令的曲線線標字串 曲線符號符號 O 圓形 + 加號 X 叉號 * 星號 . 點號 ^ 朝上三角形 V 朝下三角形

31

32 Enhanced control of plotted lines
Plot(x,y,’PropertyName’, value,..) Property 的名稱 LineWidth MarkerEdgeColor MarkerFaceColor MarkerSize X=0:pi/15:4*pi; y=exp(2*sin(x)); Plot(x,y,’-ko’,’LineWidth’,3,’MarkerSize’,6,… ‘MarkerEdgeColor’,’r’,‘MarkerFaceColor’,’g’)

33 圖軸的控制 plot 指令會根據座標點自動決定圖軸範圍 也可以使用 axis 指令指定圖軸範圍 使用語法:
axis([xmin, xmax, ymin, ymax]) xmin , xmax:指定 x 軸的最小和最大值 ymin , ymax:指定 y 軸的最小和最大值

34 圖軸控制範例-1 (I) 畫出正弦波在 y 軸介於 0 和 1 的部份 範例:plotxy10.m
x = 0:0.1:4*pi; % 起始與結束元素為 0 及 4,0.1 為各 % 元素相差值 y = sin(x); plot(x, y); axis([-inf, inf, 0, 1]); % 畫出正弦波 y 軸介於 0 和 1 的部份

35 圖軸控制範例-1 (II) inf指令: 以資料點(上例: x 軸的資料點)的最小和最大值取代之

36 Ex4_7.m close all; x=.01:.01:20; y=cos(x)./x; plot(x,y);
axis([ ]); %xlim([0, 20]); %ylim([-5, 5]);

37 圖軸控制範例-2 (I) 指定圖軸上的格線點(Ticks) 範例:plotxy11.m x = 0:0.1:4*pi;
plot(x, sin(x)+sin(3*x)) set(gca, ‘ytick’, [ ]); % 在 y 軸加上格線點 grid on % 加上格線

38 圖軸控制範例-2 (II) grid on:加上格線 gca: get current axis的簡稱 傳回目前使用中的圖軸
使用者加入的格線點和文字 grid on:加上格線 gca: get current axis的簡稱 傳回目前使用中的圖軸 gca屬Handle Graphics的指令

39 圖軸控制範例-3 (I) 將格線點ytick的數字改為文字 範例:plotxy12.m x = 0:0.1:4*pi;
plot(x, sin(x)+sin(3*x)) set(gca, 'ytick', [ ]); % 改變格線點 set(gca, ‘yticklabel’, {‘極小’,‘臨界值’,‘崩潰值’,‘極大’}); % 改變格線點的文字 grid on % 加上格線

40 圖軸控制範例-3 (II)

41 圖軸控制範例-4 (I) 將格線點xtick的數字改為文字 範例: x = [1:6];y=[13,5,7,14,10,12];
plot(x, y, ‘o’, x, y) set(gca, ‘xtick', [1:6], axis([ ]), xlabe;(‘Month’); set(gca, ‘xticklabel’, [‘Jan’;’’Feb’;’Mar’;’Apr’;’May’;’Jun’]); Ylabel(‘Monthly Sales ($1000)’); Title(‘Printer sales for January to June, 1997’);

42 圖軸控制範例-4 (II)

43 一次畫出多條曲線在同一圖上Ex4_3.m dx=.01; x=.5*dx:dx:10-0.5*dx; y=sin(5*x);
y2=cos(x); % the second function % plot both plot(x,y,'r-',x,y2,'b-');

44 一次畫出多條曲線在同一圖上 x=-pi:pi/20:pi; y1=sin(x); y2=cos(x);
plot(x,y1,’b-’, x,y2,’k—’); legend (‘sin(x)’,’cos(x)’);

45 分批畫出多條曲線在同一圖上 x=-pi:pi/20:pi; y1=sin(x); y2=cos(x); plot(x,y1,’b-’);
hold on; plot(x,y2,’k—’); hold off legend (‘sin(x)’,’cos(x)’);

46 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)’)

47 Possible locations for a plot legend

48

49 分批畫出多條曲線在不同視窗 figure(1) figure(2) x=-pi:pi/20:pi; y1=sin(x);
y2=cos(x); plot(x,y1,’b-’); legend (‘sin(x)’); figure(2) plot(x,y2,’k—’); legend (’cos(x)’);

50 Subplot subplot 在一個視窗產生多個圖形(圖軸) 一般形式為 subplot (m, n, p) 將視窗分為 m ×n 個區域
下一個 plot 指令繪圖於第 p 個區域 p 的算法為由左至右,一列一列

51 圖軸控制範例-4 (I) 同時畫出四個圖於一個視窗中 範例:plotxy13.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); % 此為右下角圖形

52 圖軸控制範例-4 (II) Subplot(2,2,1) Subplot(2,2,2) Subplot(2,2,3)

53 Subplot(2,1,1) Subplot(2,1,2)

54

55 圖軸控制範例-5 (I) 長寬比(Aspect Ratio) 一般圖軸長寬比是視窗的長寬比 可在 axis 指令後加不同的字串來修改
範例:plotxy14.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

56 圖軸控制範例-5 (II) axis normal axis square axis equal axis equal tight

57 改變圖軸長寬比的指令 改變目前圖軸長寬比的指令 需在 plot 指令之後呼叫才能發揮效用 指令 說明 axis normal
使用預設長寬比(等於圖形長寬比) axis square 長寬比例為 1 axis equal 長寬比例不變,但兩軸刻度一致 axis equal tight 兩軸刻度比例一致,且圖軸貼緊圖形 axis image 兩軸刻度比例一致(適用於影像顯示)

58 改變圖軸背景顏色的指令 colordef 改變圖軸與視窗之背景顏色 先呼叫 colordef 指令,其後 plot 指令產生的圖形才有效用
說明 colordef white 圖軸背景為白色,視窗背景為淺灰色 colordef black 圖軸背景為黑色,視窗背景為暗灰色 colordef none 圖軸背景為黑色,視窗背景為黑色(這是 MATLAB 第 4 版的預設值)

59 grid 和 box 指令 畫出格線或畫出圖軸外圍的方形 指令 說明 grid on 畫出格線 grid off 取消格線 box on
畫出圖軸的外圍長方形 box off 取消圖軸的外圍長方形

60 加入說明文字 在圖形或圖軸加入說明文字,增進整體圖形的可讀性 指令 說明 title 圖形的標題 xlabel x 軸的說明 ylabel
zlabel z 軸的說明(適用於立體繪圖) legend 多條曲線的說明 text 在圖形中加入文字 gtext 使用滑鼠決定文字的位置

61 說明文字範例-1 (I) 範例:plotxy15.m subplot(1,1,1); x = 0:0.1:2*pi;
y1 = sin(x); y2 = exp(-x); plot(x, y1, '--*', x, y2, ':o'); xlabel('t = 0 to 2\pi'); ylabel('values of sin(t) and e^{-x}') title('Function Plots of sin(t) and e^{-x}'); legend('sin(t)','e^{-x}');

62 說明文字範例-1 (II) legend 指令 「\」為特殊符號 畫出一小方塊,包含每條曲線的說明 產生上標、下標、希臘字母、數學符號等
遵循一般 LaTex 或 TeX數學模式

63 說明文字範例-2 (I) text指令 使用語法: text(x, y, string) x、y :文字的起始座標位置
範例:plotxy16.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(5\pi/4) = \rightarrow', 'HorizontalAlignment', 'right');

64 說明文字範例-2 (II) 「HorizontalAlignment」及「right」指示 text 指令將文字向右水平靠齊

65 Enhanced control of Text Strings
\bf Boldface 粗體字 \it Italics 斜體字 \rm Remove modifiers 取消字體修飾,恢復預設字體 \fontname{fontname) 定義使用的字型名稱 \fontsize{fontsize} 定義使用的字型大小 _{xxx} {}內的字為下標 ^{xxx} {}內的字為上標

66 ‘\bf\itx_{1}^{2} + x_{2}^{2} \rm(units: \bfm^{2}\rm) ’
練習 ‘\bf{B}_{\itS}’ Bs ‘\tau\it_{m}’ m ‘\bf\itx_{1}^{2} + x_{2}^{2} \rm(units: \bfm^{2}\rm) ’

67 Enhanced control of Text Strings
\circ \sim \infty \pm \leq \geq \neq \prop \div \int \cong \leftarrow \rightarrow \uparrow \downarrow \leftrightarrow

68 範例:ex4_1.m close all; x=.01:.01:20; y=cos(x)./x; plot(x,y);
axis([ ]); str1='\alpha \beta \gamma \delta \epsilon \phi '; str2='\theta \kappa \lambda \mu \nu \pi '; str3='\rho \sigma \tau \xi \zeta'; xlabel([str1 str2 str3]); text(10,1,'\theta_1'); text(10,2,'\theta_{12}'); text(10,3,'\theta^{10}');

69 Ex4_2.m dx=.01; x=.5*dx: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)

70 gtext指令 使用語法 gtext(string) 在圖上點選一位置後,string 顯示在其上。 gtext 只能用在二維平面繪圖

71 gtext

72 Enhanced control of Text Strings
\alpha \beta \gamma \delta \epsilon \eta \theta \lambda \mu \nu \pi \phi \rho \sigma \tau \omega \Omega \Sigma \Pi \Lambda \Delta \Gamma

73 練習:ideal gas law PV=nRT
請完成本程式,結果如下圖所示 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; loglog(…); hold off; legend(‘T=273 K’,’T=373K’);

74 The power function y = 2x -0.5 and the exponential function y = 101-x .

75 Exponential function : polyfit(x,y,n)

76 The exponential function: y = b(10)mx. In this case
log10 y = mx + log10b which has the form w = p1z + p2 where the polynomial variables w and z are related to the original data variables x and y by w = log10 y and z = x. We can find the exponential function that fits the data by typing p = polyfit(x, log10(y),1) The first element p1 of the vector p will be m, and the second element p2 will be log10b. We can find b from b = 10p2 .

77 時間,秒 溫度,oF 145 620 130 2266 103 3482 90 詳見 coffee.m 範例:咖啡冷卻問題 室溫 68oF下瓷杯內的咖啡由 145oF逐漸冷卻,溫度對應於經過時間的數據如上,請建立溫度對時間的函數,並與此模式預測,經過多少時間,杯內溫度會達120oF? time=[0,620,2266,3482]; temp=[145,130,103,90]; temp=temp-68; subplot(2,2,1); plot(time,temp, time,temp,’o’),xlabel(‘Time (sec)’),… ylabel(‘Relative Temperature (deg F)’) subplot(2,2,2); semilogy(time,temp, time,temp,’o’),xlabel(‘Time (sec)’),…

78 範例:咖啡冷卻問題 (續) P=polyfit(time, log10(temp),1); m=p(1); b=10^p(2);
t_120=(log10(120-68)-log10(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,120,'+'),xlabel('Time (sec)'),... ylabel('Temperature (deg F)'););

79

80 Power function: polyfit(x,y,n)

81 The power function: y = bxm. In this case
log10 y = m log10x + log10b which has the form w = p1z + p2 where the polynomial variables w and z are related to the original data variables x and y by w = log10 y and z = log10x. Thus we can find the power function that fits the data by typing p = polyfit(log10(x),log10(y),1) The first element p1 of the vector p will be m, and the second element p2 will be log10b. We can find b from b = 10p2 .

82 互動式繪圖 Interactive Plotting

83 Tools on the figure toolbar

84 Edit tool

85

86

87

88

89 產生 M 檔案 點選File 選單 選取 Generate M-File 選項

90 Figures 的存檔 Figure 的預設存入檔案名稱為 .fig.
選取Figure 視窗中 File menu 內的 Save 選項 或者直接點選工具列內的 Save 按鈕 (磁碟片圖示). 該檔案為首次存入時會先出現 Save As 對話框確認存入的是 MATLAB Figure (*.fig). 確認檔名後可以點選 OK.

91 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.

92 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.

93 其他平面繪圖指令 各種二維繪圖指令 指令 說明 errorbar 在曲線加上誤差範圍 fplot、ezplot 較精確的函數圖形
polar、ezpolar 極座標圖形 hist 直角座標直方圖(累計圖) rose 極座標直方圖(累計圖) compass 羅盤圖 feather 羽毛圖 area 面積圖(第五章「特殊圖形」介紹) stairs 階梯圖(第五章「特殊圖形」介紹)

94 其他平面繪圖範例-1 (I) 已知資料的誤差範圍,用 errorbar 表示 以 y 座標高度 20% 作為做資料的誤差範圍
範例:plotxy17.m x = linspace(0,2*pi,30); % 在 0 到 2 間,等分取 30 個點 y = sin(x); e = y*0.2; errorbar(x,y,e) % 圖形上加上誤差範圍 e

95 其他平面繪圖範例-1 (II) 誤差範圍

96 其他平面繪圖範例-2 (I) fplot 指令 對劇烈變化處進行較密集的取樣 範例:plotxy18.m
fplot('sin(1/x)', [ ]); % [ ]是繪圖範圍

97 其他平面繪圖範例-2 (II) 此區作較精確的取點繪圖

98 x=([1:0.01:2]; plot(x, cos(tan(x)-tan(sin(x)));

99 fplot(‘cos(tan(x)-tan(sin(x))', [1 2]); % [1 2]是繪圖範圍

100 Plotting Polynomials with the polyval Function.
To plot the polynomial 3x5 + 2x4 – 100x3 + 2x2 – 7x + 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’);

101 其他平面繪圖範例-3 (I) polar 指令 產生極座標圖形 範例:plotxy19.m
theta = linspace(0, 2*pi); r = cos(4*theta); polar(theta, r); % 進行極座標繪圖

102 其他平面繪圖範例-3 (II)

103 範例 某環繞太陽運轉的星球之極座標方程式 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’);

104 徑向直線顯示90與270o時,該星球與太陽的距離為2AU
原點為太陽的位置 半徑為1的圓為地球的軌跡 180o時,該星球與太陽的距離為最短,僅1.3AU 0o時,該星球與太陽有最遠的距離,達4AU 徑向直線顯示90與270o時,該星球與太陽的距離為2AU 橢圓為該星球的軌跡,太陽位於其焦點之一

105 直方圖及hist指令 直方圖(Histogram) hist指令 對大量的資料,顯示資料的分佈情況和統計特性
將資料依大小分成數堆,將每堆的個數畫出

106 其他平面繪圖範例-4 (I) 將 10000 個由 randn 產生的正規分佈之亂數分成 25 堆 範例:plotxy20.m
x = randn(10000, 1); % 產生 個正規分佈亂數 hist(x, 25); % 繪出直方圖,顯示 x 資料的分佈情 %況和統計特性,數字 25 代表資料依 %大小分堆的堆數,即是指方圖內長條 %的個數 set(findobj(gca, ‘type’, ‘patch’), ‘edgecolor’, ‘w’); % 將長條 %圖的邊緣設定成白色

107 其他平面繪圖範例-4 (II) 直方圖逼近亂數的機率分佈函數,且資料量越大時,逼近程度越高

108 其他平面繪圖範例-5 (I) rose指令 角度:資料大小 距離:資料個數 以極座標繪製表示 範例:plotxy21.m
x = randn(5000, 1); % 產生 5000 個正規分佈的亂數 rose(x); % x 資料大小為角度,x 資料個數為距離, %以繪製類似玫瑰花瓣的極座標直方圖

109 其他平面繪圖範例-5 (II)

110 其他平面繪圖範例-6 (I) compass 指令 畫出以原點為起始點的向量圖 稱為「羅盤圖」 範例:plotxy22.m
theta = linspace(0, 2*pi, 50); rho = sin(0.5*theta); [x, y] = pol2cart(theta, rho); % 由極座標轉換至直角座標 compass(x, y); % 畫出以原點為向量起始點 % 的羅盤圖

111 其他平面繪圖範例-6 (II) 若只有一個引數輸入z 將 z 的實部做為 x 座標,將 z 的虛部做為 y 座標,再進行作圖
範例:plotxy23.m 若只有一個引數輸入z 將 z 的實部做為 x 座標,將 z 的虛部做為 y 座標,再進行作圖 compass(z) 即等效於 compass(real(z),imag(z)) 上述四列程式碼可簡化 theta = linspace(0, 2*pi, 50); compass(sin(0.5*theta).*exp(j*theta));

112 其他平面繪圖範例-7 (I) 羽毛圖 起始點是 (k, 0),k = 1~n ,其中 n 是向量個數 範例:plotxy24.m
theta = linspace(0.2*pi,50); rho = 10; [x, y] = pol2cart(theta,rho); % 由極座標轉換至直角座標 feather(x, y); % 繪製羽毛圖

113 其他平面繪圖範例-7 (II)

114 練習 試寫一函數 regPoly(n),其功能為畫出一個圓心在 (0, 0)、半徑為 1 的圓,並在圓內畫出一個內接正 n 邊形,其中一頂點位於 (0, 1)。例如 regPoly(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

115 Function plotParam t = linspace(0, 4*pi); x = sin(t);
一條參數式的曲線可由下列方程式表示: x = sin(t), y = 1 - cos(t) + t/10 當 t 由 0 變化到 4*pi 時,請寫一個 MATLAB 的腳本 plotParam.m,畫出此曲線在 XY 平面的軌跡。 Function plotParam t = linspace(0, 4*pi); x = sin(t); y = 1-cos(t)+t/10; plot(x, y, '-o');

116 試寫一函數 regStar(n),其功能為畫出一個圓心在 (0, 0)、半徑為 1 的圓,並在圓內畫出一個內接正 n 星形,其中一頂點位於 (0, 1)。例如 regStar(7) 可以畫出如下之正 7 星型: function regStar(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

117 平面上的橢圓可表成:(x/a)2+(y/b)2=1, 橢圓參數式表示成: x = a. cos(t) 與 y = a
平面上的橢圓可表成:(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

118 請用寫一個 MATLAB 腳本 figSolve.m,利用圖解法,說明下列聯立方程式有無窮多組解:y = x; y = sin(1/x)
Function figSolve x=linspace(0,1); plot(x, x, x, sin(1./x));

119 當一個小圓輪在平面上滾動時,輪緣的一點在滾動時所形成的軌跡稱為「擺線」(Cycloid)。請用 MATLAB 畫出一個典型的擺線,其中小圓輪的半徑為 1,而且至少要滾三圈。
clear all; r=1; t1=linspace(0,2*pi,1000); t2=linspace(0,6*pi,1000); x1=r*(t1-sin(t1)); y1=r*(1-cos(t1)); x2=r*(t2-sin(t2)); y2=r*(1-cos(t2)); subplot(2,1,1);plot(x1,y1); title('小圓輪滾一圈'); subplot(2,1,2);plot(x2,y2); title('小圓輪滾三圈');

120 當一個小圓輪沿著一條曲線行進時,輪緣任一點的軌跡就會產生變化豐富的擺線。假設小圓輪的半徑 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); % 內花瓣線 c1=(R-r)*exp(i*t)+r*exp(i*(-R*t/r+t)); % 外花瓣線 c2=(R+r)*exp(i*t)+r*exp(i*(pi+R*t/r+t)); plot(real(c), imag(c), real(c1), imag(c1), real(c2), imag(c2)); axis image title('內花瓣線(綠色)和外花瓣線(紅色)');

121

122 若大圓和小圓的半徑成整數比,當小圓在大圓的內部滾動時,小圓內的任一點 A 的軌跡就會形成一個漂亮無缺的花瓣線。當大圓半徑 R 為 10,小圓半徑 r 為 3,且 A 點離小圓圓心距離 r1 為 2 時,請畫出此完整的花瓣線。 r=3; % 小圓半徑 R=10; % 大圓半徑 d=2; % 筆心離小圓圓心距離 n=r/gcd(r, R); % 圈數 t=linspace(0, n*2*pi, 1000); c=R*exp(i*t); % 內花瓣線 c1=(R-r)*exp(i*t)+d*exp(i*(-R*t/r+t)); % 外花瓣線 c2=(R+r)*exp(i*t)+d*exp(i*(pi+R*t/r+t)); plot(real(c), imag(c), real(c1), imag(c1), real(c2), imag(c2)); axis image title('內花瓣線(綠色)和外花瓣線(紅色)');

123

124

125 HW#2 Snell’s Law n1 sin(θ1)=n2 sin(θ2)
光線由空氣進入水中,請繪圖 plot(x,y) ,其中x為入射角由0~90度,y為R折射角 光線由水進入空氣中,其他同(a)

126 HW#2


Download ppt "Chap.3 二維平面繪圖 方煒 台大生機系."

Similar presentations


Ads by Google