how to plot 3d inequalities on matlab

前端 未结 3 1702
情书的邮戳
情书的邮戳 2020-12-19 18:19

I want to plot a 3d region in MATLAB bounded from a set of inequalities.

For example:

0 <= x <= 1

sqrt(x) <= y <= 1

0 <= z <= 1          


        
相关标签:
3条回答
  • 2020-12-19 18:45

    I've been trying to figure out the same thing, and the trick is to make the size of everything not in the intersection 0. Tobold's scatter3 line uses '3' as the option for size, meaning all points will show up as point 3. This can be substituted for a matrix of equal size to X1 with the set of sizes. The easiest way to do this is just make s = 3*all:

    all = ineq1 & ineq2 & ineq3;
    colors = zeros(size(X))+all;
    sizes = 3 * all;
    scatter3(X1(:),Y1(:),Z1(:),sizes,colors(:)','filled')
    

    That should get you just the area in the intersection.

    -- edit: The color variable needs to change too. You just want the intersection, not the other inequalities.

    0 讨论(0)
  • 2020-12-19 18:59

    You can do almost the same thing as in the 2d case that you linked to. Just write down your three inequalities, use a 3d meshgrid, multiply each inequality with a number from a set of three numbers that has unique subset sums (e.g. 2, 4, 8) and use scatter3:

    [X,Y,Z]=meshgrid(0:0.1:1,0:0.1:1,0:0.1:1); % Make a grid of points between 0 and 1
    p1=0.1; p2=0.2; % Choose some parameters
    ineq1 = (X >= 0 & X <= 1) * 2;
    ineq2 = (X >= sqrt(X) & Y <= 1) * 4;
    ineq3 = (Z >= 0 & Z <= 1 - Y) * 8;
    colors = zeros(size(X))+ineq1+ineq2+ineq3;
    scatter3(X(:),Y(:),Z(:),3,colors(:),'filled')
    
    0 讨论(0)
  • 2020-12-19 19:04

    I don't understand several things in the code that you wrote as modification of @Tobold's help. For example what are the parameters p1 and p2 doing in your code?

    Anyway, The code plotting only the points of your grid satisfying all of your inequalities;

    [X,Y,Z]=meshgrid(0:0.1:1,0:0.1:1,0:0.1:1);
    ineq1 = (X >= 0 & X <= 1);
    ineq2 = (Y >= sqrt(X) & Y <= 1);
    ineq3 = (Z >= 0 & Z <= 1 - Y);
    all = ineq1 & ineq2 & ineq3;
    scatter3(X(all),Y(all),Z(all),'b','filled')
    

    The result is brought in the following image.

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