The matlab function isosurface
can do what you are asking. However you can also achieve the results you want using other alternatives, like using surf
. I will cover both methods.
We need to create the domain for x
, y
and z
and then generate a 3D mesh with those values so that we can evaluate the function f(x,y,z) = x^2 + y^2 + z^2
. Then, we shall give a value to the constant k
and feed all this information to isosurface
, so that we can obtain the family of (x,y,z)
values that satisfy the condition: f(x,y,z) = k
. Note that this contours are in fact spheres! Finally we can use patch
to generate a surface with those values.
It is very interesting to give different values for k
iterativelly and see the contours associated with those values.
% Value for x, y and z domain.
a = 10;
% Domain for x ,y and z.
x = linspace(-a,a);
y = linspace(-a,a);
z = linspace(-a,a);
% Generate a 3D mesh with x, y and z.
[x,y,z] = meshgrid(x,y,z);
% Evaluate function (3D volume of data).
f = x.^2 + y.^2 + z.^2;
% Do contours from k = 0 to k = 100 in steps of 1 unit.
for k = 0:100
% Draw the contour that matches k.
p = patch(isosurface(x,y,z,f,k));
isonormals(x,y,z,f,p)
p.FaceColor = 'red';
p.EdgeColor = 'none';
% Adjust figure properties.
title(sprintf('Contours of f(x,y,z) = x^2 + y^2 + z^2\nwith f(x,y,z) = k = %d',k));
xlabel('x-axis');
ylabel('y-axis');
zlabel('z-axis');
axis equal;
grid on;
box on;
axis([-10 10 -10 10 -10 10]);
camlight left;
lighting phong;
% Update figure.
drawnow;
% Clear axes.
cla;
end
This is the output:

Method 2: Using surf
As in the previous method, to contour the function f(x,y,z) = x^2 + y^2 + z^2
, we need to match the function to a constant value, this is f(x,y,z) = k
, where k
is any constant value you choose.
If we isolate z
in terms of k
, x
and y
we have: z = ± sqrt(k-x.^2-y.^2)
, so we have the explicit values for x
, y
and z
. Now, let's give different values for k
iterativelly and see the results that we get with the surf
function!
% Do contours from k = 0 to k = 100 in steps of 1 unit.
for k = 0:100
% Find the value where: k - x^2 - y^2 = 0
a = sqrt(k);
% Domain for x and y.
x = linspace(-a,a);
y = linspace(-a,a);
[x,y] = meshgrid(x, y);
% Isolate z in terms of k, x and y.
z = sqrt(k-x.^2-y.^2);
% Find complex entries in z.
i = find(real(z)~=z);
% Replace complex entries with NaN.
z(i) = NaN;
% Draw upper hemisphere of surface.
surf(x,y,z,'FaceColor','red','EdgeColor','none');
hold on;
% Draw lower hemisphere of surface.
surf(x,y,-z,'FaceColor','red','EdgeColor','none');
% Adjust figure properties.
title(sprintf('Contours of f(x,y,z) = x^2 + y^2 + z^2\nwith f(x,y,z) = k = %d',k));
xlabel('x-axis');
ylabel('y-axis');
zlabel('z-axis');
axis equal;
grid on;
box on;
axis([-10 10 -10 10 -10 10]);
camlight left;
lighting phong;
% Update figure.
drawnow;
hold off;
end
This is the output:

References
I took some of the ideas from David Arnold's article "Complex Numbers and Plotting in Matlab", which is well worth a read and will help you understand how to plot functions that generate complex numbers.