Re: Nonlinear Fit
- To: mathgroup at smc.vnet.net
- Subject: [mg59904] Re: Nonlinear Fit
- From: Peter Pein <petsie at dordos.net>
- Date: Thu, 25 Aug 2005 06:33:37 -0400 (EDT)
- References: <dehilr$buv$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Kevin Towles schrieb: > I am trying to do a nonlinear fit of a function that contains a > numerical integration. My problem is that when I call the nonlinear > fit, it tries to evaluate within the nonlinear fit call. For example: > > Test=Function[{x1,a1}, > NIntegrate[a*x^3,{x,0,100}] > ]; > > NonlinearFit[data,Test[x,a],x,{a,0,1}] > > Here I have a variable x and a parameter a. This would return an > NIntegrate error saying that 'NIntegrate is not numerical at x={50.}' > because there is an unknown, a, in the integration. > > It tries this a couple times and then shuts off the functioning of > NIntegrate for further calculations, making fitting this impossible. > Is there a way to fit this kind of situation without having the test > function evaluate prior to fitting occuring? > > Thanks for any help, > > Kevin Towles > > Hi Kevin, 1.) I do not understand, why you define a function which doesn't use the arguments. 2.) This is /really/ a FAQ. To prevent functions from evaluating with symbolic arguments use PatternTest[arg_,testfun] (see documentation). generating test data: SeedRandom[1]; data = Table[({#1, Random[Real, {.4, .6}]*(Random[Real, {.9, 1.1}]*#1)^4}&) [10^(2*Random[])], {50}]; "arg_?NumericQ" matches iff arg is numeric (surprise ;-) ): Test[(x1_)?NumericQ, (a1_)?NumericQ] := NIntegrate[a1*x^3,{x, 0, x1}]; dfit = NonlinearFit[data, Test[x, a], x, {a, 0, 1}] --> Test[x, 2.6076323810140423] DisplayTogether[ {LogLogPlot[dfit, Prepend[(#1[First /@ data] & ) /@ {Min, Max}, x]], LogLogListPlot[data, PlotStyle -> {Red, AbsolutePointSize[3]}] }]; hope this helps, Peter -- Peter Pein, Berlin GnuPG Key ID: 0xA34C5A82 http://people.freenet.de/Peter_Berlin/