0
$\begingroup$

I tried to fit my data with a very complicate Integrated function. But I get many errors messages when I perform the fit. For exemple I get :

NIntegrate::inumr: The integrand ....has evaluated to non-numerical values for all sampling points in the region with boundaries {{Infinity,1.}}.

NonlinearModelFit::nrlnum: The function value is not a list of real numbers

I added ?NumericQ to all of my function parameters (As explained in the forum) but still doesn't work...

I used manipulate to find the parameters approximatively too and fixe them in the fit function but still I get the same error message...

Here my code and function and data...

data={{0.01635, 31756.}, {0.01691, 29033.3}, {0.01747, 28584.5}, {0.01802, 
  27148.6}, {0.01857, 27560.3}, {0.01913, 26664.5}, {0.01969, 
  24857.8}, {0.02024, 24392.}, {0.02079, 23780.3}, {0.02135, 
  23624.6}, {0.02191, 22001.8}, {0.02246, 21508.1}, {0.02301, 
  21152.}, {0.02356, 20179.9}, {0.02412, 18917.1}, {0.02468, 
  19023.4}, {0.02523, 19312.2}, {0.02579, 17525.7}, {0.02634, 
  17125.}, {0.0269, 17610.7}, {0.02745, 16233.3}, {0.028, 
  15462.2}, {0.02856, 15606}, {0.02912, 15253.8}, {0.02967, 
  14715.7}, {0.03022, 13692.8}, {0.03079, 13733.5}, {0.03135, 
  13308.5}, {0.0319, 12976.1}, {0.03244, 12597.9}, {0.033, 
  12046.9}, {0.01442, 33274.2}, {0.01575, 30759.3}, {0.01712, 
  28168.1}, {0.01851, 26533.9}, {0.01992, 24367.4}, {0.02126, 
  23074.2}, {0.0226, 21306.2}, {0.024, 20009.5}, {0.02537, 
  17511.2}, {0.02674, 17062.3}, {0.02812, 15578.5}, {0.02949, 
  14643.2}, {0.03086, 13703.2}, {0.03223, 12567.1}, {0.03361, 
  11719.5}, {0.03499, 10743.7}, {0.03638, 9760.39}, {0.03776, 
  8954.93}, {0.03911, 8508.59}, {0.04049, 7881.02}, {0.04188, 
  7283.29}, {0.04326, 6963.29}, {0.04462, 6268.47}, {0.04598, 
  5997.21}, {0.04737, 5527.98}, {0.04874, 5185.51}, {0.0501, 
  4670.73}, {0.05148, 4431.54}, {0.05287, 4191.26}, {0.05425, 
  3834.45}, {0.0556, 3620.98}, {0.05697, 3376.51}, {0.05835, 
  3279.51}, {0.05974, 2999.57}, {0.06111, 2895.32}, {0.06248, 
  2674.02}, {0.06386, 2555.34}, {0.06524, 2392.53}, {0.06661, 
  2272.93}, {0.06797, 2095.73}, {0.06935, 1956.46}, {0.07073, 
  1842.65}, {0.0721, 1767.54}, {0.07346, 1672.86}, {0.07484, 
  1551.8}, {0.07623, 1442.5}, {0.07761, 1337.48}, {0.07897, 
  1349.77}, {0.08034, 1213.42}, {0.08165, 1191.64}, {0.05499, 
  3254.36}, {0.06006, 2610.76}, {0.0653, 2034.53}, {0.07064, 
  1620.34}, {0.07594, 1273.4}, {0.08103, 1037.84}, {0.0862, 
  846.835}, {0.09152, 663.821}, {0.0968, 546.384}, {0.10211, 
  450.586}, {0.10735, 361.546}, {0.11266, 306.515}, {0.11794, 
  248.237}, {0.12301, 207.235}, {0.12825, 178.407}, {0.13365, 
  153.417}, {0.13892, 129.051}, {0.14415, 118.376}, {0.14945, 
  102.996}, {0.15476, 93.4138}, {0.16002, 80.5334}, {0.16517, 
  70.9643}, {0.17038, 63.7196}, {0.17571, 61.1032}, {0.18097, 
  54.2794}, {0.18616, 47.2895}, {0.19145, 48.1226}, {0.19672, 
  44.8735}, {0.20194, 39.2719}, {0.20718, 37.0787}, {0.2124, 
  35.3538}, {0.21762, 32.7991}, {0.22286, 30.3889}, {0.22811, 
  30.4247}, {0.23332, 28.0626}, {0.23855, 26.2669}, {0.24382, 
  26.6999}, {0.24905, 26.7481}, {0.25423, 26.3765}, {0.25945, 
  26.0366}, {0.2647, 25.5727}, {0.26988, 23.2764}, {0.27511, 
  21.555}, {0.28031, 21.5483}, {0.28555, 21.747}, {0.29076, 
  20.7841}, {0.29604, 20.5216}, {0.30106, 18.8202}, {0.3058, 20.6229}}


mu0 = 4*Pi*10^(-7);
Ms = 0.0926/mu0 ;
bH = 2.91*10^8 ;
R = 4;

(*Function*)
Heff[q_, A_, Hi_] := (Hi/mu0 + 2*A/(Ms*mu0)*(q*10^(10))^2) 
p[q_, A_, Hi_] := Ms/Heff[q, A, Hi]
Vp[r_] := 4/3*Pi*(r*10^(-9))^3 
FF[q_, r_] := 9*SphericalBesselJ[1, q*10^(10)*r*10^(-9)]^2/(q*10^(10)*r*10^(-9))^2 
RH[q_, A_, Hi_] := (1/4)*p[q, A, Hi]^2*(2 + 1/(1 + p[q, A, Hi])^(0.5)) 
RM[q_, A_, Hi_] := ((1 + p[q, A, Hi])^(0.5) - 1)/2
Mz2[q_, dM_, r_] := ((dM/mu0)^2/(8*Pi)^3)*Vp[r]^2*FF[q, r];
h2[q_, Hp_, r_] := ((Hp/mu0)^2/(8^3*Pi^3))*Vp[r]^2*FF[q, r]; 

f[r_, c_, pp_] := (1/(c*r*10^(-9)*pp))*Exp[-(Log[r*10^(-9)] - Log[R*10^(-9)])^2/(2*pp^2)]

 Sigma1[q_?NumericQ, A_?NumericQ, Hi_?NumericQ, dM_?NumericQ, 
  Hp_?NumericQ, c_?NumericQ, pp_?NumericQ, R_?NumericQ, 
  BG_?NumericQ] := 
 NIntegrate[((Mz2[q, dM, r]*RM[q, A, Hi] + h2[q, Hp, r]*RH[q, A, Hi])*
     f[r, c, pp, R]), {r, 0, 100}, MaxRecursion -> 10] + BG


Manipulate[Plot[Sigma1[q, A, Hi, dM, Hp, c, pp, R, BG], {q, 0.001, 0.9}, 
  PlotStyle -> {Thickness[0.0035], Red}, 
  ScalingFunctions -> {"Log", "Log"}, PlotRange -> All, 
  Epilog -> {PointSize[0.005], Point[Log[data]]}, 
  ImageSize -> 850], {{A, 15*10^(-12)}, 0*10^(-12), 
  30*10^(-12)}, {{Hi, 20.65}, 0, 30}, {{dM, 0.05}, 0, 
  8000}, {{Hp, 0.05}, 0, 2}, {{c, 0.005*10^(9)}, 0*10^(9), 
  1*10^(9)}, {{pp, 0.78}, 0, 1}, {{R, 4}, 0, 5}, {{BG, 18}, 0, 30}]



    singlefit1 =  Normal[NonlinearModelFit[data, Sigma1[q, 15*10^(-12), 20.65, dM, Hp, c, pp, 4, 18], {{dM, 0.05}, {Hp, 0.062}, {c, 0.005*10^(-9)}, {pp,0.78}, {BG, 18}}, q]];
singlefit2 = NonlinearModelFit[data, Sigma1[q, 15*10^(-12), 20.65, dM, Hp, c, pp, 4, 18], {{Hi, 20.65}, {dM, 0.05}, {Hp, 0.062}, {c, 0.005*10^(-9)},{pp,0.78}, {BG, 18}}, q];

singlefit2["BestFitParameters"]
singlefit2["EstimatedVariance"]
singlefit2["ParameterErrors"]
singlefit2["ParameterConfidenceIntervals"]
MatrixForm[singlefit2["CorrelationMatrix"]]
Print[singlefit2["ParameterTable"]];
$\endgroup$
  • $\begingroup$ I think you need to modify the code to get the function Sigma1 working first. For example you define p as both a function and as a parameter. (More minor is removing the lines data = ReadList[file, Number, RecordLists -> True]; dataT = Transpose[data]; dataT = {dataT[[1]], dataT[[2]]}; data = Transpose[dataT];) $\endgroup$ – JimB Oct 17 '18 at 14:59
  • $\begingroup$ I think that you might want to fit the log of the dependent variable but in the meantime here are a few specifics about the potential typos: (1) C is a protected symbol. Use lowercase letters for variable names. (2) Change p when it is meant to be a parameter rather than a function to pp. (3) Change {C, 0.4*10^(-41), {p, 0.14}} to {c, 0.4*10^(-41), {pp, 0.14} (besides changing C to c and p to pp, there is a } in the wrong place. $\endgroup$ – JimB Oct 17 '18 at 16:25
  • $\begingroup$ Thanks, I edited my code and use manipulate to find approximatively the parameters but then when I want to fit my function by fixing 2 or 3 parameters I still have the error message... $\endgroup$ – Bigprophete Oct 18 '18 at 7:21
1
$\begingroup$

I find two mistakes in your code,

First, you defined "f[r_, c_, pp_]", and then in Next step, you are using it as "f[r_, c_, pp_, R_]".

Second, In "NonlinearModelfit line", you are using value of BG twice.

In the Following I corrected your code and used "FindFit" :

 mu0 = 4*Pi*10^(-7);
 Ms = 0.0926/mu0;
 bH = 2.91*10^8;
 Heff[q_, A_, Hi_] := (Hi/mu0 + 2*A/(Ms*mu0)*(q*10^(10))^2)
 p[q_, A_, Hi_] := Ms/Heff[q, A, Hi]
 Vp[r_] := 4/3*Pi*(r*10^(-9))^3
 FF[q_, r_] := 9*SphericalBesselJ[1,q*10^(10)*r*10^(-9)]^2/(q*10^(10)*r*10^(-9))^2
 RH[q_, A_, Hi_] := (1/4)*p[q, A, Hi]^2*(2 + 1/(1 + p[q, A, Hi])^(0.5))
 RM[q_, A_, Hi_] := ((1 + p[q, A, Hi])^(0.5) - 1)/2
 Mz2[q_, dM_, r_] := ((dM/mu0)^2/(8*Pi)^3)*Vp[r]^2*FF[q, r];
 h2[q_, Hp_, r_] := ((Hp/mu0)^2/(8^3*Pi^3))*Vp[r]^2*FF[q, r];
 f[r_, c_, pp_, R_] := (1/(c*r*10^(-9)*pp))*Exp[-(Log[r*10^(-9)] - Log[R*10^(-9)])^2/(2*pp^2)]
 Sigma1[q_?NumericQ, A_?NumericQ, Hi_?NumericQ, dM_?NumericQ, 
 Hp_?NumericQ, c_?NumericQ, pp_?NumericQ, R_?NumericQ, BG_?NumericQ] := 
 NIntegrate[((Mz2[q, dM, r]*RM[q, A, Hi] + h2[q, Hp, r]*RH[q, A, Hi])*
 f[r, c, pp, R]), {r, 0, 100}, MaxRecursion -> 10] + BG
 model = Sigma1[xx, A, Hi, dM, Hp, c, pp, R, BG];
 result = FindFit[data, 
 model, {{A, 0.00000000000001}, {Hi, 20.6}, {dM, 0.05}, {Hp, 0.062}, {c, 0.005*10}, {pp, 0.78}, {R, 4}, {BG, 18}}, xx]
Plot[model /. result, {xx, 0, 0.3}, PlotStyle -> {Red, Thick}]
ListPlot[data]

This Gives the fitting parameters as,

 {A -> 1.*10^-14, Hi -> 20.6, dM -> 0.05, Hp -> 0.062, c -> 0.05, pp -> 0.78, R -> 4., BG -> 8374.02}

Fitted Funtion:

enter image description here

Listplot of data.

enter image description here

This is totally Error Free!!,

but results are not matching, this is because of inappropriate choice of fitting parameters.

For the Desired Result:

Now you change your Guess parameter for proper fitting.

Even after doing that, you don't get the proper fitting, then search for better fitting function.

$\endgroup$
  • $\begingroup$ Thanks, I will try this. If I want to put R and BG as constants and not fitting parameters during the fit, the following line is correct ? -> model = Sigma1[xx, A, Hi, dM, Hp, c, pp, 4,18] $\endgroup$ – Bigprophete Oct 18 '18 at 12:15
  • $\begingroup$ @Bigprophete, Yes It will work. $\endgroup$ – math Oct 19 '18 at 13:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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