Re: nonLinearFit and nonLinearRegress

*To*: mathgroup at smc.vnet.net*Subject*: [mg55346] Re: nonLinearFit and nonLinearRegress*From*: "Carl K. Woll" <carlw at u.washington.edu>*Date*: Sat, 19 Mar 2005 04:47:03 -0500 (EST)*Organization*: University of Washington*References*: <d1ecpc$etg$1@smc.vnet.net>*Sender*: owner-wri-mathgroup at wolfram.com

John, See below for my comments. "John Hawkin" <john.hawkin at gmail.com> wrote in message news:d1ecpc$etg$1 at smc.vnet.net... > I'm trying to do a nonLinear fit on some data, fitting a parameter > which appears in the limits of the integral that I'm trying to fit. > When I do a nonLinearFit, I get about a page of errors, but it does > work. It takes about 10 minutes to do it for 20 points with a > reasonable initial estimate. However when I use the nonLinearRegress > command, even with the best fit parameter from nonLinearFit given as > the initial guess, it takes hours to work. Is it possible there is a > way to speed this up, and is this normal? > > The commands that I'm using to integrate this function are: > > h[B]:=2/(B pi) NIntegrate[x Sin[x] Exp[-(x/B)^3/2], {x, 0, inf}, > Method-Oscillatory]; > holts[c_, d_, Q_]:= NIntegrate[h[B], {B, c/Q, d/Q}]; > There are a number of syntax errors in your definition of h[B]. I will assume in the following that you meant to define h[B_] as follows: h[B_] := 2/(B Pi) NIntegrate[x Sin[x] Exp[-(x/B)^(3/2)],{x,0,Infinity}, Method->Oscillatory] If the above is indeed the proper definition of h[B_], then you should be able to turn your holts function into a single integral instead of a double integral. First, let x=B t in the integral for h, to obtain h[B_] := 2B/Pi NIntegrate[t Sin[B t] Exp[-t^(3/2)], {t,0,Infinity}] Then change the order of integration in the holts integral to obtain holts[c_,d_,Q_]:=2/Pi NIntegrate[t Exp[-t^(3/2) <stuff>, {t,0,Infinity}] where <stuff> = Integral[B Sin[B t],{B,c/Q,d/Q}] Evaluating the revised holts function is practically instantaneous as compared to your definition, and produces the same answer. > where holts is the function I am fitting (the h[B] integral is known > as the Holtsmark integral). The fitting command I'm using is: > > NonlinearFit[fitData, holts[c,d,Q], {c,d}, {Q, estimateQ}]; > > where fitData contains sets of size 3, with the values of c and d as > the first two values, and the data point that I'm fitting the integral > to as the 3rd value. If anyone has any ideas on how I can perform the > nonlinear regression fast enough so that I can do it many times (maybe > 50), I would greatly appreciate it. Thanks very much, > > John Hawkin > Returning to your versions of h and holts, the first thing you should do is to make sure the code for holts and h only executes when the arguments are numerical. h[B_?NumericQ] := 2/(B Pi) NIntegrate[ etc. ] holts[c_?NumericQ,d_?NumericQ,Q_?NumericQ] := NIntegrate[ etc. ] Next, in order to use NonlinearFit or NonlinearRegress with functions which Mathematica is unable to differentiate, I like to teach Mathematica these derivatives. So, using your definition of holts, it's easy to see that the derivative of holts with respect to c, d and Q are holtsc[c_?NumericQ,d_?NumericQ,Q_?NumericQ] := -h[c/Q]/Q holtsd[c_?NumericQ,d_?NumericQ,Q_?NumericQ] := h[d/Q]/Q holtsQ[c_?NumericQ,d_?NumericQ,Q_?NumericQ] := c h[c/Q]/Q^2 - d h[d/Q]/Q^2 Then, we teach these derivatives to Mathematica as follows: Derivative[1,0,0][holts] = holtsc Derivative[0,1,0][holts] = holtsd Derivative[0,0,1][holts] = holtsQ Then, use NonlinearFit or NonlinearRegress in the usual way. With your definitions of h and holts, this takes a long time and produces lots of error messages associated with oscillating functions and recursion. Using the version of holts that I proposed above, using NonlinearFit or NonlinearRegress is an order of magnitude faster, but still produces a bunch of error messages. Carl Woll