trying to model points inside a sphere

if true
clear
clear all
r=randn(0,1);
theta=randn(0,1)*(pi);
phi=randn(0,1)*(2*pi);
for N=1:20
hold on
x=r(N).*sin(theta(N)).*cos(phi(N));
y=r(N).*sin(theta(N)).*sin(phi(N));
z=r(N).*cos(theta(N));
end
hold off
drawnow
% code
end
I am trying to model a bunch of points inside a sphere, I put the spherical cooridinates into cartesian, but cannot get it to plot, any help would be appreciated.

5 comentarios

jonas
jonas el 3 de Mayo de 2018
The code stops because theta, phi and r are all empty, because randn(0,1) does not output anything. You are also overwriting the variables x,y and z each iteration (consider removing the loop). If you fix those issues, maybe the question becomes more clear.
Thank you very much jonas, I have made some adjustments to the code and it runs now, but am having trouble fine-tuning the graph. What I need is a sphere with the random points inside of it, I will attach my current code, any suggestions would be appreciated.
if true
clear
clear all
n=20;
r=rand(n,1);
theta=rand(n,1)*pi;
phi=rand(n,1)*(2*pi);
x=r.*sin(theta).*cos(phi);
y=r.*sin(theta).*sin(phi);
z=r.*cos(theta);
[X,Y,Z]=sphere;
hold on
scatter3(x,y,z)
plot3(X,Y,Z)
hold off
% code
end
jonas
jonas el 4 de Mayo de 2018
Editada: jonas el 4 de Mayo de 2018
No problem! I am a bit confused, because running your code gives me what you requested: a sphere with random points inside of it. Just type view(3) to get a 3d-perspective. If you want tips to improve the visualization, then you need to be a bit more specific in what you are trying to achieve. Try this for example:
if true
clear
clear all
n=20;
r=rand(n,1);
theta=rand(n,1)*pi;
phi=rand(n,1)*(2*pi);
x=r.*sin(theta).*cos(phi);
y=r.*sin(theta).*sin(phi);
z=r.*cos(theta);
sphere;
hold on
scatter3(x,y,z)
hold off
% code
end
ha=get(gca,'children')
set(ha(2),'FaceColor',[0 0 0],'FaceAlpha',0.1,'EdgeAlpha',0.2)
Devon Romine
Devon Romine el 4 de Mayo de 2018
I did not know about the view(3), that was what I needed thank you!
jonas
jonas el 4 de Mayo de 2018
Great! I will post an answer for future reference, if anyone else wants to model a sphere with random points

Iniciar sesión para comentar.

Respuestas (1)

jonas
jonas el 4 de Mayo de 2018
This will generate a sphere with random points inside of it (code adapted from the original question):
if true
clear
clear all
n=20;
r=rand(n,1);
theta=rand(n,1)*pi;
phi=rand(n,1)*(2*pi);
x=r.*sin(theta).*cos(phi);
y=r.*sin(theta).*sin(phi);
z=r.*cos(theta);
sphere;
hold on
scatter3(x,y,z)
hold off
% code
end
ha=get(gca,'children')
set(ha(2),'FaceColor',[0 0 0],'FaceAlpha',0.1,'EdgeAlpha',0.2)

1 comentario

Hi Jonas / Devin,
This code is pretty good, except if you try it for something like n = 2000 you will see a cluster of points in the middle of the sphere and also along the z axis. It's more usual to be looking for a uniform density of points in the sphere, which would be
r = rand(n,1).^(1/3);
costheta = 2*rand(n,1)-1;
sintheta = sqrt(1-costheta.^2);
phi=rand(n,1)*(2*pi);
x=r.*sintheta.*cos(phi);
y=r.*sintheta.*sin(phi);
z=r.*costheta;

Iniciar sesión para comentar.

Etiquetas

Preguntada:

el 3 de Mayo de 2018

Comentada:

el 5 de Mayo de 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by