Contour plot for quadratic equation in R

白昼怎懂夜的黑 提交于 2019-12-20 05:33:09

问题


I'm trying to recreate contour plot shown in the picture below using R. It is a contour plot of constant syrup loss as a function of speed and pressure.

Data used for this example:

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)

With that data quadratic equation was created:

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

Which gives:

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

I tried creating contour plot using this code, but it doesn't give acceptable result:

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:


In the outer function, the Y argument is missing. The code

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) 

fixed the problem.



来源:https://stackoverflow.com/questions/31328184/contour-plot-for-quadratic-equation-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!