我有3个变量的线性不等式系统,我想绘制这些区域。理想情况下,我希望看起来像PolyhedronData中的对象。我试过RegionPlot3D,但结果是视觉差,过于多边形重实时旋转在Mathematica中绘制线性不等式
这里就是我的意思是,下面的代码生成10套线性约束,并绘制他们
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}]
任何建议?
更新:结合下面的建议,这里的版本,我结束了使用绘制线性不等式
(* 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] ]
代码的系统的可行域测试
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}];
谢谢,看起来非常好!我想我有一个想法如何摆脱脸上多余的线条,将更新我的帖子 – 2010-10-06 03:46:11
@Yaroslav发现它。那里有一个选项。查看编辑 – 2010-10-06 04:39:28
@Yaroslav退化的解决方案对你来说是一个问题吗?你的eq。系统有时会生成无限的柱面(我没有检查是否也有飞机和孤立点) – 2010-10-06 06:26:22