Lime
Lime

Reputation: 756

Using metafunctions to iterate many values

I'm trying to imitate this arithmetic expression using metafunctions: enter image description here

Essentially, I want random real numbers from -1<0<1 for x and theta whilst y takes any positve or negative value from -20<0<20.

This is what I have tried:

am_thirteen <-function(x, y, theta){
  
  x1=runif(x, 0, 1)
  y2=runif(y, 0, 20)
  theta3=runif(theta, 0, 1)
  lapply(
    x1, y2, theta3, 
    FUN = function(x1, y2, theta3){
  (-(1/theta3)*log10(1+(((exp(-theta3*x1)-1)*(exp(-theta3*y2)-1))/(exp(-theta3)-1))))

      })
}

then:

lapply(c(10, 10, 10), am_thirteen)

error:

Error in runif(y, 0, 20) : argument "y" is missing, with no default

I expect the output to be like this when I call the function likeso:

am_thirteen <-function(x, y, theta){
     (-(1/theta)*log10(1+(((exp(-theta*x)-1)*(exp(-theta*y)-1))/(exp(-theta)-1))))
 }

am_thirteen(runif(30, -1, 1),runif(30, -1, 1) ,runif(30, -1, 1) )
 [1] -0.1171828665 -0.1084171900 -0.2441978216  0.0256141946  0.0131042671  0.0844813069 -0.2898303502 -0.0051398158  0.0211143988  0.0667284968  0.0956009788 -0.2560234695
[13]  0.0039000563  0.0438593991 -0.0405444828 -0.1644587072 -0.0740161357  0.0652280253  0.0009757007  0.1854526942 -0.0905563222  0.0223378333 -0.0274033047 -0.2896201555
[25] -0.0173795526 -0.1840047563           NaN -0.1889213392 -0.0772152240 -0.0233664868

Upvotes: 0

Views: 46

Answers (1)

ThomasIsCoding
ThomasIsCoding

Reputation: 102241

You can simply do like

am_thirteen <- function(x, y, theta) {
  x1 <- runif(x, 0, 1)
  y2 <- runif(y, 0, 20)
  theta3 <- runif(theta, 0, 1)
  -(1 / theta3) * log10(1 + (((exp(-theta3 * x1) - 1) * (exp(-theta3 * y2) - 1)) / (exp(-theta3) - 1)))
}

such that

> am_thirteen(30, 30, 30)
 [1] 0.24717749 1.05846557 0.45755718 0.66018800 0.03834795 0.39526527
 [7] 1.99589251 3.43298477 0.56534793 1.01813706 0.49402778 0.53281734
[13] 1.15933195 2.30322906 0.12077730 0.06233150 0.05681387 0.32175138
[19] 1.39598926 0.63679915 0.77333056 0.03321481 0.31202521 0.83615194
[25] 0.64084402 0.69740361 0.61896551 2.28605706 0.92365230 0.21639357

Upvotes: 1

Related Questions