I get a nans produced warning message.How can I solve problem?

aDemand = as.data.frame(read.table("locations.dat", header = FALSE))
rho.data=as.data.frame(read.table("rhoset.dat", header = FALSE))
dij= as.data.frame(read.table("distances.dat", header = FALSE))

xi = as.vector(aDemand[,1])
xj = as.vector(aDemand[,1])
yi = as.vector(aDemand[,2])
yj = as.vector(aDemand[,2])
ri = as.vector(rho.data[,2])
rj = as.vector(rho.data[,2])

for(i in 1:length(xi)){
 for (j in 2:length(xi)){
 
   
  result1=((xj[j]+xi[i])/2)+((xj[j]-xi[i])*(ri[i]^2-rj[j]^2))/(2*dij[i,j]^2)+((yj[j]-yi[i])/(2*dij[i,j]^2))*sqrt((((ri[i]+rj[j])^2)-(dij[i,j]^2))*((dij[i,j]^2)-((ri[i]-rj[j])^2)))
  
  
  result2=((yj[j]+yi[i])/2)+((yj[j]-yi[i])*(ri[i]^2-rj[j]^2))/(2*dij[i,j]^2)+((xj[j]-xi[i])/(2*dij[i,j]^2))*sqrt((((ri[i]+rj[j])^2)-(dij[i,j]^2))*((dij[i,j]^2)-((ri[i]-rj[j])^2)))

 
}
  }

Break your formulas into pieces to see where the NaN comes from. Quite likely it's from the sqrt().

It looks like there are some * missing in between coefficients and variables and in between parentheses.

This code could also benefit from vectorizing.

use na.omit() to eliminate those null records

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.