# Re: [R] Package nleqslv ERROR

```Santi,
```
```
In the second line of your function you have the following:
f <- numeric(length(x))

This sets the length of this numeric vector (i.e., "f") to the length of the
vector "x".
Later, inside the function you assign to values to 4 elements of the vector "f".
This assumes that "f" is at least 4 element in length.

However, you define "startx" to be a numeric vector of length 2 with
startx <- c(16350, 1.33)
which you then use as the argument to the function "fun" (bad name for a
function, by the way, as it is not descriptive).

Thus, when "x" inside your function gets the value of "startx" it becomes a
numeric vector of length 2, which is then used to set the length of the numeric
vector "f". As soon as the function tries to assign a value to f[3], R
correctly throws an informative error.

Mark
R. Mark Sharp, Ph.D.
msh...@txbiomed.org

> On Aug 9, 2017, at 3:56 PM, Santi Burone <santiagobur...@gmail.com> wrote:
>
> 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!"?
>
> Santiago.
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> and provide commented, minimal, self-contained, reproducible code.

CONFIDENTIALITY NOTICE: This e-mail and any files and/or attachments
transmitted, may contain privileged and confidential information and is
intended solely for the exclusive use of the individual or entity to whom it is
addressed. If you are not the intended recipient, you are hereby notified that
any review, dissemination, distribution or copying of this e-mail and/or
attachments is strictly prohibited. If you have received this e-mail in error,
please immediately notify the sender stating that this transmission was
misdirected; return the e-mail to sender; destroy all paper copies and delete
all electronic copies from your system without disclosing its contents.
______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help