1 / 71

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

MATLAB 程式設計入門篇 三維立體繪圖. 張智星 (Roger Jang) jang@mirlab.org http://mirlab.org/jang 台大資工系 多媒體檢索實驗室. 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);

alcina
Download Presentation

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

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. MATLAB 程式設計入門篇三維立體繪圖 張智星 (Roger Jang) jang@mirlab.org http://mirlab.org/jang 台大資工系 多媒體檢索實驗室

  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); xlabel('X 軸 = column index'); % X 軸的說明文字 ylabel('Y 軸 = row index'); % Y 軸的說明文字

  3. Matrix Indexing  Coordinate • Conversion from matrix indexing to x-y coordinates • jx • iy j (= x) i (= y) (i, j) = (3, 2) (x, y) = (2, 3)

  4. 4-1 基本立體繪圖指令 • 範例4-1 :plotxyz001.m

  5. 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))); % 標示曲面高度 set(h, 'hori', 'center', 'vertical', 'bottom', 'color', 'r'); % 改變位置及顏色 end end

  6. 4-1 基本立體繪圖指令 • 範例4-2 :plotxyz002.m

  7. 控制Y軸的方向 • axis xy: 由小變大 • axis ij: 由大變小

  8. 4-1 基本立體繪圖指令 • 範例4-3 :plotxyz011.m meshgrid 的作用是產生 x 及 y (均為向量) 為基準的格子點 (Grid Points),其輸出為 xx 及 yy(均為矩陣),分別代表格子點的 x 座標及 y 座標。

  9. 4-1 基本立體繪圖指令 • 若要產生一個曲面 z=x*y,其中x=3:6,y=5:9 x = .* y zz = xx .* yy Generated by meshgrid!

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

  11. 4-1 基本立體繪圖指令 • 範例4-3 :plotxyz011.m

  12. 4-1 基本立體繪圖指令 • 如果你不習慣使用meshgrid,也可以使用傳統的for-loop來一一填入曲面的值,但要小心座標和矩陣索引的對應關係。範例(plotxyz011.m)如下: x = 3:6; y = 5:9; zz=zeros(length(y), length(x)); for i=1:size(zz,1) for j=1:size(zz,2) zz(i,j)=y(i)*x(j); end end mesh(x, y, zz); xlabel('X'); ylabel('Y'); zlabel('Z'); axis tight

  13. 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); % 畫出立體網狀圖

  14. 4-1 基本立體繪圖指令 • 範例4-4 :plotxyz01.m

  15. 4-1 基本立體繪圖指令 • 範例4-5 :plotxyz02.msurf 和 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); % 畫出立體曲面圖

  16. 4-1 基本立體繪圖指令 • 範例4-5 :plotxyz02.m

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

  18. 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)

  19. 4-1 基本立體繪圖指令 • peaks的圖形

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

  21. 4-1 基本立體繪圖指令 • 範例4-6:plotxyz03.m

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

  23. 4-1 基本立體繪圖指令 • 範例4-7:plotxyz04.m

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

  25. 4-1 基本立體繪圖指令 • 範例4-8:plotxyz05.m

  26. 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 的曲線

  27. 4-1 基本立體繪圖指令 • 範例4-9:plotxyz06.m

  28. 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); % 同時畫兩條曲線

  29. 4-1 基本立體繪圖指令 • 範例4-10:plotxyz07.m

  30. 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);

  31. 4-1 基本立體繪圖指令 • 範例4-11:plotxyz08.m

  32. 4-1 基本立體繪圖指令 • plot3: • 上例中,所有的資料點都必需是在格子點上,MATLAB 才能根據每點的高度來作圖。如果所給的資料點不在格子點上,我們必需先用 griddata 指令來進行內插法以產生格子點

  33. 4-1 基本立體繪圖指令 • 範例4-12:plotxyz09.m x = 6*rand(100,1)-3; % x 為介於 [-3, 3] 的 100 點亂數 y = 6*rand(100,1)-3; % y 為介於 [-3, 3] 的 100 點亂數 z = peaks(x, y); % z 為 peaks 指令產生的 100 點輸出 [X, Y] = meshgrid(-3:0.1:3); Z = griddata(x, y, z, X, Y, 'cubic'); mesh(X, Y, Z); hold on plot3(x, y, z, '.', 'MarkerSize', 16); % 晝出 100 個取樣 hold off axis tight

  34. 4-1 基本立體繪圖指令 • 範例4-12:plotxyz09.m

  35. 4-1 基本立體繪圖指令 • 整理:基本三維立體繪圖指令的列表

  36. 4-1 基本立體繪圖指令 • 整理:基本三維立體繪圖指令的列表

  37. 4-1 基本立體繪圖指令 • ezmesh, ezsurf: • 如果我們只是要很快地檢視一個具有二個輸入的函數的圖形,就可以使用 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)');

  38. 4-1 基本立體繪圖指令 • 基礎 • span=4*pi*[-1, 1]; • ezplot('sin(x)/x', span);

  39. Result of ez-family

  40. 4-2 立體圖形與圖軸的基本技巧 • hidden off: • 在繪製網狀圖時,MATLAB 會隱藏被遮蓋的網線,若要使被遮蓋的網線亦能呈現出來,可用 hidden off 指令 • 若再鍵入 hidden on,則恢復原先的設定 • 範例4-14:plotxyz10.m [x,y,z] = peaks; mesh(x,y,z); hidden off axis tight

  41. 4-2 立體圖形與圖軸的基本技巧 • 範例4-14:plotxyz10.m

  42. 4-2 立體圖形與圖軸的基本技巧 • 整理:以 on/off 來切換的指令:

  43. 4-2 立體圖形與圖軸的基本技巧 • rotate3d on: • 若要能夠旋轉立體圖形,可已在產生 3D 圖形之後(例如輸入 peaks 之後),再輸入「rotate3d on」,此時您可以壓下滑鼠左鍵來拖曳圖軸,以選取最理想的觀測角度。 • 也可以點選圖形視窗上面的 圖示,就可以開始旋轉立體圖形。

  44. z y 觀測點 原點 Elevation x Azimuth 4-2 立體圖形與圖軸的基本技巧 • 三維曲線的觀測角度: • 一般而言,三維曲線的觀測角度是由 Azimuth(方位角)及 Elevation(仰角)來決定

  45. 4-2 立體圖形與圖軸的基本技巧 • 對二維圖形而言,預設值為 Azimuth = 0°,Elevation = 90°;對三維圖形而言,預設值為 Azimuth = -37.5°,Elevation = 30°。若要改變觀測角度,可用 view 指令 • 範例4-15:plotxyz11.m peaks; view([0,-30]);

  46. 4-2 立體圖形與圖軸的基本技巧 • 範例4-15:plotxyz11.m

  47. 4-2 立體圖形與圖軸的基本技巧 • NaN: • 有時候我們希望將曲面圖切掉一部份,以呈現不同的效果,此時可用 NaN 或 nan(Not a Number,即“非數值”)來取代矩陣某一部份的值,MATLAB 一碰到 NaN,就會“鏤空” • 範例4-16:plotxyz12.m [X, Y, Z] = peaks; Z(10:20,10:20) = nan; % 將 Z 矩陣的一部分代換為 nan surf(X, Y, Z); axis tight

  48. 4-2 立體圖形與圖軸的基本技巧 • 範例4-16:plotxyz12.m

  49. 4-3 曲面顏色的控制 • colorbar: • 利用 colorbar 指令,可顯示 MATLAB 如何以不同顏色來代表曲面的高度 • 例如先輸入「peaks」,再輸入「colorbar」

  50. 4-3 曲面顏色的控制 • 整理:常用顏色的 RGB 成分:

More Related