Skip to content
Prev 299816 / 398503 Next

nls problem: singular gradient

Hi Duncan,
Thank you for your suggestion. I wrote a function "mytanh" and 
nls terminates a bit later with another error message:

Error in nls(data = dd, y ~ 1/2 * (1 - mytanh((x - ttt)/1e-04) * exp(-x/tau2)),  : 
  number of iterations exceeded maximum of 50

How can i fix that?
Kind regards,
Jonas

============================ R CODE STARTS HERE =======

mytanh <- function(x){
  return(x - x^3/3 + 2*x^5 /15 - 17 * x^7/315)
}

t <- seq(0,1,0.001)
t0 <- 0.5
tau1 <- 0.02

yy <- 1/2 * ( 1- tanh((t - t0)/0.0001) * exp(-t / tau1) ) + rnorm(length(t))*0.001

plot(x=t, y=yy, pch=18)

dd <- data.frame(y=yy, x=t)

nlsfit <- nls(data=dd,  y ~  1/2 * ( 1- mytanh((x - ttt)/0.0001) * exp(-x / tau2) ), start=list(ttt=0.5, tau2=0.02) , trace=TRUE)

============================ R CODE ENDS HERE =======