如何使用嵌套For循环的函数在单个图表中绘制多条线?

时间:2019-04-30 04:04:34

标签: r for-loop

我想使用嵌套的for循环在单个图表中绘制多条线

h <- c(1,20,50,100,200,400)
d <- seq(20, 420, by=20)

我想为h和d的不同值计算体重w

# the formula is w =1/(1+(d^2/h^2))

for (i in 1:h) {
  for(j in 1:d) { 
    w <- 1/((1+j^2)/i^2)
    }
  }

我不知道该怎么做

我想要一个图,其中d为x轴,w为y轴

1 个答案:

答案 0 :(得分:1)

我们可以使用outer

m1 <- outer(h, d, FUN = function(x, y) 1/((1 + y^2)/x^2))
matplot(t(m1), type = "l")

enter image description here