My data gives some function integrated over bins of the argument. I want to fit the function with this data. I determine another function with the bin number as the argument and use NonlinearModelFit
. But the bin number is passed to the function as a symbol so it fails:
Part::pkspec1: The expression jpp cannot be used as a part specification.
The (simplified) example follows. The data table contains lower and upper limits of integration for each bin, log10 of the measured value of the integral and its error. The function I really need is much more complicated, so the integral cannot be done analytically.
data =
{{-1.7211, -1.4201, 0.7799, 0.0076}, {-1.4201, -1.0221, 0.4844, 0.0105},
{-1.0221, -0.545, -0.0118, 0.0184}, {-0.545, -0.0221, -0.9004, 0.0508},
{-0.0221, 0.6768, -2.1792, 0.2902}};
y[a_, b_, jp_] :=
Log[10., NIntegrate[a x + b, {x, data[[jp]][[1]], data[[jp]][[2]]}]];
nlm =
NonlinearModelFit[
data[[All, 3]], y[a, b, jpp], {{a, 0.1}, b}, jpp,
Weights -> 1/data[[All, 4]]^2, VarianceEstimatorFunction -> (1 &)];
Using Extract
instead of Part
does not help.
I would appreciate your help.
y
to be evaluated only with numerical arguments usingNumericQ
, I.e. changing its definition toClear[y]; y[a_?NumericQ, b_?NumericQ, jp_?NumericQ] := yourcode
. See this FAQ. $\endgroup$