Mathematica Mathematica nonlinear fit problem

AI Thread Summary
Performing a complicated nonlinear fit in Mathematica that includes a numerical integral can be challenging. The issue arises when using NonlinearRegress with NIntegrate, as it evaluates the integrand symbolically, leading to non-numerical values. To resolve this, it is suggested to create a custom fitting loop where parameters p1 and p2 are defined numerically, allowing the regression to work effectively. The built-in function struggles because it requires a symbolic form rather than numeric values. Additionally, searching for terms like NIntegrate, NumericQ, and related errors can provide insights, with the common advice being to use "?NumericQ" to ensure only numeric values are processed, which can help mitigate the issue.
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:

Similar threads

Replies
13
Views
2K
Replies
4
Views
2K
Replies
1
Views
3K
Replies
12
Views
5K
Replies
2
Views
3K
Replies
2
Views
4K
Back
Top