MATLAB: Surface of a equation

equationmeshplotsurface

Hi.
I want to plot the quadratic surface of a sphere:
x^2 + y^2 + z^2 = r , where r is equal to 1. Therefore:
x^2 + y^2 + z^2 = 1, where x, y and z are values between -1.5 and 1.5
Can anyone explain me how to do this? I've looked into mesh surfaces but I can only plot functions ( f(x,y) )..
Any help will be highly appreciated. Thanks.

Best Answer

I think you meant to square r in your equation, and you cannot have a value between -1.5 and 1.5 if the radius is 1. The radius has to be 1.5. Think about what happens if x=0,y=0,z=1.5 as you stated must be a point that satisfies the equation x^2+y^2+z^2 = r^2
You should probably do it with spherical coordinates:
n = 100;
r = 1.5;
theta = (-n:2:n)/n*pi;
phi = (-n:2:n)'/n*pi/2;
cosphi = cos(phi); cosphi(1) = 0; cosphi(n+1) = 0;
sintheta = sin(theta); sintheta(1) = 0; sintheta(n+1) = 0;
x = r*cosphi*cos(theta);
y = r*cosphi*sintheta;
z = r*sin(phi)*ones(1,n+1);
surf(x,y,z)
xlabel('X'); ylabel('Y'); zlabel('Z')
Note that MATLAB has a function for this with a unit sphere, sphere.m