error in uniroot() in R, f.lower = f(lower) is NA + actual example

1.4k Views Asked by At

here is my code,

  a3<- c(-1141591.0, -1192686.3,  -402424.1,  -418201.6,  -439137.0,  -654150.5,  -670148.4 , -696922.9,  -719566.3,
         -766338.7,  -794186.4 ,-812940.5,  -835722.0,  -856374.3)
  a2<- c(271925.56, 284085.61,  95976.92,  99656.88, 104595.08, 156220.48, 159932.78,
         166278.36, 171602.05, 182776.53, 189310.01, 193667.54, 199019.19, 203847.51)
  a1<- c(1186677.7, 1239790.1,  418324.7,  434721.3,  456480.8 , 680003.3,  696628.4 ,
         724458.4,  747992.8 , 796613.2,  825555.9,  845045.8,868723.6,  890187.6)
  a0<- c(286237.4, 299037.5, 101028.3, 104902.0, 110100.1, 164442.6, 168350.3, 175029.8, 180633.7,
         192396.3, 199273.7, 203860.6, 209493.9, 214576.3)
  
  coef.mat <- matrix(rbind(a3,a2,a1,a0), nrow = 4, byrow = TRUE)
  sol.vec <- vector()
  
  j=1
  for(i in 1:ncol(coef.mat)){
      sol.vec[i]<-  uniroot(function(x) (coef.mat[i,j]*x^3)+(coef.mat[i,j+1]*x^2)+ 
                      (coef.mat[i,j+2]*x)-coef.mat[i,j+3], lower = -1, upper = 1, tol = 1e- 
   9)$root 
   
   }

for i=1 , this gives me 0.2413788, but for any other i I get error below:
f.lower = f(lower) is NA

I appreciate help with how to solve this error?

0

There are 0 best solutions below