Nagsaver
Nagsaver

Reputation: 81

Plotting a 3D solid sphere in matlab

I want to plot a solid sphere in matlab and hence i trid the following code

for radius = 0:0.1:10
theta = linspace(0,2*pi);
phi = linspace(0,pi/2);
[theta,phi] = meshgrid(theta,phi);
[xs,ys,zs] = sph2cart(theta,phi,radius);
surf(xs,ys,zs);
end

But I still only get the surface of the outer most sphere.Any help is appreciated.

Upvotes: 0

Views: 9271

Answers (2)

ANon
ANon

Reputation: 1

One quicker way that worked for me is:

phi = linspace(0, 2*pi);

Upvotes: 0

Dennis Jaheruddin
Dennis Jaheruddin

Reputation: 21563

As can be made out from the comments, the code to plot a solid half sphere would be:

hold on
for radius = 0:0.1:10
theta = linspace(0,2*pi);
phi = linspace(0,pi/2);
[theta,phi] = meshgrid(theta,phi);
[xs,ys,zs] = sph2cart(theta,phi,radius);
surf(xs,ys,zs);
end

For a full sphere one would also be interested in

surf(xs,ys,-zs);

Upvotes: 1

Related Questions