R中二次方程的等高线图

时间:2015-07-09 20:50:56

标签: r data-visualization

我正在尝试使用R重新创建下图所示的等高线图。 它是恒定糖浆损失随速度和压力变化的等高线图。

Contours of constant syrup loss as a function of speed and pressure

此示例使用的数据:

speed      = c(100,100,100,100,100,100,120,120,120,120,120,120,140,140,140,140,140,140)
pressure   = c(10,10,15,15,20,20,10,10,15,15,20,20,10,10,15,15,20,20)
syrup_loss = c(-35,-25,110,75,4,5,-45,-60,-10,30,-40,-30,-40,15,80,54,31,36)

利用该数据创建了二次方程式:

model <- lm(syrup_loss~speed + pressure + speed^2 + pressure^2 + speed*pressure)
summary(model)

给出了:

z = 1217.30556 - 31.25625 * x + 86.01667 * y + 0.1291 * x ^ 2 - 2.87333 * y ^ 2 + 0.02875 * x * y

我尝试使用此代码创建等高线图,但它没有给出可接受的结果:

x = seq(100, 140, len=100)
y = seq( 10,  20, len=100)
z = outer(x, 1217.30556 - 31.25625*x + 86.01667*y + 0.1291*x^2 - 
             2.87333*y^2 + 0.02875*x*y)
contour(x, y, z, nlev=12)

1 个答案:

答案 0 :(得分:2)

outer函数中,缺少Y参数。代码

z <- outer(x, y, function(x, y) 1217.30556 - 31.25625*x + 86.01667*y + 0.1291*x^2  - 2.87333*y^2 + 0.02875*x*y) 

修复了问题。