简体   繁体   中英

Solving non linear equations in R using BB package

I was trying to solve a set of non linear equations but stuck.

0<x1<1 ; x2>0 ; x3>0

library(BB) 

f <- function(x) { 
  x1 <- x[1] 
  x2 <- x[2]
  x3 <- x[3] 

  F <- rep(NA, 3) 

  F[1] <- 1-x1^(log(1+1/(x3)^x2))-0.64

  F[2] <- x1^(log(1+1/(x3)^x2))-x1^(log(1+(2/x3)^x2))-0.17

  F[3] <- x1^(log(1+(2/x3)^x2))-x1^(log(1+(3/x3)^x2))-0.10

  return(F) 
} 

p0 <- c(0.5,2,1) 

dfsane(par=p0, fn=f,control=list(maxit=30000)) 

It stuck with an error on first iteration only

"Failure: Error in function evaluation"

Can anyone help how to get rid of the error?

To debug this sort of error, you might print out the value of x that is being processed at each function iteration to identify the argument that is causing your function to misbehave.

When I did so, I found that the following input data is problematic:

x <- c(-1.199934, -2.745093,  3.648077)
f(x)
# [1] NaN NaN NaN

You should update your function so it doesn't return NaN for out-of-bounds data (or move to a solver that accepts constraints on the function arguments).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM