2
votes

I am trying to produce a contour plot for the 3D vectors returned by a custom function in the xy plane where z = 0.

I tried this but I just get an empty graph:

% Stand in for the real function I want to plot.
f = @(x, y, z) [x ^ 2, y ^ 2, x * y + z];

x = linspace(-5, 5, 50);
y = linspace(-5, 5, 50);
z = zeros(length(x), length(y), 3);

% I know this can be vectorized but the function I really want to graph can't
% be.
for i = 1:length(x)
    for j = 1:length(y)
        z(i, j, :) = f(x(i), y(j), 0);
    end
end

figure;
axis equal;
contour(x, y, z);
1

1 Answers

1
votes

You should mention what your axis will be. You have x,y and 3 outputs from f.

If you consider 3 outputs of your f as the ones to be plotted then you should use,

contour(z(:,:,1),z(:,:,2),z(:,:,3));

Which will give you this,

enter image description here

I think what you are looking for, is a function with one output, like,

f = @(x, y,z) [x ^ 2 + y ^ 2 + x * y + z ];
x = linspace(-5, 5, 50);
y = linspace(-5, 5, 50);
z = zeros(length(x), length(y));
for i = 1:length(x)
    for j = 1:length(y)
       z(i, j) = f(x(i), y(j),0);
    end
end
contour(x,y,z,20);

enter image description here