Presentation is loading. Please wait.

Presentation is loading. Please wait.

MATLAB 程式設計入門篇 三維立體繪圖 (part1)

Similar presentations


Presentation on theme: "MATLAB 程式設計入門篇 三維立體繪圖 (part1)"— Presentation transcript:

1 MATLAB 程式設計入門篇 三維立體繪圖 (part1)
改自張智星講義 清大資工系 多媒體檢索實驗室 2009/12/29

2 4-1 基本立體繪圖指令 mesh 和 surf: mesh:可畫出立體的「網狀圖」(Mesh Plots)
surf:可畫出立體的「曲面圖」(Surface Plots) 範例4-1: plotxyz001.m z = [0 2 1; 3 2 4; 4 4 4; 7 6 8]; mesh(z); grid on; xlabel('X 軸 = column index'); % X 軸的說明文字 ylabel('Y 軸 = row index'); % Y 軸的說明文字 2009/12/29

3 4-1 基本立體繪圖指令 範例4-1 :plotxyz001.m 2009/12/29

4 4-1 基本立體繪圖指令 範例4-2 :plotxyz002.m
若要將與曲面對應的 x 座標和 y 座標都一併畫出來,還是可以使用 mesh 指令 z = [0 2 1; 3 2 4; 4 4 4; 7 6 8]; mesh(z); xlabel('X 軸 = column index'); % X 軸的說明文字 ylabel('Y 軸 = row index'); % Y 軸的說明文字 for i=1:size(z,1) for j=1:size(z,2) h=text(j, i, z(i,j), num2str(z(i, j))); % 標示曲面高度 end 2009/12/29

5 4-1 基本立體繪圖指令 範例4-2 :plotxyz002.m 2009/12/29

6 4-1 基本立體繪圖指令 範例4-3 :plotxyz011.m
meshgrid 的作用是產生 x 及 y (均為向量) 為基準的格子點 (Grid Points),其輸出為 xx 及 yy(均為矩陣),分別代表格子點的 x 座標及 y 座標。 x = 3:6; y = 5:9; [xx, yy] = meshgrid(x, y); % xx 和 yy 都是矩陣 (3,5) (4,5) (5,5) (6,5) (3,6) (4,6) (5,6) (6,6) (3,7) (4,7) (5,7) (6,7) (3,8) (4,8) (5,8) (6,8) (3,9) (4,9) (5,9) (6,9) 2009/12/29 xx(:,1) xx(:,2) xx(:,3) xx(:,4)

7 4-1 基本立體繪圖指令 範例4-3 :plotxyz011.m x = 3:6; y = 5:9;
[xx, yy] = meshgrid(x, y); % xx 和 yy 都是矩陣 zz = xx.*yy; % 計算函數值 zz,也是矩陣 subplot(2,2,1); mesh(xx); title('xx'); axis tight subplot(2,2,2); mesh(yy); title('yy'); axis tight subplot(2,2,3); mesh(xx, yy, zz); title('zz 對 xx 及 yy 作圖'); axis tight colormap(zeros(1,3)); % 以黑色呈現 2009/12/29

8 4-1 基本立體繪圖指令 範例4-3 :plotxyz011.m 2009/12/29

9 4-1 基本立體繪圖指令 範例4-4 :plotxyz01.m 使用 linspace 來產生較密集的資料,以便畫出由函數形成的立體網狀圖
x = linspace(-2, 2, 25); % 在 x 軸 [-2,2] 之間取 25 點 y = linspace(-2, 2, 25); % 在 y 軸 [-2,2] 之間取 25 點 [xx, yy] = meshgrid(x, y); % xx 和 yy 都是 25×25 的矩陣 zz = xx.*exp(-xx.^2-yy.^2); % 計算函數值,zz 也是 25×25 的矩陣 mesh(xx, yy, zz); % 畫出立體網狀圖 2009/12/29

10 4-1 基本立體繪圖指令 範例4-4 :plotxyz01.m 2009/12/29

11 4-1 基本立體繪圖指令 範例4-5 :plotxyz01.m surf 和 mesh 指令的用法類似
x = linspace(-2, 2, 25); % 在 x 軸 [-2,2] 之間取 25 點 y = linspace(-2, 2, 25); % 在 y 軸 [-2,2] 之間取 25 點 [xx,yy] = meshgrid(x, y); % xx 和 yy 都是 25×25 的矩陣 zz = xx.*exp(-xx.^2-yy.^2); % zz 也是 25×2 的矩陣 surf(xx, yy, zz); % 畫出立體曲面圖 2009/12/29

12 4-1 基本立體繪圖指令 範例4-5 :plotxyz01.m 2009/12/29

13 4-1 基本立體繪圖指令 peaks: 為了方便測試立體繪圖,MATLAB 提供了一個 peaks 函數,可產生一個凹凸有致的曲面,包含了三個局部極大點(Local Maxima)及三個局部極小點(Local Minima) 其方程式為: 2009/12/29

14 4-1 基本立體繪圖指令 畫出此函數的最快方法,即是在 MATLAB 命令視窗直接鍵入 peaks,可得到下列方程式
z = 3*(1-x).^2.*exp(-(x.^2) - (y+1).^2) ... - 10*(x/5 - x.^3 - y.^5).*exp(-x.^2-y.^2) ... - 1/3*exp(-(x+1).^2 - y.^2) 2009/12/29

15 4-1 基本立體繪圖指令 peaks的圖形 2009/12/29

16 4-1 基本立體繪圖指令 meshz: meshz 指令有將曲面加上「圍裙」或「舞台」的效果 範例4-6:plotxyz03.m
[x, y, z] = peaks; meshz(x,y,z); axis tight; 2009/12/29

17 4-1 基本立體繪圖指令 範例4-6:plotxyz03.m 2009/12/29

18 4-1 基本立體繪圖指令 waterfall: waterfall 指令可在 x 方向或 y 方向產生水流效果
範例4-7:plotxyz04.m [x, y, z] = peaks; waterfall(x,y,z); axis tight; 2009/12/29

19 4-1 基本立體繪圖指令 範例4-7:plotxyz04.m 2009/12/29

20 4-1 基本立體繪圖指令 meshc: meshc 可同時畫出網狀圖與「等高線」(Contours) 範例4-8:plotxyz05.m
[x, y, z] = peaks; meshc(x, y, z); axis tight; 2009/12/29

21 4-1 基本立體繪圖指令 範例4-8:plotxyz05.m 2009/12/29

22 4-1 基本立體繪圖指令 plot3: plot3 指令可畫出三度空間中的曲線 範例4-9:plotxyz06.m
t = linspace(0,20*pi, 501); % 在 0 及 20*pi 中間取 501 點 plot3(t.*sin(t), t.*cos(t), t); % 畫出 tsin(t),tcos(t),t 的曲線 2009/12/29

23 4-1 基本立體繪圖指令 範例4-9:plotxyz06.m 2009/12/29

24 4-1 基本立體繪圖指令 plot3: 亦可同時畫出兩條三度空間中的曲線 範例4-10:plotxyz07.m
t = linspace(0, 10*pi, 501); plot3(t.*sin(t), t.*cos(t), t, t.*sin(t), t.*cos(t), -t); % 同時畫兩條曲線 2009/12/29

25 4-1 基本立體繪圖指令 範例4-10:plotxyz07.m 2009/12/29

26 4-1 基本立體繪圖指令 plot3: 如果輸入引數是三個大小相同的矩陣 x、y、z,那麼 plot3 會依序畫出每個行向量在三度空間所對應的曲線 範例4-11:plotxyz08.m [x, y] = meshgrid(-2:0.1:2); z = y.*exp(-x.^2-y.^2); plot3(x, y, z); 第 i 行 第 i 行 第 i 行 X= ,Y= ,Z= 第i條curve相當於plot3( , , ) 2009/12/29

27 4-1 基本立體繪圖指令 範例4-11:plotxyz08.m 2009/12/29

28 4-1 基本立體繪圖指令 整理:基本三維立體繪圖指令的列表 類別 指令 說明 網狀圖 mesh, ezmesh 立體網狀圖
meshc, ezmeshc 網狀圖加上等高線 meshz 網狀圖加上“圍裙”(或“舞臺”) 曲面圖 surf, ezsurf 立體曲面圖 surfc, ezsurfc 曲面圖加上等高線 surfl 曲面圖加上光源 2009/12/29

29 4-1 基本立體繪圖指令 整理:基本三維立體繪圖指令的列表 類別 指令 說明 曲線圖 plot3, ezplot3 立體曲線圖 低階函數
surface Surf 用到的低階指令 line3 Plot3 用到的低階指令 等高線 contour, ezcontour 平面上的等高線 contour3 空間中的等高線 影像表示 pcolor 在二維平面中以顏色表示曲面的高度 2009/12/29

30 4-1 基本立體繪圖指令 ezmesh, ezsurf :
範例4-13:plotxyz091.m subplot(2,2,1); ezmesh('sin(x)/x*sin(y)/y'); subplot(2,2,2); ezsurf('sin(x*y)/(x*y)'); subplot(2,2,3); ezmeshc('sin(x)/x*sin(y)/y'); subplot(2,2,4); ezsurfc('sin(x*y)/(x*y)'); 2009/12/29

31 4-1 基本立體繪圖指令 範例4-13:plotxyz091.m 2009/12/29


Download ppt "MATLAB 程式設計入門篇 三維立體繪圖 (part1)"

Similar presentations


Ads by Google