Search code examples
matlabmatlab-figure

How to create the figure in MatLab?


I got that I should use function slice or is it not so? Simply, I don't get how is V used if I have function for z?

enter image description here


Solution

  • I do not think there is a built-in function to draw such a plot, and the slice function is definitely not for that. However, you can achieve the desired result by drawing several separate patches using patch:

    plots one or more filled polygonal regions using the elements of X and Y as the coordinates for each vertex. patch connects the vertices in the order that you specify them. To create one polygon, specify X and Y as vectors. To create multiple polygons, specify X and Y (and Z) as matrices where each column corresponds to a polygon.

    nS = 19; % number of slices
    ygv = linspace(-2, 2, nS); % position of slices on Y axis
    xgv = linspace(-2, 2, 100); % position of points on each curve
    [X, Y] = meshgrid(xgv, ygv);
    Z = X.^2+Y.^2;
    % adding two points with z=0 at both ends of each curve
    X = [X(:, 1), X, X(:, end)];
    Y = [Y(:, 1), Y, Y(:, end)];
    Z = [zeros(nS, 1), Z, zeros(nS, 1)];
    patch('Xdata', X', 'Ydata', Y', 'Zdata', Z', 'facecolor', [1 1 1]*0.9);
    xlabel('x'), ylabel('y'), zlabel('z')
    title('z=x^2+y^2')
    grid on
    view(3)
    

    enter image description here