# Nonlinearmodelfit with integral or nintegral as constrain

Until now I have been using the Nonlinearmodelfit without any issues, but I want to add a new constrain (Integral or NIntegral) to my modelfit.

My fitting function is (xt1 - xs1)*(2*Pi*freq*tao1)^(1 - alpha1)* Cos[alpha1*Pi/2]/(1 + 2*(2*Pi*freq*tao1)^(1 - alpha1)*Sin[alpha1*Pi/2] + (2*Pi*freq*tao1)^(2 - 2*alpha1)) where freq is my known variable and the others (xs1, xt1, alpha1 and tao1) the fitting parameters. Until now it was all ok, but I wanted to add as constrain NIntegral[previousfunction,{freq,1,100000}] > something. I get printed the following error "has evaluated to non-numerical values for all sampling points in the region with boundaries {{1,100000}".

I checked the integral is, in fact, performed very fast when the fit parameters are known but: Why does not the NonLinearModelFit use the parameters of the fit in order to perform the integral?

NonlinearModelFit[dato, {ximg, {0 < xs1 < 1, xs1 < xt1 < 3, 0 < alpha1 < 1, NIntegral[ximg, {freq, 1, 100000}] > propProces}}, {{xs1, inixs1}, {xt1, inixt1}, {alpha1, inialpha1}, {tao1, initao1}}, {freq}, MaxIterations -> 10000];


Thank you, probably I am missing an important code

PS: In case of wonder, I am interested on this because my actual fit is in fact:

NonlinearModelFit[dato, {(ximg1+ximg2), {0 < xs1 < 1, xs1 < xt1 < 3, 0 < alpha1 < 1, NIntegral[ximg1/(ximg1+ximg2), {freq, 1, 100000}] > propProces, 0 < xs2 < 1, xs2 < xt2 < 3, 0 < alpha2 < 1}}, {{xs1, inixs1}, {xt1, inixt1}, {alpha1, inialpha1}, {tao1, initao1}, {xs2, inixs2}, {xt2, inixt2}, {alpha2, inialpha2}, {tao2, initao2}}, {freq}, MaxIterations -> 10000];


I want the integral to calculate the weigth between the two.

data = {{1., 0.75}, {2., 0.89}, {3., 0.42}, {4., 0.99}, {5.,
0.84}, {6., 0.34}, {7., 0.83}, {8., 0.93}, {9., 0.76}, {10.,
0.11}};
const[a_?NumericQ, b_?NumericQ] :=
NIntegrate[Exp[t/b], {t, 0, Infinity}];
nlm = NonlinearModelFit[
data, {Exp[a + b x^2], const[a, b] > 1/2}, {{a, 0}, {b, -1}}, x]


Compare with the first example under "Possible Issues" of the docs for NonlinearModelFit.

• Yeah, it actually works, it does not give an error anymore. Sadly it takes loooong periods of time to fit the data (in my situation). Could be due to weird starting points or the integral is not working that fast as expected Thank you
– Jose
Jun 5, 2020 at 14:19
• @Jose You're welcome. The NIntegrate above makes the NMF[] run about 15 times longer (~0.4 sec) than the equivalent constraint -b > 1/2. Since the integral is probably of the same type, you can sometimes save some time by circumventing NIntegrate automatic adaptive approach. In my example, adding the options Method -> {"GlobalAdaptive", "SymbolicProcessing" -> 0, Method -> {"GaussKronrodRule", "Points" -> 19}}, MaxRecursion -> 0 cuts the time in half. Jun 5, 2020 at 14:35
• I have tried your method but it does not seem to improve much my calculation. I was considering using CUDA for those, but I am really new in mathematica. Probably a right method helps even more. It loos like a big gaussian. My function has a shape similar to Gaussian, but with x in the logarithmic scale. Dunn, I will keep trying
– Jose
Jun 5, 2020 at 16:56
• @Jose, Sometimes you can spend more time figuring out how to speed up a process than you save. Good luck! Jun 5, 2020 at 17:51