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 RegionPlot3D, but the results are visually poor and too polygon-heavy to rotate in real time
Here’s what I mean, the code below generates 10 sets of linear constraints and plots them
randomCons := Module[{},
hadamard = KroneckerProduct @@ Table[{{1, 1}, {1, -1}}, {3}];
invHad = Inverse[hadamard];
vs = Range[8];
m = mm /@ vs;
sectionAnchors = Subsets[vs, {1, 7}];
randomSection :=
Mean[hadamard[[#]] & /@ #] & /@
Prepend[RandomChoice[sectionAnchors, 3], vs]; {p0, p1, p2, p3} =
randomSection;
section =
Thread[m ->
p0 + {x, y, z}.Orthogonalize[{p1 - p0, p2 - p0, p3 - p0}]];
And @@ Thread[invHad.m >= 0 /. section]
];
Table[RegionPlot3D @@ {randomCons, {x, -3, 3}, {y, -3, 3}, {z, -3,
3}}, {10}]
Any suggestions?
Update: Incorporating suggestions below, here’s the version I ended up using to plot feasible region of a system of linear inequalities
(* Plots feasible region of a linear program in 3 variables, \
specified as cons[[1]]>=0,cons[[2]]>=0,...
Each element of cons must \
be an expression of variables x,y,z only *)
plotFeasible3D[cons_] :=
Module[{maxVerts = 20, vcons, vertCons, polyCons},
(* find intersections of all triples of planes and get rid of \
intersections that aren't points *)
vcons = Thread[# == 0] & /@ Subsets[cons, {3}];
vcons = Select[vcons, Length[Reduce[#]] == 3 &];
(* Combine vertex constraints with inequality constraints and find \
up to maxVerts feasible points *)
vertCons = Or @@ (And @@@ vcons);
polyCons = And @@ Thread[cons >= 0];
verts = {x, y, z} /.
FindInstance[polyCons && vertCons, {x, y, z}, maxVerts];
ComputationalGeometry`Methods`ConvexHull3D[verts,
Graphics`Mesh`FlatFaces -> False]
]
Code for testing
randomCons := Module[{},
hadamard = KroneckerProduct @@ Table[{{1, 1}, {1, -1}}, {3}];
invHad = Inverse[hadamard];
vs = Range[8];
m = mm /@ vs;
sectionAnchors = Subsets[vs, {1, 7}];
randomSection :=
Mean[hadamard[[#]] & /@ #] & /@
Prepend[RandomChoice[sectionAnchors, 3], vs]; {p0, p1, p2, p3} =
randomSection;
section =
Thread[m ->
p0 + {x, y, z}.Orthogonalize[{p1 - p0, p2 - p0, p3 - p0}]];
And @@ Thread[invHad.m >= 0 /. section]
];
Table[plotFeasible3D[List @@ randomCons[[All, 1]]], {50}];
Here is a small program that seems to do what you want:
The result is:
Downside: the undocumented function is not perfect. When the face is not a triangle, it will show a triangulation:
Edit
There is an option to get rid of the foul triangulation
does the magic. Sample:
Edit 2
As I mentioned in a comment, here are two sets of degenerate vertex generated by your randomCons
and
Still trying to see how to cope gently with those …
Edit 3
This code is not general enough for the full problem, but eliminates the cylinder degenerancy problem for your sample data generator. I used the fact that the pathogenic cases are always cylinders with their axis paralell to one of the coordinate axis, and then used RegionPlot3D to plot them.
I’m not sure if this will be useful for your general case :(.
Here you can find an image of the generated output, the degenerated cases (all cylinders) are in transparent yellow
HTH!