Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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]]
share|improve this question
We really need the generic NumericQ question / answer. –  Leonid Shifrin Jan 14 '13 at 17:09
Use cnet[x_?NumericQ,b_?NumericQ,c_?NumericQ]:=...(did not test for this particular piece of code). –  Leonid Shifrin Jan 14 '13 at 17:11
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) –  m_goldberg Jan 14 '13 at 17:17
@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... –  Oleksandr R. Jan 14 '13 at 17:27
add comment

1 Answer

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.

share|improve this answer
No "type casting", as I see it. Just pattern matching. –  belisarius Jan 14 '13 at 17:43
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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