1 / 39

MATLAB 三維立體繪圖

MATLAB 三維立體繪圖. 今天會用到大小為 m×n 的矩陣. 在每一橫列結尾加上分號( ; ),例如: >> A = [1 2 3 4; 5 6 7 8; 9 10 11 12]; % 建立 3×4 的矩陣 A >> A % 顯示矩陣 A 的內容 A = 1 2 3 4 5 6 7 8 9 10 11 12 [1 2 3 4] 等同於 [1,2,3,4] 但不同於 [1;2;3;4].

mabli
Télécharger la présentation

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 三維立體繪圖

  2. 今天會用到大小為 m×n的矩陣 • 在每一橫列結尾加上分號(;),例如: >> A = [1 2 3 4; 5 6 7 8; 9 10 11 12]; % 建立 3×4 的矩陣 A >> A % 顯示矩陣 A 的內容 A = 1 2 3 4 5 6 7 8 9 10 11 12 [1 2 3 4] 等同於[1,2,3,4] 但不同於[1;2;3;4]

  3. + - * / ^ default Matrix.+ .- .* ./ .^ elememt to element 設 A=[1 2 3 4]; B=[5 6 7 8] 則A*B=? 執行結果: ??? Error using ==> * Inner matrix dimensions must agree. 設 A=[1 2 3 4]; B=[5 ;6; 7; 8] 則A.*B=? 執行結果: ??? Error using ==> .* Matrix dimensions must agree.

  4. 設 A=[1 2 3 4]; B=[5 6 7 8] 則A.*B=? ans = 5 12 21 32 設 A=[1 2 3 4]; B=[5 ;6; 7; 8] 則A*B=? ans = 70 設 A=[1 2 3 4]; B=[5 ,6, 7, 8] 則A.*B=? ans = 5 12 21 32

  5. 基本立體繪圖指令 • mesh 和 surf: • mesh:可畫出立體的「網狀圖」(Mesh Plots) • surf:可畫出立體的「曲面圖」(Surface Plots)

  6. z = [0 2 1; 3 2 4; 4 4 4; 7 6 8];

  7. z = [0 2 1; 3 2 4; 4 4 4; 7 6 8];mesh(z);xlabel('X 軸 = columnn index');ylabel('Y 軸 = row index');

  8. meshgrid meshgrid 的作用是產生 x 及 y (均為向量) 為基準的格子點 (Grid Points),其輸出為 xx 及 yy(均為矩陣),分別代表格子點的 x 座標及 y 座標。

  9. x = 3:6;y = 5:9;[xx, yy] = meshgrid(x, y);% xx 和 yy 都是矩陣 • xx = • 3 4 5 6 • 3 4 5 6 • 3 4 5 6 • 3 4 5 6 • 3 4 5 6 • yy = • 5 5 5 5 • 6 6 6 6 • 7 7 7 7 • 8 8 8 8 • 9 9 9 9

  10. zz = 15 20 25 30 18 24 30 36 21 28 35 42 24 32 40 48 27 36 45 54 xx = 3 4 5 6 3 4 5 6 3 4 5 6 3 4 5 6 3 4 5 6 yy = 5 5 5 5 6 6 6 6 7 7 7 7 8 8 8 8 9 9 9 9 zz = xx.*yy; % 計算函數值 zz,也是矩陣

  11. 注意meshgrid(x)和meshgrid(x,y)之不同 • >> x = 3:6; • >> y = 5:9; • >> xx=meshgrid(x) • xx = • 3 4 5 6 • 3 4 5 6 • 3 4 5 6 • 3 4 5 6 • >> yy=meshgrid(y) • yy = • 5 6 7 8 9 • 5 6 7 8 9 • 5 6 7 8 9 • 5 6 7 8 9 • 5 6 7 8 9 • >> x = 3:6; • >> y = 5:9; • >> [xx,yy]=meshgrid(x,y) • xx = • 3 4 5 6 • 3 4 5 6 • 3 4 5 6 • 3 4 5 6 • 3 4 5 6 • yy = • 5 5 5 5 • 6 6 6 6 • 7 7 7 7 • 8 8 8 8 • 9 9 9 9

  12. 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)); % 以黑色呈現

  13. mesh(xx), mesh(yy) and mesh(xx,yy,zz) • xx = • 3 4 5 6 • 3 4 5 6 • 3 4 5 6 • 3 4 5 6 • 3 4 5 6 • yy = • 5 5 5 5 • 6 6 6 6 • 7 7 7 7 • 8 8 8 8 • 9 9 9 9 • zz = • 15 20 25 30 • 18 24 30 36 • 21 28 35 42 • 24 32 40 48 • 27 36 45 54

  14. 使用 linspace 來產生較密集的資料,以便畫出由函數形成的立體網狀圖 >> x = linspace(-2, 2, 5); >> y = linspace(-2, 2, 5); [xx, yy] = meshgrid(x, y) xx = -2 -1 0 1 2 -2 -1 0 1 2 -2 -1 0 1 2 -2 -1 0 1 2 -2 -1 0 1 2 yy = -2 -2 -2 -2 -2 -1 -1 -1 -1 -1 0 0 0 0 0 1 1 1 1 1 2 2 2 2 2 Point-to-Point zz = xx.*exp(-xx.^2-yy.^2) zz = -0.0007 -0.0067 0 0.0067 0.0007 -0.0135 -0.1353 0 0.1353 0.0135 -0.0366 -0.3679 0 0.3679 0.0366 -0.0135 -0.1353 0 0.1353 0.0135 -0.0007 -0.0067 0 0.0067 0.0007

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

  16. surf 和 mesh 指令的用法類似 x = linspace(-2, 2, 25); y = linspace(-2, 2, 25); [xx,yy] = meshgrid(x, y); zz = xx.*exp(-xx.^2-yy.^2); surf(xx, yy, zz);

  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) >>[x, y, z] = peaks; >>meshz(x,y,z)

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

  20. meshz 指令有將曲面加上「圍裙」或「舞台」 [x, y, z] = peaks; meshz(x,y,z); axis tight;

  21. waterfall 指令可在 x 方向或 y 方向產生水流效果 [x, y, z] = peaks; waterfall(x,y,z); axis tight;

  22. meshc 可同時畫出網狀圖與「等高線」(Contours) [x, y, z] = peaks; meshc(x, y, z); axis tight;

  23. plot3 指令可畫出三度空間中的曲線 plot3: t = linspace(0,20*pi, 501); plot3(t.*sin(t), t.*cos(t), t);

  24. t = linspace(0, 10*pi, 501); plot3(t.*sin(t), t.*cos(t), t, t.*sin(t), t.*cos(t), -t); % 同時畫兩條曲線

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

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

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

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

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

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

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

  32. 我就是聽不懂ㄝ怎麼辦? ezmesh, ezsurf: 如果我們只是要很快地檢視一個具有二個輸入的函數的圖形,就可以使用 ezmesh 或是 ezsurf 等來快速地畫出函數的曲面圖形

  33. >> x=linspace(-pi,pi); >> y=linspace(0,2*pi); >> ezmesh('sin(x)/x*sin(y)/y')

  34. 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)');

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

  36. colorbar • >> x=linspace(-pi,pi); • >> y=linspace(0,2*pi); • >> ezmesh('sin(x)/x*sin(y)/y'); • >>colorbar

  37. 曲面顏色的控制 • 整理:MATLAB 現成的顏色對照表:

  38. 曲面使用感覺較冷的顏色 peaks; colormap cool; colorbar

  39. 曲面使用感覺較暖的顏色 [X, Y, Z] = peaks; surf(X, Y, Z, gradient(Z)); axis tight; colormap hot

More Related