2012-03-23 241 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)');