[R] Package nleqslv ERROR

Santi Burone santiagoburone at gmail.com
Wed Aug 9 22:56:24 CEST 2017


Dear all, 
I am relatively new to R and have had some difficulty in understanding an error i get when running a code to solve a system of non-linear equations, with four equations and two variables. 

This is my code:

ALPHA <- c(-0.0985168033402, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4)
BETA <- c(-0.0985168033402, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4)
GAMMA <- c(0.3940672148378, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4)
MEA <- 30000
MIA <- 10000
MAA <- 50000
DEA <- 0.385
fun <- function(x)  {
  f <- numeric(length(x)) 
  f[1] <-   1*x[1] - ((((MEA*((0.5*((MIA-MEA)^2))^(-BETA[1+j]))*((0.5*((MAA-MEA)^2))^(-ALPHA[1+i]))*((DEA)^(-GAMMA[1+k]))))*((12^0.5)^(-GAMMA[1+k])))/(((0.5)^(-BETA[1+j]-ALPHA[1+i]))*(((1-1*x[2])/(2))^(-BETA[1+j]))*(((-1+1*x[2])/(2))^(-ALPHA[1+i]))*(2*(1*x[2]-1)^(-GAMMA[1+k]))))^(1/(-1-BETA[1+j]-ALPHA[1+i]-1.5*GAMMA[1+k]))
  f[2] <-   1*x[1] - (((MAA*((0.5*((MIA+MAA)^(2)))^(-BETA[1+j]))*((DEA)^(-GAMMA[1+k])))*((12^0.5)^(-GAMMA[1+k])))/((1*x[2])*(2^(-BETA[1+j]-GAMMA[1+k]))*((1*x[2]-1)^(-GAMMA[1+k]))*((((1-1*x[2])^2)/2)^(-BETA[1+j]))))^(1/(1-2*BETA[1+j]-1.5*(GAMMA[1+k])))
  f[3] <-   1*x[1] - (((MIA*((0.5*((MIA+MAA)^(2)))^(-ALPHA[1+i]))*((DEA)^(-GAMMA[1+k])))*((12^0.5)^(-GAMMA[1+k])))/((2-1*x[2])*(2^(-ALPHA[1+i]-GAMMA[1+k]))*((1*x[2]-1)^(-GAMMA[1+k]))*((((-1+1*x[2])^2)/2)^(-ALPHA[1+i]))))^(1/(1-2*ALPHA[1+i]-1.5*(GAMMA[1+k])))
  f[4] <- 1*x[2] > 1
  f 
 
}

Result <- matrix(0,nrow=9*9*9,ncol=6)
startx<-c(16350, 1.33)
indx <- 1
for (i in 1:9) {
  for (j in 1:9) {
    for (k in 1:9) {
      f.startx <- fun(startx)
      if(anyNA(f.startx)) {
        Result[indx,1:3] <- NA
      } else {
        z <- nleqslv(startx,fun)
        Result[indx,1:3] <- c(z$termcd,z$x)
      }
      Result[indx,4:6] <- c(i,j,k)
      indx <- indx+1
    }
  }
}


The error i get when solving for specific values of ALPHA, BETA and GAMMA, not using the loop is:

Error in nleqslv(xstart, fun) : Length of fn result <> length of x!

I had already solved this problem useing ALPHA, BETA and GAMMA as X[1], X[2] and X[3] and being X[1] and X[2] of this system  given values, in that case for the first values of alpha beta and gamma given here the solution was (16350, 1.33).
I dont understand what’s the error i get here, as i know the system as a unique solution. "What’s the meaning of Length of fn result <> length of x!"?

Thanks in advance! 
Santiago. 
	[[alternative HTML version deleted]]



More information about the R-help mailing list