Mathematica nonlinear fit problem

Click For Summary
SUMMARY

The forum discussion centers on performing a nonlinear regression using Mathematica, specifically involving a numerical integral with the NIntegrate function. The user encounters an error indicating that the integrand evaluates to non-numerical values due to the symbolic evaluation of the fitted model. The solution involves creating a custom fitting loop to provide numeric values for parameters p1 and p2, allowing NIntegrate to function correctly. Additionally, utilizing the NumericQ function can help ensure that only numeric values are processed, potentially resolving the issue.

PREREQUISITES
  • Familiarity with Mathematica's NonlinearRegress function
  • Understanding of numerical integration using NIntegrate
  • Knowledge of symbolic versus numeric evaluation in Mathematica
  • Experience with custom loops and parameter fitting techniques
NEXT STEPS
  • Explore the implementation of custom fitting loops in Mathematica
  • Research the use of NumericQ in Mathematica for error handling
  • Learn about advanced numerical integration techniques in Mathematica
  • Investigate examples of nonlinear regression models involving integrals
USEFUL FOR

This discussion is beneficial for data scientists, researchers, and engineers who utilize Mathematica for complex nonlinear modeling and require insights into numerical integration challenges.

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 9 ·
Replies
9
Views
3K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
Replies
6
Views
1K
  • · Replies 5 ·
Replies
5
Views
2K
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K