I'm having trouble writing a Nonlinear Model fit where the model is a numerical integral evaluated with NIntegrate. I have read other questions and answers about more or less the same problem, but for some reason I cannot make sense of this. Here goes:
I define a function containing the integral:
LumDist[zmax_?NumericQ, Om_?NumericQ, Ol_?NumericQ, Ot_?NumericQ,
n_?NumericQ] := NIntegrate[1/\[Sqrt](Om*(1 + x)^3 + Ol + Ot*(1 + x)^n), {x, 0, zmax}]
and from there define the model:
DistMod[zmax_?NumericQ, Om_?NumericQ, Ol_?NumericQ, Ot_?NumericQ,
n_?NumericQ, mu0_?NumericQ] :=
5*Log10[LumDist[zmax, Om, Ol, Ot, n]] + mu0
From here I write a NonlinearModelFit for this model (data) is an (x,y) List:
lmvar = NonlinearModelFit[
data, {DistMod[z, 0.28, 0.71, Ot, 10^(-3), mu0],
1 < Ot >= 0}, {{Ot, 0.08}, {mu0, 23.89}}, x, Weights -> 1/errors^2,
VarianceEstimatorFunction -> (1 &), Method -> "NMinimize"]
But this does not work, and I get lots of errors such as:
NIntegrate::nlim: x = z is not a valid limit of integration. >>
and
NIntegrate::itraw: Raw object 0.01012` cannot be used as an iterator. >>
Here is a sample of the data and errors:
data = {{0.015, 34.1114},{0.0277, 35.705},{0.048948, 36.7316},{0.0651, 37.3067},{0.100915, 38.4567},{0.159, 39.4164},{0.248508, 40.2722},{0.455, 42.3239},{0.655, 42.3151},{0.75, 43.243},{0.84, 43.5143},{0.961, 44.2642},{1.188, 44.6076},{1.34, 45.0675},{1.414, 44.8038}}
errors={0.215239,0.118114,0.175773,0.242628,0.121087,0.21481,0.152213,0.306006,0.188809,0.198402,0.471047,0.1534, 0.421,0.213,0.1345}
Can anyone tell me what I am doing wrong here?
Moreover, I can even write another simpler model (it's completely wrong, but I want to try to isolate the problem):
testfunc[a_?NumericQ, x_?NumericQ] := NIntegrate[a*t, {t, 1, x}];
Which I can try to fit using:
NonlinearModelFit[data, {testfunc[b], b > 0.}, b, x,
Method -> "NMinimize"]
but this also throws lots of errors, such as:
NonlinearModelFit::nrnum: The function value 1/2 ((-26.877+testfunc[0.0914636])^2+(-26.2011+testfunc[0.0914636])^2+(-26.1609+testfunc[0.0914636])^2+(-26.0592+testfunc[0.0914636])^2+(-26.0071+testfunc[0.0914636])^2+(-25.9072+testfunc[0.0914636])^2+(-25.7394+testfunc[0.0914636])^2+(-25.6034+testfunc[0.0914636])^2+<<35>>+(-24.5574+testfunc[0.0914636])^2+(-24.548+testfunc[0.0914636])^2+(-24.5443+testfunc[0.0914636])^2+(-24.5128+testfunc[0.0914636])^2+(-24.4717+testfunc[0.0914636])^2+(-24.4442+testfunc[0.0914636])^2+(-24.4217+testfunc[0.0914636])^2+<<996>>) is not a real number at {b} = {0.0914636}. >>
z
a numeric value? $\endgroup$