# Fitting system of Differential equations to a dataset

Posted 6 years ago
15442 Views
|
5 Replies
|
6 Total Likes
|
 Hello everyone! I am trying to model the kinetics of an experimental system. I am trying to find the values of 3 variables in a system of differential equations by fitting them to an experimental data set. I have values for "g" as a function of time and I would like to find the values of "k1", "k2", and "k3" that provide the best fit to my data with minimun and maximum value constraints. The values of the k's in my example below are the correct values, as I have set this up to learn how so I can apply it to a more complex system. I've tried to learn through the online resources of Mathematica but I am having trouble getting it to work for me. So in short, I would like to find the values of k1, k2, and k3 that solve my set of equations to best fit my experimental values of g. Notebook below: k1 = 20;k2 = 10;k3 = 2;totaltime = 3;dataset1 = {{0, 0}, {0.15, 0.271161}, {0.3, 0.525314}, {0.45,     0.68284}, {0.6, 0.782049}, {0.75, 0.846992}, {0.9,     0.890934}, {1.05, 0.921414}, {1.5, 0.969472}}; solution = NDSolve[    {a'[t] == -k1*a[t]*b[t],     b'[t] == -k1*a[t]*b[t],     c'[t] == k1*a[t]*b[t] - k3*c[t]*f[t],     d'[t] == -k2*d[t]*e[t],     e'[t] == -k2*d[t]*e[t],     f'[t] == k2*d[t]*e[t] - k3*c[t]*f[t],     g'[t] == k3*c[t]*f[t],     a[0] == 5,    b[0] == 2,    c[0] == 0,    d[0] == 2,    e[0] == 1,    f[0] == 0,    g[0] == 0},   {a, b, c, d, e, f, g},   {t, 0, totaltime},   MaxSteps -> Infinity];Show[{ListPlot[dataset1, PlotRange -> {{0, 1.6}, {0, 1}},    PlotRangeClipping -> True]}, Plot[Evaluate[{f[t], g[t]} /. solution],  {t, 0, totaltime}, PlotLegends -> {"f", "g"}]]
5 Replies
Sort By:
Posted 6 years ago
 For an example of how to do this, please see the documentation for NonlinearModelFit.The first example under "Generalizations and Extnesions" shows to how find the best fit parameters for a model defined using NDSolve. The major key is creating a function using ?NumericQ, in this case called model, which accepts values of the parameters and returns an Interpolation.
Posted 6 years ago
 The major key is creating a function using ?NumericQNot really necessary to use _?NumericQ, at least in version 9. Could try something like  Clear[k1, k2, k3]; totaltime = 3; dataset1 = {{0, 0}, {0.15, 0.271161}, {0.3, 0.525314}, {0.45,      0.68284}, {0.6, 0.782049}, {0.75, 0.846992}, {0.9,      0.890934}, {1.05, 0.921414}, {1.5, 0.969472}};  model = ParametricNDSolveValue[{a'[t] == -k1*a[t]*b[t],      b'[t] == -k1*a[t]*b[t], c'[t] == k1*a[t]*b[t] - k3*c[t]*f[t],      d'[t] == -k2*d[t]*e[t], e'[t] == -k2*d[t]*e[t],     f'[t] == k2*d[t]*e[t] - k3*c[t]*f[t], g'[t] == k3*c[t]*f[t],     a[0] == 5, b[0] == 2, c[0] == 0, d[0] == 2, e[0] == 1, f[0] == 0,     g[0] == 0}, g, {t, 0, totaltime}, {k1, k2, k3}];fit = FindFit[dataset1, model[k1, k2, k3][t], {k1, k2, {k3, 0}}, t](* Out[4]= {k1 -> 20.0007, k2 -> 9.99989, k3 -> 2.} *) Check the fitPlot[model[k1, k2, k3][t] /. fit, {t, 0, 1.5}, Epilog -> {Red, Point[dataset1]}]
Posted 6 years ago
 The solution posted by Ilian was exactly what I needed. It seems like using ParametricNDSolveValue was the way to go. I really appreciate the help!