We are receiving many error messages when using NIntegrate with NonlinearModelFit. Here is a much-simplified version of the code. It arrives at the correct answer after several messages saying that it is stopping the integration. In the real version, which is fitting Beta distributions, the situation is similar but more complicated, and it is not clearly getting the best fit.

data = {{0, 0}, {1, 1}, {2, 4}}

cnet[x_, b_, c_] := NIntegrate[b + 2 c u, {u, 0, x}]

Print[cnet[0, 0, 1]]
Print[cnet[1, 0, 1]]
Print[cnet[2, 0, 1]]
Print[cnet[3, 0, 1]]

Chop[NonlinearModelFit[data, cnet[x, b, c], {b, c}, x]]
  • 5
    $\begingroup$ We really need the generic NumericQ question / answer. $\endgroup$ Commented Jan 14, 2013 at 17:09
  • $\begingroup$ Use cnet[x_?NumericQ,b_?NumericQ,c_?NumericQ]:=...(did not test for this particular piece of code). $\endgroup$ Commented Jan 14, 2013 at 17:11
  • 1
    $\begingroup$ Just an aside, but in Mathematica, you don't need to use Print to see the value of an expression at top-level. Just type the expression and press shift-enter (Win) or shift-return or keypad-enter (OS X) $\endgroup$
    – m_goldberg
    Commented Jan 14, 2013 at 17:17
  • $\begingroup$ @m_goldberg that should be shift-return, or enter. Unfortunately many keyboards (such as mine) incorrectly have "enter" written on the return key. I even saw a Toshiba laptop that produced the same scan code (that of enter) for both the return and enter keys. Using Mathematica on that one was more or less impossible... $\endgroup$ Commented Jan 14, 2013 at 17:27

1 Answer 1


I have seen this NIntegrate::nlim: u = x is not a valid limit of integration. >> message with my code generated from previous versions of Mathematica. The reason for this message comes from upgrades within the NIntegrate code to be more efficient and work more quickly. Previous versions allowed a symbol or a numeric value as input. If you use the ?NumberQ variable type explicitly, this should resolve your problem. It is kind of like type-casting your variables. The following code

Clear[data, cnet] 
data = {{0, 0}, {1, 1}, {2, 4}}

cnet[x_?NumberQ, b_?NumberQ, c_?NumberQ] := NIntegrate[b + 2 c u, {u, 0, x}]

cnet[0, 0, 1]
cnet[1, 0, 1]
cnet[2, 0, 1]
cnet[3, 0, 1]

Chop[NonlinearModelFit[data, cnet[x, b, c], {b, c}, x]]

Should give you output with no complaints.

  • 1
    $\begingroup$ No "type casting", as I see it. Just pattern matching. $\endgroup$ Commented Jan 14, 2013 at 17:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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