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.