我想绘制一个多面体,由以下不等式描述:
3*x+5*y+9*z<=500
4*x+5*z<=350
2*y+3*z<=150
x,y,z>=0
Run Code Online (Sandbox Code Playgroud)
这是一个线性程序.目标函数是:
4*x+3*y+6*z
Run Code Online (Sandbox Code Playgroud)
多面体是该程序的可行区域.我可以将不等式绘制为平面,这应该描述多面体(请注意,这是我第一次尝试使用rgl,因此代码有点混乱.如果你想改进它,请随意这样做):
# setup
x <- seq(0,9,length=20)*seq(0,9,length=20)
y <- x
t <- x
f1 <- function(x,y){y=70-0.8*x}
z1 <- outer(x,y,f1)
f2 <- function(x,y){500/9-x/3-(5*y)/9}
z2 <- outer(x,y,f2)
f3 <- function(x,y){t=50-(2*y)/3}
z3 <- outer(x,y,f3)
# plot planes with rgl
uM = matrix(c(0.72428817, 0.03278469, -0.68134511, 0,
-0.6786808, 0.0555667, -0.7267077, 0,
0.01567543, 0.99948466, 0.05903265, 0,
0, 0, 0, 1),
4, 4)
library(rgl)
open3d(userMatrix = uM, windowRect = c(0, 0, 400, 400))
rgl.pop("lights")
light3d(diffuse='white',theta=0,phi=20)
light3d(diffuse="gray10", specular="gray25")
rgl.light(theta = 0, phi …Run Code Online (Sandbox Code Playgroud) scatterplot3d在 R 中使用,我试图从观察到回归平面绘制红线:
wh <- iris$Species != "setosa"
x <- iris$Sepal.Width[wh]
y <- iris$Sepal.Length[wh]
z <- iris$Petal.Width[wh]
df <- data.frame(x, y, z)
LM <- lm(y ~ x + z, df)
library(scatterplot3d)
G <- scatterplot3d(x, z, y, highlight.3d = FALSE, type = "p")
G$plane3d(LM, draw_polygon = TRUE, draw_lines = FALSE)
Run Code Online (Sandbox Code Playgroud)
要获得下图的 3D 等效项:
在 2D 中,我可以使用segments:
pred <- predict(model)
segments(x, y, x, pred, col = 2)
Run Code Online (Sandbox Code Playgroud)
但是在 3D 中,我对坐标感到困惑。