Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Generating equidistributed points on the surface of a sphere ?? (error)

Subject: Generating equidistributed points on the surface of a sphere ?? (error)

From: Animesh Pandey

Date: 10 Jul, 2011 18:44:09

Message: 1 of 2

r = 1;

phi = linspace (0, pi, 30);
theta = linspace (0, 2*pi, 30);
[phi, theta] = meshgrid(phi, theta);

x = r.*cos(theta).*sin(phi);
y = r.*sin(theta).*sin(phi);
z = r.*cos(phi);
% This Plots the sphere !
mhndl = mesh(x, y, z);

set(mhndl, ...
    'EdgeColor', [0.6,0.6,0.6], ...
    'EdgeAlpha', 0.5, ...
    'FaceAlpha', 0.5);

axis equal
axis off

N = 10;
a = (2*r)/N;

j = -r:a:r;
k = 0:(2*pi)/N:2*pi;

for i = 0:1:N;
    x = sqrt(r*r-j(i)*j(i+1))*cos(k(i+1));
    y = sqrt(r*r-j(i)*j(i+1))*sin(k(i+1));
end

hold on
plot3(x, y, j,'*');

-------------------------------------------------------------------------------------------------------------
I have to implement this algorithm to evenly plot N number of points on a sphere !

repeat N times {
Choose 'z' equidistributed from [-r:r]?-r----eeedfndr
Choose 'phi' equidistributed from [0:2*Pi]
Set x = (sqrt(r*r - z*z))* cos(phi);
Set y = (sqrt(r*r - z*z))* sin(phi);
}

I get this error :
"??? Subscript indices must either be real positive integers or logicals. "

The sphere plot is correct, this error is in the for loop !

Please Help !

Subject: Generating equidistributed points on the surface of a sphere ?? (error)

From: Roger Stafford

Date: 10 Jul, 2011 19:46:10

Message: 2 of 2

"Animesh Pandey" wrote in message <ivcrtp$5c2$1@newscl01ah.mathworks.com>...
> ........
> N = 10;
> a = (2*r)/N;
> j = -r:a:r;
> k = 0:(2*pi)/N:2*pi;
> for i = 0:1:N;
> x = sqrt(r*r-j(i)*j(i+1))*cos(k(i+1));
> y = sqrt(r*r-j(i)*j(i+1))*sin(k(i+1));
> end
> hold on
> plot3(x, y, j,'*');
>
> I have to implement this algorithm to evenly plot N number of points on a sphere !
> .......
> I get this error :
> "??? Subscript indices must either be real positive integers or logicals. "
- - - - - - - - - - -
  The error message is telling you the truth, Animesh. You cannot use fractional values to index your vectors j and k. You must revise your code accordingly.

  Also your assignment to x and y is incorrect. As it stands only the last values obtained in the for loop will be placed in x and y, and these will therefore only be scalar quantities. All previous values will be overwritten. You must learn to index such variable assignments if you wish to preserve the values in them.

  To avoid the for loop altogether you can use linspace as you did earlier:

 phi = linspace(0,2*pi,N+1);
 z = linspace(-r,r,N+1);
 x = sqrt((r^2-z.^2).*cos(phi);
 y = sqrt((r^2-z.^2).*sin(phi);
 plot3(x,y,z)

  By the way, these points you are placing in plot3 will not be equally-spaced. To do so would require much more complicated computations than this.

Roger Stafford

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us