1

How to generate random two concentric spheres synthetic data with radius1=40 and radius2 =100 in MATLAB and save that data in format *.mat with one variable.(1000*3 double)? Also, how to plot this data in 3D with 2 colors: red and blue?

My Code:

rng(0,'twister');
rvals = 2*rand(1000,1)-1;
elevation = asin(rvals);

azimuth = 2*pi*rand(1000,1);

radii = 3*(rand(1000,1).^(1/3));

[x,y,z] = sph2cart(azimuth,elevation,radii);
data=[x,y,z];
figure
plot3(x,y,z,'.');
axis equal

Expected Output:

enter image description here

Brian Tompsett - 汤莱恩
  • 5,438
  • 68
  • 55
  • 126
Mohammad
  • 161
  • 2
  • 10
  • 1
    What have you tried so far? Show us your code. Are the points distributed uniformly? Do you mean on the surfaces of each of the two spheres or in the space between exterior of the smaller sphere and the interior of the larger one? – horchler May 11 '15 at 01:33
  • uniformly distributed in 2 class.N=1000, class1 =N/2 , class 2=N/2, i want generate data set .i have not code – Mohammad May 11 '15 at 01:41
  • Don't put code in the comments. Edit your original question to improve it. – horchler May 11 '15 at 01:58
  • ok .It is very important for me Link is similar to existing photo. – Mohammad May 11 '15 at 02:09
  • Take a look at [this post](http://stackoverflow.com/questions/13033614/how-to-draw-a-crystal-ball-with-two-color-particles-inside) some answers might give you an idea. – Santhan Salai May 11 '15 at 04:31

2 Answers2

1

Easy, sample in spherical coordinates

phi = 2 * Pi * U(0,1)
cos(theta) = 2 * U(0,1) - 1
r          = r_min * U(0,1)^(1/3) (r_max - r_min)

sin(theta) = sqrt(1-cos^2(theta))
x = r * sin(theta) * cos(phi)
y = r * sin(theta) * sin(phi)
z = r * cos(theta)
Severin Pappadeux
  • 16,848
  • 3
  • 34
  • 60
1

If you want a fixed radius then assign them a constant value (instead of a random value) and keep the random numbers for the orientation angles (azimuth/elevation or theta/phi depending on notation).

This code:

rng(0,'twister');
nptSet = 500 ; r1 = 40 ; r2 = 100 ;                 %// your constraints

%// first data set (r1=40)
r1    = zeros(nptSet,1)+r1  ;           %// assign radius (fixed)
azi1  = rand( size(r1) ) * 2*pi ;       %// random azimuth   [  0     2pi]
elev1 = (rand(size(r1)) .* pi)-pi/2 ;   %// random elevation [-pi/2  pi/2]

%// second data set (r2=100)
r2    = zeros(nptSet,1)+r2  ;           %// assign radius (fixed)
azi2  = rand( size(r2) ) * 2*pi ;       %// random azimuth   [  0     2pi]
elev2 = (rand(size(r2)) .* pi)-pi/2 ;   %// random elevation [-pi/2  pi/2]

%// convert to cartesian
[x1,y1,z1] = sph2cart(azi1,elev1,r1);
[x2,y2,z2] = sph2cart(azi2,elev2,r2);

%// display and refine
figure ; hold on
plot3(x1,y1,z1,'or','MarkerSize',2);
plot3(x2,y2,z2,'+b');
xlabel('x') ; ylabel('y') ; zlabel('z')
axis equal ; grid off ; view(50,30)

Will get you that figure: spheredots

Hoki
  • 11,264
  • 1
  • 22
  • 42
  • this is just WRONG. While azimuth is uniform in [0...2 pi], elevation is NOT uniform. You cannot sample angle uniformly and expect points in space to be uniformly distributed. The uniformly distributed thing is cosine of elevation. And elevation itself is typically in [0...pi] range – Severin Pappadeux May 13 '15 at 00:48
  • @SeverinPappadeux, it was a mistake in the comment only, the elevation wasn't in the [-pi pi] range but in the `[-pi/2 pi/2]` range. So a total span of `pi` indeed. I guess that's how Matlab expects it as input for `sph2cart` because if I distribute the elevation on `[0 pi]` I only get the top half dome. – Hoki May 13 '15 at 09:15
  • ok, I see now, yes, total range is \pi, if Matlab prefers it to be from -pi/2 to pi/2 so be it. My real problem with your answer is that it is not uniform in space with elevation sampled as you wrote. Why? Because of a Jacobian. Uniform in space means `density * dx * dy * dx`, so each volume of `dx * dy *dz` has same number of points. If we go to spherical coordinates, we'll get a Jacobian, thus equal to `density * r^2 dr * sin(e) de * da` where `e` is elevation and `a` is azimuth. It is uniform in azimuth `a`, so one can sample it in [0...2\pi] range. It is NOT uniform in `e` due to sin – Severin Pappadeux May 13 '15 at 16:08
  • here is useful link to unit sphere sampling with wrong sampling being uniform in angles http://mathworld.wolfram.com/SpherePointPicking.html – Severin Pappadeux May 28 '15 at 17:42