[R] nls singular gradient matrix - fit parameters in integral's upper limits

Laura Teresa Corredor Bohórquez ltcorredorb at gmail.com
Thu Jul 16 14:21:18 CEST 2015


-------The las post rejected two files I had attached, so I modified
it.---------------


Hi. I am trying to make a nls fit for a little bit complicated expression that
includes two integrals with two of the fit parameters in their upper limits.

I got the error "Error in nlsModel(formula, mf, start, wts) :
singular gradient
matrix at initial parameter estimates". First of all, I have searched
already in the previous answers, but didn´t help. The parameters initialization
seems to be ok, I have tried to change the parameters but no one works. If
my function has just one integral everything works very nicely, but when adding
a second integral term just got the error. I don´t believe the function is
over-parametrized, as I have performed other fits with much more parameters
and they worked. I did try to enclose the data but the attachment was
rejected.

The minimal example is the following:

# read the data from a csv file
dados = read.csv("file.csv", header=FALSE, stringsAsFactors=FALSE)
x = 0*(1:97)
y = 0*(1:97)
for(i in 1:97){
  x[i] = dados[i,1]
  y[i] = dados[i,2]
}
integrand <- function(X) {
  return(X^4/(2*sinh(X/2))^2)
}
fitting = function(T1, T2, N, D, x){
  int1 = integrate(integrand, lower=0, upper = T1)$value
  int2 = integrate(integrand, lower=0, upper = T2)$value
  return(N*(D/x)^2*(exp(D/x)/(1+exp(D/x))^2
)+(448.956*(x/T1)^3*int1)+(299.304*(x/T2)^3*int2))
}
fit = nls(y ~ fitting(T1, T2, N, D, x),
start=list(T1=400,T2=200,N=0.01,D=2))

------>For reference, the fit that worked is the following:

# read the data from a csv file
dados = read.csv("file.csv", header=FALSE, stringsAsFactors=FALSE)
x = 0*(1:97)
y = 0*(1:97)
for(i in 1:97){
  x[i] = dados[i,1]
  y[i] = dados[i,2]
}
integrand <- function(X) {
  return(X^4/(2*sinh(X/2))^2)
}
fitting = function(T1, N, D, x){
  int = integrate(integrand, lower=0, upper = T1)$value
  return(N*(D/x)^2*(exp(D/x)/(1+exp(D/x))^2 )+(748.26)*(x/T1)^3*int)
}
fit = nls(y ~ fitting(T1 , N, D, x), start=list(T1=400,N=0.01,D=2))


I cannot figure out what happen. I need to perform this fit for three
integral components, but even for two I have this problem. I appreciate so
much your help. Thank you.

	[[alternative HTML version deleted]]



More information about the R-help mailing list