Re: NonlinearFit with NIntegrate, BesselJ and Normal Distribution
- To: mathgroup at smc.vnet.net
- Subject: [mg41967] Re: NonlinearFit with NIntegrate, BesselJ and Normal Distribution
- From: "Carl K. Woll" <carlw at u.washington.edu>
- Date: Wed, 11 Jun 2003 13:18:07 -0400 (EDT)
- Organization: University of Washington
- References: <bc6nek$2gk$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Ryoko, I'll give you several comments. 1. The help for NonlinearFit says the usage should be NonlinearFit[data, model, vars, params]. Hence the model, in your case sf, needs to include both the variables and the parameters. In your usage of NonlinearFit you had > NonlinearFit[adata,sf[r,s],x,{{r,10},{s,5}},... Hence, x is the variable, and your definition of sf should have read sf[r_?NumericQ, s_?NumericQ, x_?NumericQ]:= etc 2. Your definition of sf doesn't make sense. Since the PDF has no R dependence, you can take it out of the integral, and the integrand clearly diverges as R->0. Even if you changed one of the r's into an R, the integral still clearly diverges. Those NIntegrate error messages were really telling you that you had some major problems. Whenever you have a model, you ought to test it with some numerical values of the arguments to make sure the output is what you expect. 3. The derivatives of your model function sf aren't correct. One way to test that the derivatives are correct is to do a numerical check using ND from the package NumericalMath`NLimit`. Good luck. Carl Woll Physics Dept U of Washington "ryoko" <ryokofans at yahoo.com> wrote in message news:bc6nek$2gk$1 at smc.vnet.net... > Hi all, > > Using "Statistics'NonlinearFit'", is it possible to fit data with a > function that contains NIntegrate, BesselJ and NormalDistribution? > > This is what I have tried, but seems getting nowhere... > > First, I defined a function that has three parts: > > << Statistics`NormalDistribution` > > << Statistics`NonlinearFit` > > sf[r_?NumericQ, s_?NumericQ] := > NIntegrate[((3/(x R))*Sqrt[Pi/(2*(x R))]*BesselJ[0, x R])^2* > PDF[NormalDistribution[r, s], r], {R, 0, Infinity}] > > Turn off some messages during NIntegrate, > > Off[NIntegrate::slwcon]; > Off[NIntegrate::ncvb]; > Off[NIntegrate::ploss]; > > As per some of the suggestions in this forum, > > sfr[r_?NumericQ, s_?NumericQ] := NIntegrate[0, {R, 0, Infinity}]; > > sfs[r_?NumericQ, s_?NumericQ] := > NIntegrate[-9 Sqrt[Pi/2] BesselJ[0, x R]^2/(2 x^3 R^3 s^2), {R, 0, > Infinity}] > > Derivative[1, 0][sf] = sfr; > Derivative[0, 1][sf] = sfs; > > I tried to generate some dummy data, > > data = Table[{i, sf[20, 5] + Random[]10^-3}, {i, 0, 1, 0.01}] > > But unsuccessful, the error message was: > > NIntegrate::"inum" : XXXX is not numerical at XXX > > My actual exp. data was: > > adata = {{0.01138, 17.1329}, {0.01209, 16.8271}, {0.0128, 15.2979}, > {0.01351, > 15.4607}, {0.01423, 13.3394}, {0.01494, 10.1754}, {0.01565, > 8.96611}, {0.01636, 7.81989}, {0.01707, 7.23073}, {0.01778, > 7.34999}, {0.01849, 6.99683}, {0.0192, 6.28032}, {0.01991, > 6.99872}, {0.02063, 7.05303}, {0.02134, 7.61191}, {0.02205, > 6.40534}, {0.02276, 6.07442}, {0.02347, 6.02393}, {0.02418, > 4.84373}, {0.02489, 4.18738}, {0.0256, 4.11204}, {0.02632, > 3.80881}, {0.02703, 3.87838}, {0.02774, 3.59207}, {0.02845, > 3.91747}, {0.02916, 3.20307}, {0.02987, 3.38823}, {0.03058, > 3.39141}, {0.0313, 3.31697}, {0.03201, 3.3259}, {0.03272, > 2.97975}, {0.03343, 3.08749}, {0.03414, 2.77392}, {0.03485, > 2.47844}, {0.03556, 2.68963}, {0.03627, 2.65286}, {0.03698, > 2.52907}, {0.0377, 2.66072}, {0.03841, 2.71575}, {0.03912, > 2.64752}, {0.03983, 2.77143}, {0.04054, 2.75575}, {0.04125, > 2.55472}, {0.04196, 2.64779}, {0.04267, 2.51646}, {0.04339, > 2.61193}, {0.0441, 2.54807}, {0.04481, 2.54443}, {0.04552, > 2.45017}, {0.04623, 2.57174}, {0.04694, 2.49114}, {0.04765, > 2.37432}, {0.04836, 2.42763}, {0.04908, 2.53033}, {0.04979, > 2.39072}, {0.0505, 2.61487}} > > Fit my data to the function but failed with a lot of error messages, > > NonlinearFit[adata,sf[r,s],x,{{r,10},{s,5}},MaxIterations -> 100000, > ShowProgress -> True] > > Help!!! Any suggestion... > > Thanks, > ryoko >