简体   繁体   English

不使用球函数的 Matlab 球图

[英]Matlab Sphere plot without using the Sphere Function

I am trying to draw a sphere in Matlab without using the Sphere Function.我试图在不使用球体函数的情况下在 Matlab 中绘制一个球体。 This is my code:这是我的代码:

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

The code above does generate a sphere of equation r^2=x^2+y^2+z^2.上面的代码确实生成了方程 r^2=x^2+y^2+z^2 的球体。 The only problem is that there is a horizontal plane slicing the sphere.唯一的问题是有一个水平面切割球体。

My question is how can I plot a sphere that doesn't show the horizontal plane?我的问题是如何绘制一个不显示水平面的球体?

The reason I am not using a Sphere function is because I want to plot a surface equation.我不使用 Sphere 函数的原因是我想绘制一个表面方程。 If I use the Sphere function then Matlab assumes my surface will be a sphere.如果我使用 Sphere 函数,那么 Matlab 假设我的表面将是一个球体。

You should consider switching to polar coordinates.您应该考虑切换到极坐标。 MATLAB can plot surfaces that are topologically equivalent to a rectangular mesh: 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

结果

The trick is that in polar coordinates you have a rectangular mesh.诀窍是在极坐标中你有一个矩形网格。

As you can see in the above formulae, in this convention theta is the polar angle and phi is the azimuthal angle, as it is common in mathematics and physics.正如你在上面的公式中看到的,在这个约定中, theta是极角, phi是方位角,因为它在数学和物理学中很常见。 You can use sph2cart to do the transformation from spherical to Cartesian coordinates, but then you need to input azimuth and elevation for the angles, which have a bit different definitions.您可以使用sph2cart进行从球面坐标到笛卡尔坐标的转换,但是您需要输入角度的方位角和仰角,它们的定义略有不同。

Well there have certainly been nicer plots... but it works, if you just set the entry in the z-matrix to nan:嗯,当然有更好的情节......但它有效,如果你只是将 z-matrix 中的条目设置为 nan:

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

Or you can go with an implicit 3D plot.或者您可以使用隐式 3D 绘图。 In matlab file exchange you can find an according function ( Matlab File Excahnge ) The corresponding script would look like this:在 matlab 文件交换中,您可以找到相应的函数 ( Matlab File Excahnge ) 相应的脚本如下所示:

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

im new here but i did made some for matlab without using sphere function although with fmesh divide the sphere in 2 parts and use 2 functions to plot on positive and one negative我是新来的,但我确实为 matlab 做了一些没有使用球体函数虽然 fmesh 将球体分成 2 部分并使用 2 个函数绘制正负

im showing an example for one function could be我展示了一个功能的例子可能是

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

full code完整代码

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

X²+y²+z²=1 ( not negative coff) Draw the sphere X²+y²+z²=1(非负coff)绘制球体

Pls.answer![enter image description here]( https://i.stack.imgur.com/BTx4L.jpg请回答![在此处输入图片说明]( https://i.stack.imgur.com/BTx4L.jpg

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM