1
$\begingroup$

I have a problem with fitting the results of ParametricNDSolve to the experimental data. Here's my code:

eqs = {n1'[t] == 
    C3 n3[t] (NA - n1[t]) - 1/\[Tau]1  n1[t] (NA - n3[t]) - A n1[t] + 
     1/\[Tau] nnn[t] (NA - n1[t]), 
   n2'[t] == -1/\[Tau]2 n2[t] (NA - n3[t]) - A nnn[t] + 
     1/\[Tau] nnn[t] (ND - n2[t]), 
   n3'[t] == 
    -C3  n3[t] (NA - n1[t]) + 1/\[Tau]1  n1[t] (NA - n3[t]) + 
     1/\[Tau]2  n2[t] (NA - n3[t]), 
   nnn'[t] == 
    A  n1[t] + A  n2[t] - 1/\[Tau] nnn[t] (ND - n2[t]) - 
     1/\[Tau] (NA - n1[t]) - 1/\[Tau]3 nnn[t] (NZ - n4[t]), 
   n4'[t] == 1/\[Tau]3 (NZ - n4[t]) nnn[t]};

n1solve = 
  ParametricNDSolve[{eqs, nnn[0] == 0, n1[0] == 0.5, n2[0] == .1, 
    n3[0] == 2, n4[0] == 0}, 
   n1, {t, 0, 5}, {A, C3, NA, ND, 
    NZ, \[Tau], \[Tau]1, \[Tau]2, \[Tau]3}];
n2solve = 
  ParametricNDSolve[{eqs, nnn[0] == 0, n1[0] == 0.5, n2[0] == .1, 
    n3[0] == 2, n4[0] == 0}, 
   n2, {t, 0, 5}, {A, C3, NA, ND, 
    NZ, \[Tau], \[Tau]1, \[Tau]2, \[Tau]3}];

Now, that part seems fine to me - i am getting results for both n1 and n2 functions dependent on few parameters. However, the problem arises as I need to fit sum of the n1 and n2 functions (n1+n2) to the data in order to get the parameters.

NonlinearModelFit[
 data, {n1[A, C3, NA, ND, NZ, \[Tau], \[Tau]1, \[Tau]2, \[Tau]3][
     t] /. n1solve + 
     n2[A, C3, NA, ND, NZ, \[Tau], \[Tau]1, \[Tau]2, \[Tau]3][t] /. 
   n2solve, A > 0, C3 > 0, NA > 0, ND > 0, 
  NZ > 0, \[Tau] > 0, \[Tau]1 > 0, \[Tau]2 > 0, \[Tau]3 > 0}, {{A, 
   1}, {C3, 3}, {NA, 1}, {ND, 4}, {NZ, 0}, {\[Tau], 
   10}, {\[Tau]1, .5}, {\[Tau]2, 1.5}, {\[Tau]3, 5}}, t]

Now, I understand that the problem is not with the fitting itself, however, as I am unable to plot the sum of functions with the following

A = 1; C3 = 3; NA = 1; ND = 4; NZ = 0; \[Tau] = 10; \[Tau]1 = 0.5; \
\[Tau]2 = 1.5; \[Tau]3 = 5;
Plot[n1[A, C3, NA, ND, NZ, \[Tau], \[Tau]1, \[Tau]2, \[Tau]3][t] /. 
   nsumsolve + 
    n2[A, C3, NA, ND, NZ, \[Tau], \[Tau]1, \[Tau]2, \[Tau]3][t] /. 
  nsumsolve , {t, 0, 5}, PlotRange -> All]

So the question is, how should i express the sum of fucntions n1 and n2 so that I can fit it?

EDIT: Here's the data to fit to:

data={{0.0100539,1365.37},{0.0770325,1365.39},{0.144024,1365.37},{0.211026,1365.39},{0.278028,1365.43},{0.345025,1365.44},{0.412019,1365.49},{0.479008,1365.49},{0.545992,1365.48},{0.612983,1365.51},{0.679975,1365.55},{0.746967,1365.45},{0.813944,1365.5},{0.880939,1365.44},{0.947931,1365.47},{1.01492,1365.45},{1.08192,1365.4},{1.1489,1365.38},{1.21589,1365.39},{1.28288,1365.36},{1.34987,1365.37},{1.41685,1365.38},{1.48383,1365.36},{1.55082,1365.39},{1.6178,1365.34},{1.68478,1365.35},{1.75176,1365.32},{1.81875,1365.34},{1.88574,1365.32},{1.95274,1365.3},{2.01973,1365.31},{2.08672,1365.34},{2.15371,1365.32},{2.2207,1365.33},{2.28769,1365.31},{2.35468,1365.33},{2.42167,1365.28},{2.48864,1365.32},{2.55564,1365.35},{2.62261,1365.31},{2.6896,1365.34},{2.75659,1365.32},{2.82358,1365.35},{5203/1800,1365.35},{2.95753,1365.33},{3.02453,1365.32},{3.09153,1365.28},{3.1585,1365.32},{3.2255,1365.31},{3.29247,1365.32},{3.35947,1365.35},{3.42647,1365.31},{3.49344,1365.32},{3.56044,1365.33},{3.62742,1365.33},{3.69442,1365.31},{3.76142,1365.33},{3.82839,1365.31},{3.89539,1365.35},{3.96236,1365.34},{4.02936,1365.34},{4.09633,1365.35},{1249/300,1365.32},{4.23031,1365.33},{4.29731,1365.34},{4.36431,1365.33},{4.43128,1365.33},{4.49828,1365.34},{4.56525,1365.34},{4.63225,1365.34},{4.69922,1365.34},{4.76622,1365.35},{4.83319,1365.34},{4.90019,1365.31},{4.96717,1365.34}}

EDIT2: I have found minor mistake in the differential equation system (now, they have been corrected in this question). However, I'm still struggling with getting any fitting results. While inputting the parameters manually I am able to get the plot that resembles the data, when I attempt fitting I receive an error.

Here's the data I use to generate the plot

Plot[n1[1, 3, 4, 0, 1, 10, .5, 1.5, 5][t] /. n1solve, {t, 0, 5}, 
 PlotRange -> All]

And here are the errors I am receiving:

FindFit[data, {aa (n1[A, C3, 4, 0, 
        NZ, \[Tau], \[Tau]1, \[Tau]2, \[Tau]3][t] /. nsolve) + bb, 
  A >= 0, C3 >= 0, 
  NZ >= 0, \[Tau] > 0, \[Tau]1 > 0, \[Tau]2 > 0, \[Tau]3 > 0}, {{A, 
   1}, {C3, 3}, {NZ, 1}, {\[Tau], 10}, {\[Tau]1, .5}, {\[Tau]2, 
   1.5}, {\[Tau]3, 5}, {aa, 1}, {bb, 1365}}, t]

InterpolatingFunction::nomthd: There is no method 5203/1800 for InterpolatingFunction objects.
InterpolatingFunction::nomthd: There is no method 1249/300 for InterpolatingFunction objects.
FindFit::nrnum: The function value 1/2 (31.8815 +(-0.3244+1. InterpolatingFunction[{{<<2>>}},{5,7,1,{<<1>>},{<<1>>},0,0,0,0,Automatic,{},{},False},{{<<247>>}},{Developer`PackedArrayForm,{<<248>>},{<<494>>}},{Automatic}][1249/300])^2+(-0.347178+1. InterpolatingFunction[{{<<2>>}},{5,7,1,{<<1>>},{<<1>>},0,0,0,0,Automatic,{},{},False},{{<<247>>}},{Developer`PackedArrayForm,{<<248>>},{<<494>>}},{Automatic}][5203/1800])^2) is not a real number at {A,C3,NZ,\[Tau],\[Tau]1,\[Tau]2,\[Tau]3,aa,bb} = {1.,3.,1.,10.,0.5,1.5,5.,1.,1365.}.
IPOPTMinimize::badobj: Invalid objective function. The objective function doesn't evaluate to a real-valued numeric result at the initial point.
FindFit::nrgnum: The gradient is not a vector of real numbers at {A,C3,NZ,\[Tau],\[Tau]1,\[Tau]2,\[Tau]3,aa,bb} = {1.,3.,1.,10.,0.5,1.5,5.,1.,1365.}.
FindFit::grad: Evaluation of the gradient of function Experimental`NumericalFunction[{Hold[1/2 {-1365.37+bb+aa ParametricFunction[<<6>>][<<9>>][<<1>>],-1365.39+bb+aa ParametricFunction[<<6>>][<<9>>][<<1>>],-1365.37+bb+aa ParametricFunction[<<6>>][<<9>>][<<1>>],-1365.39+bb+aa ParametricFunction[<<6>>][<<9>>][<<1>>],-1365.43+bb+aa ParametricFunction[<<6>>][<<9>>][<<1>>],-1365.44+bb+aa ParametricFunction[<<6>>][<<9>>][<<1>>],-1365.49+bb+aa ParametricFunction[<<6>>][<<9>>][<<1>>],-1365.49+bb+aa ParametricFunction[<<6>>][<<9>>][<<1>>],-1365.48+bb+aa ParametricFunction[<<6>>][<<9>>][<<1>>],-1365.51+bb+aa ParametricFunction[<<6>>][<<9>>][<<1>>],-1365.55+bb+aa ParametricFunction[<<6>>][<<9>>][<<1>>],<<30>>,-1365.32+bb+aa ParametricFunction[<<6>>][<<9>>][<<1>>],-1365.35+bb+aa ParametricFunction[<<6>>][<<9>>][<<1>>],-1365.35+bb+aa ParametricFunction[<<6>>][<<9>>][<<1>>],-1365.33+bb+aa ParametricFunction[<<6>>][<<9>>][<<1>>],-1365.32+bb+aa ParametricFunction[<<6>>][<<9>>][<<1>>],-1365.28+bb+aa ParametricFunction[<<6>>][<<9>>][<<1>>],-1365.32+bb+aa ParametricFunction[<<6>>][<<9>>][<<1>>],-1365.31+bb+aa ParametricFunction[<<6>>][<<9>>][<<1>>],-1365.32+bb+aa ParametricFunction[<<6>>][<<9>>][<<1>>],<<25>>}.{-1365.37+bb+aa <<18>>[<<6>>][<<9>>][<<1>>],<<49>>,<<25>>}],Block},<<5>>] failed at {1.,3.,1.,10.,0.5,1.5,5.,1.,1365.}.

I am fitting single funciton here and declare some of the parameters to minimize number of variables. Fitting the sum of n1 and n2 results in similar errors.

I do not quite understand that, as from what I gather Mathematica suggests that the fitted function is not a real number at initial conditions. However, manually entering the numbers as parameter DOES give a real number as a result.

$\endgroup$
1
$\begingroup$

I could fit the n1 and n2 functions separately with very different fitting parameters.

result1 = FindFit[data, {aa ((n1[A, C3, 4, 0, 
      NZ, \[Tau], \[Tau]1, \[Tau]2, \[Tau]3][t] /. n1solve)) + bb,
A >= 0, C3 >= 0, NZ >= 0, \[Tau] > 0, \[Tau]1 > 0, \[Tau]2 > 0, \[Tau]3 > 0}, {{A, 1}, {C3, 3}, {NZ, 1}, {\[Tau], 10}, {\[Tau]1, .5},{\[Tau]2,1.5}, {\[Tau]3, 5}, {aa, 1}, {bb, 1365}}, t]

{A -> 1.34211, C3 -> 0.791861, NZ -> 120483., \[Tau] -> 8.56298, \[Tau]1 -> 17667.5, \[Tau]2 -> 0.351252, \[Tau]3 -> 76762.9, aa -> 0.122077, bb -> 1365.32}

result2 = FindFit[data, {aa ((n2[A, C3, 4, 0, 
      NZ, \[Tau], \[Tau]1, \[Tau]2, \[Tau]3][t] /. n2solve)) + bb,A >= 0, C3 >= 0, NZ >= 0, \[Tau] > 0, \[Tau]1 > 0, \[Tau]2 > 0, \[Tau]3 > 0}, {{A, 1}, {C3, 3}, {NZ, 1}, {\[Tau], 10}, {\[Tau]1, .5}, {\[Tau]2, 1.5}, {\[Tau]3, 5}, {aa, 1}, {bb, 1365}}, t]

{A -> 0.759609, C3 -> 0.85123, NZ -> 26825.3, \[Tau] -> 16507.8,\[Tau]1 -> 19294.2, \[Tau]2 -> 24703.3, \[Tau]3 -> 18766.5, aa -> 0.187769, bb -> 1365.44}


Show[{ListPlot[data], Plot[(aa ((n1[A, C3, 4, 0, NZ, \[Tau], \[Tau]1, \[Tau]2, \[Tau]3][t] /. n1solve)) + bb) /. result1, {t, 0, 5}, PlotStyle -> Green], Plot[(aa ((n2[A, C3, 4, 0, NZ, \[Tau], \[Tau]1, \[Tau]2, \[Tau]3][t] /. n2solve)) + bb) /. result2, {t, 0, 5}, PlotStyle -> Red]}]

Gives me the following graph (n1 in green and n2 in red):

data with n1 and n2 functions

$\endgroup$
0
$\begingroup$

There is an issue with the Mathematica syntax in your post.

The syntax of a Replace in a sum of expressions should be enclosed in ( ). So, in NonLinearModelFit:

n1[...][t] /. n1solve + n2[...][t] /. n2solve

should be

(n1[...][t] /. n1solve) + (n2[...][t] /. n2solve)

Also, the output of NonLinearModelFit should be put into the variable nsumsolve.

Your data is a simple gaussian on top of a background of the order of 1365. Usually it is good to try the input parameters by plotting the function to see if they can reach the data. From what I see, the input parameters change the function chaotically especially the overall scale and the tail at high values. Mathematica can't find a solution after few minutes, so I would focus on the initial equations to see if they correspond to what you need.

$\endgroup$
  • $\begingroup$ Thank you for your answer. Adding ( ) in the sum of functions does help and I am at least able to plot the sum of the functions. Below I'm posting the data I need to fit the model to: data={{0.0100539,1365.37},{0.0770325,1365.39},{0.144024,1365.37},{0.211026,1365.39},{0.278028,1365.43},{0.345025,1365.44},{0.412019,1365.49},{0.479008,1365.49},{0.545992,1365.48},{0.612983,1365.51},{0.679975,1365.55},{0.746967,1365.45},{0.813944,1365.5},{0.880939,1365.44},{0.947931,1365.47},{1.01492,1365.45},{1.08192,1365.4},{1.1489,1365.38},{1.21589,1365.39},{1.28288,1365.36},{1.34987,1365.37},{1.41685,1365.38}, $\endgroup$ – Igor W Dec 18 '17 at 11:44
  • $\begingroup$ @IgorW please do not use answers to post comments and if you want to add relevant info.data, please edit the question. $\endgroup$ – Kuba Dec 18 '17 at 15:22
  • $\begingroup$ I found a minor mistake in the equations, which I corrected in my question. What is more, the data is not as simple as a gaussian on background (or at least shouldn't be) as the data corresponds to the electron transitions within crystals, which I am able to observe. Quite crucial here is the slight rising of the values for t > 2, which would not be explained by that simple mathematical model. Either way, with manual searching I can find parameters that with some scaling do give some agreement with data, however, finding the exact values would be useful $\endgroup$ – Igor W Dec 19 '17 at 9:22
  • $\begingroup$ You have an error with the nsolve rules. It should be n1solve. Once this is corrected I could fit both n1 and n2 separately but the values of the parameters are very different and cannot be fit as a sum with the same fitting parameters. FindFit[data, {aa (n1[A, C3, 4, 0, NZ, [Tau], [Tau]1, [Tau]2, [Tau]3][t] /. n1solve) + bb, A >= 0, C3 >= 0, NZ >= 0, [Tau] > 0, [Tau]1 > 0, [Tau]2 > 0, [Tau]3 > 0}, {{A, 1}, {C3, 3}, {NZ, 1}, {[Tau], 10}, {[Tau]1, .5}, {[Tau]2, 1.5}, {[Tau]3, 5}, {aa, 1}, {bb, 1365}}, t] $\endgroup$ – Gwanguy Dec 19 '17 at 22:58

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.