[R] nls() fit to a lorentzian - can I specify partials?

Robert D. Merithew merithew at ccmr.cornell.edu
Fri Oct 5 17:54:13 CEST 2001

First, thanks to all who helped me with my question about rescaling axes
on the fly.  Using unlist() and range() to set the axis ranges in advance
worked well.  I've since plotted about 300 datasets with relative ease.

Now I'm trying to fit a lossy oscillator resonance to (the square root of)
a lorentzian (testframe$y is oscillator amplitude, testframe$x is drive

lorentz <- nls( y ~ a0 / (Q*sqrt((1-(x/f0)^2)^2 + ((x/f0) * 1/Q)^2)),
               data = testframe,
               start = list (a0=1.4, f0=84321.6, Q=600000))

and am running into lots of convergence trouble (singular derivatives,
step factors heading off to 1/infinity, etc).

Does nls take symbolic partial derivatives of my expression?  (I suspect
it's doing derivatives numerically) 

Can I specify the partials in advance?

I noticed the 'deriv' function, but am baffled by its output:

> deriv(~ a0 / (Q*sqrt((1-(x/f0)^2)^2 + ((x/f0) * 1/Q)^2)),"a0")
    .expr1 <- x/f0
    .expr10 <- Q * sqrt(1 - .expr1^2^2 + .expr1 * 1/Q^2)
    .value <- a0/.expr10
    .grad <- array(0, c(length(.value), 1), list(NULL, c("a0")))
    .grad[, "a0"] <- 1/.expr10
    attr(.value, "gradient") <- .grad

I think .expr10 should look more like:

	Q * sqrt((1 - .expr1^2)^2 + (.expr1 * 1/Q)^2)

Is there simply a problem with the way the output of deriv() is displayed
(missing 2 sets of parentheses)?

Finally:  Is there a better way to do my lorentzian fits with R?


Robert Merithew
LASSP, Cornell University

r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list