不使用球体函数的 Matlab 球体图

问题描述 投票:0回答:4

我试图在 Matlab 中绘制一个球体而不使用 Sphere 函数。这是我的代码:

r = 2;
[ x,y ] = meshgrid(-4:0.1:4);
z = sqrt(r^2-x.^2-y.^2);
mesh(real(z));
hold on 
mesh(real(-z));

上面的代码确实生成了方程 r^2=x^2+y^2+z^2 的球体。唯一的问题是有一个水平面切割球体。

我的问题是如何绘制一个不显示水平面的球体?

我不使用球体函数的原因是因为我想绘制表面方程。如果我使用 Sphere 函数,那么 Matlab 会假设我的表面将是一个球体。

matlab
4个回答
5
投票

您应该考虑切换到极坐标。 MATLAB 可以绘制拓扑等效于矩形网格的曲面:

N = 20;
thetavec = linspace(0,pi,N);
phivec = linspace(0,2*pi,2*N);
[th, ph] = meshgrid(thetavec,phivec);
R = ones(size(th)); % should be your R(theta,phi) surface in general

x = R.*sin(th).*cos(ph);
y = R.*sin(th).*sin(ph);
z = R.*cos(th);

figure;
surf(x,y,z);
axis vis3d

技巧是在极坐标中你有一个矩形网格。

正如您在上面的公式中看到的,在这个约定中,

theta
是极角,
phi
是方位角,这在数学和物理中很常见。您可以使用
sph2cart
进行从球面坐标到笛卡尔坐标的转换,但是您需要输入角度的方位角和仰角,它们的定义有点不同。


1
投票

我是新来的,但我确实为 matlab 做了一些,但没有使用球体函数 虽然用fmesh 将球体分为两部分,并使用 2 个函数绘制正值和负值

我展示的一个函数的示例可能是

f=@(x,y) sqrt(3 - (sqrt(3).*(x-4)).^2 -  (sqrt(3).*(y-2)).^2)-5
              ^                ^                              ^
radius of the sphere    position on x axis            position on z axis

完整代码

f=@(x,y) sqrt(3 - (sqrt(3).*(x-4)).^2 -  (sqrt(3).*(y-2)).^2)-5
fmesh(f,[-10 10 -10 10],'ShowContours','on')

hold

f1=@(x,y) -sqrt(3 -  (sqrt(3).*(x-4)).^2 -  (sqrt(3).*(y-2)).^2)-5
fmesh(f1,[-10 10 -10 10],'ShowContours','on')

hold off

0
投票

当然有更好的图...但是如果您将 z 矩阵中的条目设置为 nan,它就可以工作:

temp = real(z);
temp(temp==0) = nan; 

或者您可以使用隐式 3D 绘图。在 matlab 文件交换中,您可以找到相应的函数(Matlab File Excahnge) 相应的脚本如下所示:

f = 'x^2 +y^2 +z^2 -4';
ezimplot3(f,[-5 5])

0
投票

最上面的答案可以很容易地推广到任何一般的椭球体,例如:

N = 20;
theta = linspace(0,pi,N);
phi = linspace(0,2*pi,2*N);
[th, ph] = meshgrid(theta,phi);
r = ones(size(th));

x = a*r.*sin(th).*cos(ph);
y = b*r.*sin(th).*sin(ph);
z = c*r.*cos(th);

m = max([a b c]);
axlims = [-m m];

figure;
surf(x,y,z);
xlim(axlims);
ylim(axlims);
zlim(axlims);
xlabel('X axis');
ylabel('Y axis');
zlabel('Z axis');
axis vis3d

使用

a=2 b=1.5 c=1
你会得到: enter image description here

使用

a=1.5 b=1.5 c=1
(“扁球体”),您将得到: enter image description here

使用

a=1 b=1 c=1
,您可以恢复答案中的球体。

© www.soinside.com 2019 - 2024. All rights reserved.