Plot square surface in Matlab

后端 未结 3 1104
天涯浪人
天涯浪人 2021-01-21 20:56

How to plot a square surface in Matlab?

More exactly I want to plot a square square with value 0.5 surface which is located at X:-1 to X=1 and Y:2.5 to 3.5.

相关标签:
3条回答
  • 2021-01-21 21:18

    I ended up with

    figure;
    hold on;
    
    X = [ -2  -2   2 2];
    Y = [2 4 4 2];
    Z = [0 0 0 0];
    patch(X,Y,Z,'blue');
    
    X = [ -1  -1   1   1];
    Y = [3.5 2.5 2.5 3.5];
    Z = [0.5 0.5 0.5 0.5];
    h = patch(X,Y,Z,'red');
    
    X = [ -1 -1 1 1];
    Y = [2.5 2.5 2.5 2.5];
    Z = [0 0.5 0.5 0];
    patch(X,Y,Z,'red');
    
    X = [1, 1, 1, 1];
    Y = [2.5 2.5 3.5 3.5];
    Z = [0 0.5 0.5 0];
    patch(X,Y,Z,'red');
    
    view(45,30)
    legend(h, 'F(u,v)')
    xlabel('u')
    ylabel('v')
    zlabel('F(u,v)')
    
    0 讨论(0)
  • 2021-01-21 21:27

    You need to provide multiple Z-values together with the same X, Y values. A small example:

    >> [X, Y]= meshgrid([1,2,2,3,4], 1:2)
    X =
         1     2     2     3     4
         1     2     2     3     4
    Y =
         1     1     1     1     1
         2     2     2     2     2
    >> Z = [0,0,1,1,0;0,0,1,1,0]
    Z =
         0     0     1     1     0
         0     0     1     1     0
    >> surf(X, Y, Z)
    

    Yields this:

    Step at X=2, slope at X=3..4

    This should be the same in 2D, you just need to wrap you head around which X and Y values to duplicate and adjust the Z-Matrix accordingly.

    0 讨论(0)
  • 2021-01-21 21:30

    This is what the patch function is for.

    Matlab documentation

    so for your case:

    X = [ -1  -1   1   1];
    Y = [3.5 2.5 2.5 3.5];
    Z = [0.5 0.5 0.5 0.5];
    
    patch(X,Y,Z,'red')
    view(45,45)
    

    example

    0 讨论(0)
提交回复
热议问题