Plotting the result of a 2 parameter function in matlab (3D Graph)

Plotting the result of a 2 parameter function in matlab (3D Graph)

Keep in mind, that with matlab, youre never really plotting functions; Youre plotting arrays/vectors. So instead of trying to plot g = f(X,Y), youll actually by plotting the vectors X, Y, and g, where X and Y are your original inputs and g is a vector containing your outputs.

Im having a hard time visualizing what exactly youre trying to plot but basically, you can follow any standard matlab plotting example such as: http://web.cecs.pdx.edu/~gerry/MATLAB/plotting/plotting.html

It does not produce a 3D plot, but I have found the 2D scatter plot useful for this kind of task before:

scatter(x, y, 5, z)

Where z is the value of the function at the point (x, y) will produce something similar to what you want. Its perhaps not quite as pretty as a full 3D plot but can be used to good effect.

See:

http://www.mathworks.com/matlabcentral/fileexchange/35287-matlab-plot-gallery-scatter-plot-2d/content/html/Scatter_Plot_2D.html

Here is some (very ugly) code I put together to demonstrate the difference:

j=1;
y = -100:1:100;
for i = -100:1:100
    y = [y -100:1:100];
    count = 0;
    while count < 202;
        x(j) = i;
        j = j+1;
        count = count + 1;
    end
end

z = (abs(x) + abs(y));

figure(1)
scatter(x, y, 10, z)
h=colorbar;

figure(2)
ezsurf((abs(x) + abs(y)))

Plotting the result of a 2 parameter function in matlab (3D Graph)

Well, this is what I was going for : http://www.mathworks.com/help/matlab/ref/ezsurf.html

if i do this

ezsurf(f(x,y));

I get the 3d graph I wanted.

Thanks anyways!

Leave a Reply

Your email address will not be published.