I am having trouble using FindFit with the following function.

f[x_] := NIntegrate[a x k, {k, 0, 1}];

data = {{0, 0}, {0.2, 0.1}, {0.4, 0.2}};

param = FindFit[data, {f[x],2>a>1},a, x]

I get the following error

NIntegrate::inumr: The integrand a k x has evaluated to non-numerical values for all sampling points in the region with boundaries {{0,1}}. >>

Yet I can easily plot the function if I set "a" to any value between 1 and 2. So I assume FindFit is not assigning "a" a numerical value for some reason?

This function is the simplest function in which I can reproduce the error (The data set is similarly a toy dataset). The actual function I need to use will be more complicated, so unfortunately getting rid of NIntegrate is not possible.



1 Answer 1


This is the approach I usually use in this situation:

f[(a_)?NumericQ, (x_)?NumericQ] := NIntegrate[a*x*k, {k, 0, 1}]; 
data = {{0, 0}, {0.2, 0.1}, {0.4, 0.2}}; 
param = FindFit[data, {f[a, x], 2 > a > 1}, a, x]

The ?NumericQ predicate will prevent evaluation of f until it gets numerical values, since NIntegrate cannot work with symbolic integrands. In this particular circumstance, however, a new set of errors is generated

NIntegrate::izero: Integral and error estimates are 0 on all integration subregions. 
Try increasing the value of the MinRecursion option. If value of integral may be 0, 
specify a finite value for the AccuracyGoal option.

I will leave that one for someone else to solve.

  • $\begingroup$ Thanks, this worked for my more complicated function. $\endgroup$
    – DJames
    Nov 28, 2013 at 15:02

Not the answer you're looking for? Browse other questions tagged or ask your own question.