2012-03-23 237 views
4

我需要繪製多個領域和我所用的示例代碼從mathwork幫助如下 -如何在Matlab中繪製球體時定義半徑?

figure 
[x,y,z] = sphere(); 
surf(x,y,z) % sphere centered at origin 
hold on 
surf(x+3,y-2,z) % sphere centered at (3,-2,0) 
surf(x,y+1,z-3) % sphere centered at (0,1,-3) 
daspect([1 1 1]) 

我需要的領域是不同的半徑。我如何爲每個球體定義半徑?

回答

9

爲[sphere](http://www.mathworks.com.au/help/techdoc/ref/sphere.html)的幫助文件說,它產生用於單位球,或半徑1的球要半徑爲1的球改變座標半徑r你的球體只是乘法座標他們通過r

[x,y,z] = sphere(); 
r = 5; 
surf(r*x, r*y, r*z) % sphere with radius 5 centred at (0,0,0)  
2

IMO,surf()不是用戶友好的。代碼surf(x+3,y-2,z) % sphere centered at (3,-2,0)是反直覺的(surf(x-1,y+2,0)與數學一致)。

無論如何,我建議您使用ellipsoid()來代替。由於球是橢圓形的只是一個特例,你可以很容易地理解它,你不必應付surf(),看http://www.mathworks.com/help/matlab/ref/ellipsoid.html

一個簡單的例子:

r=5; 
[x,y,z]=ellipsoid(1,2,3,r,r,r,20); 
surf(x, y, z,'FaceColor','y', 'FaceAlpha', 0.2); 
axis equal; 
box on; xlabel('x-axis (m)'); ylabel('y-axis (m)'); zlabel('z-axis (m)');