Vasker Sharma
Vasker Sharma

Reputation: 187

How to plot multiple lines in a single chart with a function using nested For loop?

I want to plot the multiple lines in a single chart using the nested for loop

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

I want to calculate weight, w for different values of h and d

# 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)
    }
  }

I could not figure out how to do it

I want a graph with d as x axis and w as y axis

Upvotes: 0

Views: 74

Answers (1)

akrun
akrun

Reputation: 887048

We can use outer

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

enter image description here

Upvotes: 1

Related Questions