# How do I fit NDSolve result to experimental data? [duplicate]

This question already has an answer here:

I have two coupled ordinary differential equations which I solve numerically in Mathematica, but now I want to fit the solution with experimental data.

I do following, (the experimental data corresponds to z[t] in my notation)

ss =
ParametricNDSolve[{
z'[t] == 4*(1 - X^-1)*z[t] + 4*z[t]*y[t]/X - 8*(z[t])^2 &&
y'[t] == γ*z[t] - γ*y[t]/X && z[0] == z0 &&
y[0] == y0} /. {X -> 20},
{z, y}, {t, 0, 5000}, {γ, z0, y0}]


I know the value of X = 20. The rest has to be determined by fitting. Also time has to be rescaled to fit with experimental data. hence in total of 4 parameters.

γ and y0 does not have any constraint apart from being positive. but x0 has to be very small (close to zero).

data = {{4.01, 0.0338}, {6.02, 0.0719}, {7.99, 0.14}, {9.99, 0.216}, {9.98,
0.25}, {12., 0.307}, {12., 0.35}, {14., 0.459}, {16., 0.558}, {16.,
0.605}, {18., 0.688}, {20., 0.767}, {22., 0.848}, {24.,
0.898}, {26., 0.932}, {28., 0.965}, {30., 0.98}, {32., 0.987}, {34.,
0.99}, {36., 0.994}, {38., 0.983}, {40., 0.973}, {42.,
0.968}, {44., 0.95}, {46., 0.941}, {48., 0.933}, {50., 0.918}, {52.,
0.911}, {54., 0.901}, {56., 0.89}, {58., 0.881}, {60.,
0.871}, {62., 0.864}, {64., 0.861}, {66., 0.854}, {68.,
0.846}, {70., 0.845}, {72., 0.837}, {74., 0.835}, {76.,
0.825}, {78., 0.825}, {80., 0.822}, {82., 0.817}, {84.,
0.813}, {84., 0.82}, {86., 0.812}, {88., 0.816}, {90., 0.808}, {92.,
0.805}, {94., 0.807}, {96., 0.802}, {98., 0.798}, {100.,
0.799}, {102., 0.797}, {104., 0.79}, {106., 0.797}, {108.,
0.792}, {110., 0.791}, {112., 0.788}, {114., 0.792}, {116.,
0.786}, {118., 0.787}, {120., 0.785}, {122., 0.783}, {124.,
0.788}, {126., 0.783}, {128., 0.781}, {130., 0.78}, {132.,
0.78}, {134., 0.775}, {136., 0.774}, {138., 0.777}, {140.,
0.775}, {142., 0.774}, {144., 0.77}, {146., 0.773}, {148.,
0.772}, {150., 0.768}, {152., 0.772}, {154., 0.764}, {156.,
0.77}, {158., 0.769}, {160., 0.771}, {162., 0.769}, {164.,
0.769}, {166., 0.77}, {168., 0.764}, {170., 0.765}, {172.,
0.762}, {174., 0.761}, {176., 0.762}, {178., 0.765}, {180.,
0.764}, {182., 0.763}, {184., 0.764}, {186., 0.761}, {188.,
0.762}, {190., 0.765}, {192., 0.76}, {194., 0.756}, {196.,
0.764}, {198., 0.757}, {200., 0.762}, {202., 0.758}, {204.,
0.758}, {206., 0.757}, {208., 0.753}, {210., 0.759}, {212.,
0.757}, {214., 0.758}, {216., 0.76}, {218., 0.755}, {220.,
0.758}, {222., 0.754}, {224., 0.758}, {226., 0.754}, {228.,
0.755}, {230., 0.753}, {232., 0.753}}


and then I use NonlinearModelFit](http://reference.wolfram.com/mathematica/ref/NonlinearModelFit.html) to obtain a fit, but it's really bad:

fit1 =
NonlinearModelFit[
datak,
{z[γ, z0, y0][α*t] /. ss, γ > 0, 0 < z0 < 0.05, y0 > 0, 0 < α < 0.5},
{γ, z0, y0, α}, t]

-

## marked as duplicate by Oleksandr R., RunnyKine, Dr. belisarius, m_goldberg, Yves KlettMay 28 '14 at 15:24

This question was marked as an exact duplicate of an existing question.

yes i learned it from there only but the fit is really bad thats what my concern is. – nitin May 28 '14 at 11:50

using technique described at NonlinearModelFit does not find a good fit

I managed to get a much better with some rearrangements,

ss = ParametricNDSolve[{
z'[t] == 4*(1 - X^-1)*z[t] + 4*z[t]*y[t]/X - 8*(z[t])^2 &&
y'[t] == \[Gamma]*z[t] - \[Gamma]*y[t]/X && z[0] == z0 &&
y[0] == y0} /. {X -> 3, z0 -> 0.01},
{z, y}, {t, 0, 5000}, {y0, \[Gamma]}
]

data = {{4.01, 0.0338}, {6.02, 0.0719}, {7.99, 0.14}, {9.99, 0.216}, {9.98,
0.25}, {12., 0.307}, {12., 0.35}, {14., 0.459}, {16., 0.558}, {16.,
0.605}, {18., 0.688}, {20., 0.767}, {22., 0.848}, {24.,
0.898}, {26., 0.932}, {28., 0.965}, {30., 0.98}, {32., 0.987}, {34.,
0.99}, {36., 0.994}, {38., 0.983}, {40., 0.973}, {42.,
0.968}, {44., 0.95}, {46., 0.941}, {48., 0.933}, {50., 0.918}, {52.,
0.911}, {54., 0.901}, {56., 0.89}, {58., 0.881}, {60.,
0.871}, {62., 0.864}, {64., 0.861}, {66., 0.854}, {68.,
0.846}, {70., 0.845}, {72., 0.837}, {74., 0.835}, {76.,
0.825}, {78., 0.825}, {80., 0.822}, {82., 0.817}, {84.,
0.813}, {84., 0.82}, {86., 0.812}, {88., 0.816}, {90., 0.808}, {92.,
0.805}, {94., 0.807}, {96., 0.802}, {98., 0.798}, {100.,
0.799}, {102., 0.797}, {104., 0.79}, {106., 0.797}, {108.,
0.792}, {110., 0.791}, {112., 0.788}, {114., 0.792}, {116.,
0.786}, {118., 0.787}, {120., 0.785}, {122., 0.783}, {124.,
0.788}, {126., 0.783}, {128., 0.781}, {130., 0.78}, {132.,
0.78}, {134., 0.775}, {136., 0.774}, {138., 0.777}, {140.,
0.775}, {142., 0.774}, {144., 0.77}, {146., 0.773}, {148.,
0.772}, {150., 0.768}, {152., 0.772}, {154., 0.764}, {156.,
0.77}, {158., 0.769}, {160., 0.771}, {162., 0.769}, {164.,
0.769}, {166., 0.77}, {168., 0.764}, {170., 0.765}, {172.,
0.762}, {174., 0.761}, {176., 0.762}, {178., 0.765}, {180.,
0.764}, {182., 0.763}, {184., 0.764}, {186., 0.761}, {188.,
0.762}, {190., 0.765}, {192., 0.76}, {194., 0.756}, {196.,
0.764}, {198., 0.757}, {200., 0.762}, {202., 0.758}, {204.,
0.758}, {206., 0.757}, {208., 0.753}, {210., 0.759}, {212.,
0.757}, {214., 0.758}, {216., 0.76}, {218., 0.755}, {220.,
0.758}, {222., 0.754}, {224., 0.758}, {226., 0.754}, {228.,
0.755}, {230., 0.753}, {232., 0.753}};

fit = ComplexFit[
data, {model = \[Beta]*z[y0, \[Gamma]][\[Alpha]*t] /. ss,
TransformedParameter[Re, y0] > 8,
TransformedParameter[Re, \[Gamma]] > 5,
TransformedParameter[Re, \[Beta]] > 1,
TransformedParameter[Re, \[Alpha]] < 0.05},
{y0, \[Gamma], \[Alpha], \[Beta]}, t,
"CoordinateSystem" -> "Real",
Method -> {NMinimize,
Method -> {"DifferentialEvolution", "SearchPoints" -> 4,
"ScalingFactor" -> 0.95, "CrossProbability" -> 0.5,
"PostProcess" -> {FindMinimum, Method -> "QuasiNewton"}}}]

Show[{ListPlot[data, AxesOrigin -> {0, 0}, PlotRange -> All,
Frame -> True,
FrameLabel -> {Style["Total A\[Beta] conc", 30],
Style["\!$$\*SubscriptBox[\(\[Tau]$$, $$lag$$]\)(MCS units)",
30]}, ImageSize -> {600, 400}, FrameTicks -> Automatic,
FrameTicksStyle -> Directive[30],
PlotStyle -> Directive[PointSize[0.01], Red]],
Plot[model /. fit, {t, Min@data[[All, 1]], Max@data[[All, 1]]},
PlotStyle -> Thickness[0.005], PlotRange -> All]}]


-