Plotting linear inequalities in Mathematica

后端 未结 3 468
谎友^
谎友^ 2021-01-06 10:17

I have linear systems of inequalities in 3 variables and I\'d like to plot these regions. Ideally, I\'d like something that looks like objects in PolyhedronData. I tried Reg

3条回答
  •  不要未来只要你来
    2021-01-06 10:42

    A triplet chosen from your set of inequalities will generally determine a point obtained by solving the corresponding triplet of equations. I believe that you want the convex hull of this set of points. You can generate this like so.

    cons = randomCons;  (* Your function *)
    eqs = Apply[Equal, List @@@ Subsets[cons, {3}], {2}];
    sols = Flatten[{x, y, z} /. Table[Solve[eq, {x, y, z}], {eq, eqs}], 1];
    pts = Select[sols, And @@ (NumericQ /@ #) &];
    ComputationalGeometry`Methods`ConvexHull3D[pts]
    

    Of course, some triplets might actually be underdetermined and lead to lines or evan a whole plane. Thus the code will issue a complaint in those cases.

    This appeared to work in the few random cases that I tried but, as Yaro points out, it doesn't work in all. The following picture will illustrate exactly why:

    {p0, p1, p2, 
       p3} = {{1, 0, 0, 0, 0, 0, 0, 0}, {1, 1/2, -(1/2), 0, -(1/2), 0, 
        0, -(1/2)}, {1, 0, 1/2, 1/2, 0, 0, -(1/2), 1/2}, {1, -(1/2), 1/2, 
        0, -(1/2), 0, 0, -(1/2)}};
    hadamard = KroneckerProduct @@ Table[{{1, 1}, {1, -1}}, {3}];
    invHad = Inverse[hadamard];
    vs = Range[8];
    m = mm /@ vs;
    section = 
      Thread[m -> 
        p0 + {x, y, z}.Orthogonalize[{p1 - p0, p2 - p0, p3 - p0}]];
    cons = And @@ Thread[invHad.m >= 0 /. section];
    eqs = Apply[Equal, List @@@ Subsets[cons, {3}], {2}];
    sols = Flatten[{x, y, z} /. Table[Solve[eq, {x, y, z}], {eq, eqs}], 
        1]; // Quiet
    pts = Select[sols, And @@ (NumericQ /@ #) &];
    ptPic = Graphics3D[{PointSize[Large], Point[pts]}];
    regionPic = 
      RegionPlot3D[cons, {x, -2, 2}, {y, -2, 2}, {z, -2, 2}, 
       PlotPoints -> 40];
    Show[{regionPic, ptPic}]
    

    Thus, there are points that are ultimately cut off by the plane defined by some other constraint. Here's one (I'm sure terribly inefficient) way to find the ones you want.

    regionPts = regionPic[[1, 1]];
    nf = Nearest[regionPts];
    trimmedPts = Select[pts, Norm[# - nf[#][[1]]] < 0.2 &];
    trimmedPtPic = Graphics3D[{PointSize[Large], Point[trimmedPts]}];
    Show[{regionPic, trimmedPtPic}]
    

    Thus, you could use the convex hull of trimmedPts. This ultimately depends on the result of RegionPlot and you might need to ramp of the value of PlotPoints to make it more reliable.

    Googling about a bit reveals the concept of a feasibility region in linear programming. This seems to be exactly what you're after.

    Mark

提交回复
热议问题