-1

As we can make contour plot of f=(x.^2) + (y.^2); in 2-D as follows:

[x,y]= meshgrid(-10:10, -10:10);
contour(x,y, (x.^2)+(y.^2));

and we can make contour plot of f=(x.^2) + (y.^2); in 3-D using contour3

Can we make contour plot of f=(x.^2) + (y.^2) + (z.^2); in 3-D

Sardar Usama
  • 19,536
  • 9
  • 36
  • 58
  • 1
    [`contour3`](http://www.mathworks.com/help/matlab/ref/contour3.html) – sco1 Mar 29 '16 at 18:39
  • [`scatter3`](http://www.mathworks.com/help/matlab/ref/scatter3.html#btr5_il-1) where your color is `f` – sco1 Mar 29 '16 at 19:02
  • @excaza ! `scatter3` is useful after the contours are made! `contour3` can be used for contour plot of `f=(x.^2) + (y.^2);` , not when f is a function of 3 variables! or can you elaborate it with an example? – Sardar Usama Mar 29 '16 at 19:09
  • 2
    you are talking about `isosurface` here – Ander Biguri Mar 29 '16 at 21:49

1 Answers1

3

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.

Method 1: Using isosurface

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:

Contours for f(x,y,z) = x^2 + y^2 + z^2 with isosurface

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:

Contours for f(x,y,z) = x^2 + y^2 + z^2 with surf


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.

codeaviator
  • 2,545
  • 17
  • 42