Mathematica nonlinear fit problem

Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
2 replies · 4K views
evilcman
Messages
40
Reaction score
2
I would like to perform a complicated nonlinear fit with mathematica, which
involves a numerical integral in the fitted model. But i can't get it to work.
If I say something like:

NonlinearRegress[data, NIntegrate[f[x, p1, p2, y], {y, 0, 1}], {p1, p2}, x]

it says the integrand evaluated to non-numerical values. I guess this happens
because it tries to evaluate the fitted model symbolically.

How can I get this to work?
 
Physics news on Phys.org
I'm not entirely sure you can unless you write your own loop to do the regression. Essentially the NIntegrate will only ever take numerical values, obviously, and will be evaluated first in the regression function. So either you supply values for your parameters so this can work, and then the regression won't, or vice versa. The only way i can see around this at the moment is do to your own fitting loop where you use test values of p1 p2. This way you can do a fit when p1 and p2 are defined, whereas the inbuilt function only takes a 'form' ie non-numeric.
 
It is difficult to tell with the limited information you provided about f, p1, etc.

This kind of problem does show up in a variety of situations. I've tried to find a really good example that is doing almost exactly what you are doing and can't seem to find just the right one. If you Google for the three terms NIntegrate NumericQ and error you will find that the usual advice is to use "?NumericQ" to only match numeric values. That seems to usually solve the problem.
 
Last edited: